arraymodule.java

来自「RESIN 3.2 最新源码」· Java 代码 · 共 2,383 行 · 第 1/5 页

JAVA
2,383
字号
    if (offset < 0)      startIndex = size + offset;    long endIndex = size;    if (! elements.isNull()) {      endIndex = elements.toLong();      if (endIndex < 0)        endIndex += size;      else        endIndex += startIndex;    }    Iterator<Map.Entry<Value, Value>> iterator = array.entrySet().iterator();    ArrayValue slicedArray = new ArrayValueImpl();    for (int k = 0; k < endIndex && iterator.hasNext(); k++) {      Map.Entry<Value, Value> entry = iterator.next();      if (k >= startIndex) {        Value entryKey = entry.getKey();        Value entryValue = entry.getValue();        if ((entryKey instanceof StringValue) || presKeys)          slicedArray.put(entryKey, entryValue);        else          slicedArray.put(entryValue);      }    }    return slicedArray;  }  /**   * Returns the removed chunk of the arrayV and splices in replace.  If offset   * is negative, then the start index is that far from the end.  Otherwise, it   * is the start index.  If length is not given then from start index to the   * end is removed.  If length is negative, that is the index to stop removing   * elements.  Otherwise that is the number of elements to remove.  If replace   * is given, replace will be inserted into the arrayV at offset.   *   * @param array the arrayV to splice   * @param offset the start index for the new arrayV chunk   * @param length the number of elements to remove / stop index   * @param replace the elements to add to the arrayV   * @return the part of the arrayV removed from input   */  public Value array_splice(Env env,                            @Reference Value arrayVar, //array gets spliced at offset                            int offset,                            @Optional("NULL") Value length,                            @Optional Value replace)  {    if (! arrayVar.isset())      return NullValue.NULL;    ArrayValue array = arrayVar.toArrayValue(env);    if (array == null)      return NullValue.NULL;        int size = array.getSize();    int startIndex = offset;    if (startIndex < 0)      startIndex += size;    int endIndex = size;    if (! length.isNull()) {      endIndex = length.toInt();      if (endIndex < 0)        endIndex += size;      else        endIndex += startIndex;    }    return spliceImpl(env, arrayVar, array, startIndex, endIndex,                      (ArrayValue) replace.toArray());  }  public Value spliceImpl(Env env,                          Value var,                          ArrayValue array,                          int start,                          int end,                          ArrayValue replace)  {    int index = 0;    ArrayValue newArray = new ArrayValueImpl();    ArrayValue result = new ArrayValueImpl();    var.set(newArray);    for (Map.Entry<Value,Value> entry : array.entrySet()) {      Value key = entry.getKey();      Value value = entry.getValue();            if (start == index && replace != null) {        Iterator<Value> replaceIter = replace.getValueIterator(env);        while (replaceIter.hasNext()) {          newArray.put(replaceIter.next());        }      }            if (start <= index && index < end) {        if (key.isString())          result.put(key, value);        else          result.put(value);      }      else {        if (key.isString())          newArray.put(key, value);        else          newArray.put(value);      }      index++;    }    if (index <= start && replace != null) {      Iterator<Value> replaceIter = replace.getValueIterator(env);      while (replaceIter.hasNext()) {        newArray.put(replaceIter.next());      }    }    return result;  }  /**   * Returns the sum of the elements in the array   *   * @param array the array to sum   * @return the sum of the elements   */  public Value array_sum(Env env,                         @ReadOnly ArrayValue array)  {    if (array == null)      return NullValue.NULL;    double sum = 0;    for (Map.Entry<Value, Value> entry : array.entrySet())      sum += entry.getValue().toDouble();    return DoubleValue.create(sum);  }  // XXX: array_udiff  // XXX: array_udiff_assoc  // XXX: array_udiff_uassoc  // XXX: array_uintersect  // XXX: array_uintersect_assoc  // XXX: array_uintersect_uassoc  /**   * Returns the inputted array without duplicates   *   * @param array the array to get rid of the duplicates from   * @return an array without duplicates   */  public Value array_unique(Env env,                            ArrayValue array)  {    if (array == null)      return BooleanValue.FALSE;    array.sort(CNO_VALUE_NORMAL, NO_KEY_RESET, NOT_STRICT);    Map.Entry<Value, Value> lastEntry = null;    ArrayValue uniqueArray = new ArrayValueImpl();    for (Map.Entry<Value, Value> entry : array.entrySet()) {      Value entryValue = entry.getValue();      if (lastEntry == null) {        uniqueArray.put(entry.getKey(), entryValue);        lastEntry = entry;        continue;      }      Value lastEntryValue = lastEntry.getValue();      if (! entryValue.toString().equals(lastEntryValue.toString()))        uniqueArray.put(entry.getKey(), entryValue);      lastEntry = entry;    }    uniqueArray.sort(CNO_KEY_NORMAL, NO_KEY_RESET, NOT_STRICT);    return uniqueArray;  }  /**   * Prepends the elements to the array   *   * @param array the array to shift   * @param values   * @return the left most value in the array   */  public Value array_unshift(Env env,                             ArrayValue array,                             Value []values)  {    if (array == null)      return NullValue.NULL;    for (int i = values.length - 1; i >= 0; i--) {      array.unshift(values[i]);    }    array.keyReset(0, NOT_STRICT);    return array;  }  /**   * Returns the values in the passed array with numerical indices.   *   * @param array the array to get the values from   * @return an array with the values of the passed array   */  public Value array_values(Env env,                            ArrayValue array)  {    if (array == null)      return NullValue.NULL;    ArrayValue arrayValues = new ArrayValueImpl();    for (Map.Entry<Value, Value> entry : array.entrySet())      arrayValues.put(entry.getValue());    return arrayValues;  }  /**   * Executes a callback on each of the elements in the array.   *   * @param array the array to walk along   * @param callback the callback function   * @param userData extra parameter required by the callback function   *   * @return true if the walk succedded, false otherwise   */  public boolean array_walk(Env env,                            @NotNull ArrayValue array,                            Callback callback,                            @Optional("NULL") Value userData)  {    if (callback == null || ! callback.isValid()) {      env.error(L.l("'{0}' is an unknown function.", callback.getCallbackName()));      return false;    }    if (array == null)      return false;    try {      Value []keyArray = array.getKeyArray(env);      for (int i = 0; i < keyArray.length; i++) {        Value key = keyArray[i];        Value val = array.getRaw(key);                callback.call(env, array, key, val, key, userData);      }            return true;    }    catch (Exception e) {      log.log(Level.WARNING, e.toString(), e);      env.warning("An error occured while invoking the callback", e);            return false;    }  }  /**   * Recursively executes a callback function on all elements in the array,   * including elements of elements (i.e., arrays within arrays).  Returns true   * if the process succeeded, otherwise false.   *   * @param array the array to walk   * @param call the name of the callback function   * @param extra extra parameter required by the callback function   * @return true if the walk succedded, false otherwise   */  public boolean array_walk_recursive(Env env,                                      @NotNull ArrayValue array,                                      Callback callback,                                      @Optional("NULL") Value extra)  {    if (callback == null || ! callback.isValid()) {      env.error(L.l("'{0}' is an unknown function.", callback.getCallbackName()));      return false;    }    if (array == null)      return false;    try {      Value []keyArray = array.getKeyArray(env);      for (int i = 0; i < keyArray.length; i++) {        Value key = keyArray[i];        Value val = array.getRaw(key);        if (val.isArray()) {          boolean result = array_walk_recursive(env,                                                (ArrayValue)val.toValue(),                                                callback,                                                extra);          if (! result)            return false;        }        else {          callback.call(env, array, key, val, key, extra);        }      }      return true;    }    catch (Exception e) {      log.log(Level.WARNING, e.toString(), e);      env.warning("An error occured while invoking the callback", e);      return false;    }  }  /**   * Sorts the array based on values in reverse order, preserving keys   *   * @param array the array to sort   * @param sortFlag provides optional methods to process the sort   * @return true if the sort works, false otherwise   * @throws ClassCastException if the elements are not mutually comparable   */  public boolean arsort(Env env, ArrayValue array,                        @Optional long sortFlag)  {    if (array == null)      return false;    switch ((int) sortFlag) {    case SORT_STRING:      array.sort(CS_VALUE_REVERSE, NO_KEY_RESET, NOT_STRICT);      break;    case SORT_NUMERIC:      array.sort(CN_VALUE_REVERSE, NO_KEY_RESET, NOT_STRICT);      break;    case SORT_LOCALE_STRING:      Locale locale = env.getLocaleInfo().getCollate().getLocale();      array.sort(new CompareLocale(ArrayValue.GET_VALUE, SORT_REVERSE,                                   Collator.getInstance(locale)),                 NO_KEY_RESET, NOT_STRICT);      break;    default:      array.sort(CNO_VALUE_REVERSE, NO_KEY_RESET, NOT_STRICT);      break;    }    return true;  }  /**   * Sorts the array based on values in ascending order, preserving keys   *   * @param array the array to sort   * @param sortFlag provides optional methods to process the sort   * @return true if the sort works, false otherwise   * @throws ClassCastException if the elements are not mutually comparable   */  static public boolean asort(Env env, ArrayValue array,                              @Optional long sortFlag)  {    if (array == null)      return false;    switch ((int) sortFlag) {    case SORT_STRING:      array.sort(CS_VALUE_NORMAL, NO_KEY_RESET, NOT_STRICT);      break;case SORT_NUMERIC:      array.sort(CN_VALUE_NORMAL, NO_KEY_RESET, NOT_STRICT);      break;    case SORT_LOCALE_STRING:      Locale locale = env.getLocaleInfo().getCollate().getLocale();      array.sort(new CompareLocale(ArrayValue.GET_VALUE, SORT_NORMAL,                                   Collator.getInstance(locale)),                 NO_KEY_RESET, NOT_STRICT);      break;    default:      array.sort(CNO_VALUE_NORMAL, NO_KEY_RESET, NOT_STRICT);      break;    }    return true;  }  /**   * Sorts the array based on keys in ascending order, preserving keys   *   * @param array the array to sort   * @param sortFlag provides optional methods to process the sort   * @return true if the sort works, false otherwise   * @throws ClassCastException if the elements are not mutually comparable   */  static public boolean ksort(Env env, ArrayValue array,                              @Optional long sortFlag)  {    if (array == null)      return false;    switch ((int) sortFlag) {    case SORT_STRING:      array.sort(CS_KEY_NORMAL, NO_KEY_RESET, NOT_STRICT);      break;    case SORT_NUMERIC:      array.sort(CN_KEY_NORMAL, NO_KEY_RESET, NOT_STRICT);      break;    case SORT_LOCALE_STRING:      Locale locale = env.getLocaleInfo().getCollate().getLocale();      array.sort(new CompareLocale(ArrayValue.GET_KEY, SORT_NORMAL,                                   Collator.getInstance(locale)),                 NO_KEY_RESET, NOT_STRICT);      break;    default:      array.sort(CNO_KEY_NORMAL, NO_KEY_RESET, NOT_STRICT);      break;    }    return true;  }  /**   * Sorts the array based on keys in reverse order, preserving keys   *   * @param array the array to sort   * @param sortFlag provides optional methods to process the sort   * @return true if the sort works, false otherwise   * @throws ClassCastException if the elements are not mutually comparable   */  public boolean krsort(Env env, ArrayValue array,                        @Optional long sortFlag)  {    if (array == null)      return false;    switch ((int) sortFlag) {    case SORT_STRING:      array.sort(CS_KEY_REVERSE, NO_KEY_RESET, NOT_STRICT);      break;    case SORT_NUMERIC:      array.sort(CN_KEY_REVERSE, NO_KEY_RESET, NOT_STRICT);      break;    case SORT_LOCALE_STRING:      Locale locale = env.getLocaleInfo().getCollate().getLocale();      array.sort(new CompareLocale(ArrayValue.GET_KEY, SORT_REVERSE,                                   Collator.getInstance(locale)),                 NO_KEY_RESET, NOT_STRICT);

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?