arraymodule.java

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

JAVA
2,383
字号
   * Undocumented alias for {@link #array_key_exists}.   */  public static boolean key_exists(Env env,                                   @ReadOnly Value key,                                   @ReadOnly Value searchArray)  {    return array_key_exists(env, key, searchArray);  }  /**   * Returns an array of the keys in the given array   *   * @param array the array to obtain the keys for   * @param searchValue the corresponding value of the returned key array   * @return an array containing the keys   */  public Value array_keys(Env env,                          @ReadOnly ArrayValue array,                          @Optional @ReadOnly Value searchValue,                          @Optional boolean isStrict)  {    if (array == null)      return NullValue.NULL;    ArrayValue newArray = new ArrayValueImpl(array.getSize());    for (Map.Entry<Value, Value> entry : array.entrySet()) {      Value entryValue = entry.getValue();      Value entryKey = entry.getKey();      if (searchValue == null || searchValue instanceof DefaultValue)        newArray.put(entryKey);      else if (entryValue.eq(searchValue))        newArray.put(entryKey);    }    return newArray;  }  /**   * Returns an array with a number of indices filled with the given value,   * starting at the start index.   *   * @param start the index to start filling the array   * @param num the number of entries to fill   * @param value the value to fill the entries with   * @return an array filled with the given value starting from the given start   *         index   */  public Value array_fill(Env env, long start, long num, Value value)  {    if (num < 0) {      env.warning("Number of elements must be positive");      return BooleanValue.FALSE;    }    ArrayValue array = new ArrayValueImpl();    for (long k = start; k < num + start; k++)      array.put(LongValue.create(k), value);    return array;  }  /**   * Returns an array with the given array's keys as values and its values as   * keys.  If the given array has matching values, the latest value will be   * transfered and the others will be lost.   *   * @param array the array to flip   * @return an array with it's keys and values swapped   */  public Value array_flip(Env env,                          ArrayValue array)  {    if (array == null)      return BooleanValue.FALSE;    ArrayValue newArray = new ArrayValueImpl();    for (Map.Entry<Value, Value> entry : array.entrySet()) {      Value entryValue = entry.getValue();      if ((entryValue.isLongConvertible()) ||          (entryValue instanceof StringValue))        newArray.put(entryValue, entry.getKey());      else        env.warning("Can only flip STRING and INTEGER values!");    }    return newArray;  }  /**   * Returns an array with either the front/end padded with the pad value.  If   * the pad size is positive, the padding is performed on the end.  If   * negative, then the array is padded on the front.  The pad size is the new   * array size.  If this size is not greater than the current array size, then   * the original input array is returned.   *   * @param input the array to pad   * @param padSize the amount to pad the array by   * @param padValue determines front/back padding and the value to place in the   * padded space   * @return a padded array   */  public Value array_pad(Env env, ArrayValue input, long padSize,                         Value padValue)  {    if (input == null)      return NullValue.NULL;    long inputSize = input.getSize();    long size = Math.abs(padSize);    if (input.getSize() >= size)      return input;    if (size - inputSize > 1048576) {      env.warning("You may only pad up to 1048576 elements at a time");      return BooleanValue.FALSE;    }    ArrayValue paddedArray = new ArrayValueImpl();    boolean padFront = padSize < 0;    Iterator<Value> keyIterator = input.keySet().iterator();    for (long ctr = 0; ctr < size; ctr++) {      Value newValue;      if (padFront && ctr < size - inputSize)        newValue = padValue;      else if ((! padFront) && ctr >= inputSize)        newValue = padValue;      else        newValue = input.get(keyIterator.next());      paddedArray.put(LongValue.create(ctr), newValue);    }    return paddedArray;  }  /**   * Returns an array that filters out any values that do not hold true when   * used in the callback function.   *   * @param array the array to filter   * @param callback the function name for filtering   * @return a filtered array   */  public Value array_filter(Env env,                            ArrayValue array,                            @Optional Callback callback)  {    if (array == null)      return NullValue.NULL;    ArrayValue filteredArray = new ArrayValueImpl();    if (callback != null) {      if (! callback.isValid()) {        env.warning("The second argument, '" + ((CallbackFunction) callback).getFunctionName() + "', should be a valid callback");        return NullValue.NULL;      }      try {        Iterator<Value> iter = array.getKeyIterator(env);        while (iter.hasNext()) {          Value key = iter.next();          Value val = array.getRaw(key);          boolean isMatch = callback.call(env, array, key, val).toBoolean();          if (isMatch) {            filteredArray.put(key, val);          }        }      }      catch (Exception t) {        log.log(Level.WARNING, t.toString(), t);        env.warning("An error occurred while invoking the filter callback");        return NullValue.NULL;      }    }    else {      for (Map.Entry<Value, Value> entry : array.entrySet()) {        if (entry.getValue().toBoolean())          filteredArray.put(entry.getKey(), entry.getValue());      }    }    return filteredArray;  }  /**   * Returns the product of the input array's elements as a double.   *   * @param array the array for who's product is to be found   * @return the produce of the array's elements   */  public Value array_product(Env env,                             ArrayValue array)  {    if (array == null)      return NullValue.NULL;    if (array.getSize() == 0)      return DoubleValue.create(0);    double product = 1;    for (Map.Entry<Value, Value> entry : array.entrySet())      product *= entry.getValue().toDouble();    return DoubleValue.create(product);  }  /**   * Appends a value to the array   *   * @return the number of elements in the final array   */  public int array_push(Env env, ArrayValue array, Value []values)  {    for (Value value : values) {      array.put(value);    }    return array.getSize();  }  /**   * Returns num sized array of random keys from the given array   *   * @param array the array from which the keys will come from   * @param num the number of random keys to return   * @return the produce of the array's elements   */  public Value array_rand(Env env,                          ArrayValue array,                          @Optional("1") long num)  {    if (array == null)      return NullValue.NULL;    if (array.getSize() == 0)      return NullValue.NULL;    if (num < 1 || array.getSize() < num) {      env.warning("Second argument has to be between 1 and the number of " +                  "elements in the array");      return NullValue.NULL;    }    long arraySize = array.getSize();    Value[] keys = new Value[(int) arraySize];    array.keySet().toArray(keys);    if (num == 1) {      int index = (int) (RandomUtil.getRandomLong() % arraySize);      if (index < 0)        index *= -1;      return keys[index];    }    int length = keys.length;    for (int i = 0; i < length; i++) {      int rand = RandomUtil.nextInt(length);      Value temp = keys[rand];      keys[rand] = keys[i];      keys[i] = temp;    }    ArrayValue randArray = new ArrayValueImpl();    for (int i = 0; i < num; i++) {      randArray.put(keys[i]);    }    return randArray;  }  /**   * Returns the value of the array when its elements have been reduced using   * the callback function.   *   * @param array the array to reduce   * @param callback the function to use for reducing the array   * @param initialValue used as the element before the first element of the   * array for purposes of using the callback function   * @return the result from reducing the input array with the callback   *         function   */  public Value array_reduce(Env env,                            ArrayValue array,                            Callback callback,                            @Optional("NULL") Value initialValue)  {    if (array == null)      return NullValue.NULL;    if (callback == null || ! callback.isValid()) {      env.warning("The second argument, '" + callback +                  "', should be a valid callback");      return NullValue.NULL;    }    Value result = initialValue;    for (Map.Entry<Value, Value> entry : array.entrySet()) {      try {        // XXX: will this callback modify the array?        result = callback.call(env, result, entry.getValue());      }      catch (Exception t) {        // XXX: may be used for error checking later        log.log(Level.WARNING, t.toString(), t);        env.warning("An error occurred while invoking the reduction callback");        return NullValue.NULL;      }    }    return result;  }  /**   * Returns the inputted array reversed, preserving the keys if keyed is true   *   * @param inputArray the array to reverse   * @param keyed true if the keys are to be preservered   * @return the array in reverse   */  public Value array_reverse(Env env,                             ArrayValue inputArray,                             @Optional("false") boolean keyed)  {    if (inputArray == null)      return NullValue.NULL;    Map.Entry<Value, Value>[] entryArray =      new Map.Entry[inputArray.getSize()];    inputArray.entrySet().toArray(entryArray);    ArrayValue newArray = new ArrayValueImpl();    int newIndex = 0;    for (int index = entryArray.length - 1; index > -1; index--) {      Value currentKey = entryArray[index].getKey();      Value currentValue = entryArray[index].getValue();      if (keyed || (currentKey instanceof StringValue))        newArray.put(currentKey, currentValue);      else {        newArray.put(LongValue.create(newIndex), currentValue);        newIndex++;      }    }    return newArray;  }  /**   * Returns the key of the needle being searched for or false if it's not   * found   *   * @param needle the value to search for   * @param array the array to search   * @param strict checks for type aswell   * @return the key of the needle   */  public Value array_search(Env env,                            @ReadOnly Value needle,                            @ReadOnly ArrayValue array,                            @Optional("false") boolean strict)  {    // php/171i    // php/172y        if (array == null)      return BooleanValue.FALSE;    Iterator<Map.Entry<Value, Value>> iterator = array.getIterator(env);    while (iterator.hasNext()) {      Map.Entry<Value, Value> entry = iterator.next();      Value entryValue = entry.getValue();      Value entryKey = entry.getKey();      if (needle.eq(entryValue)) {        if (strict) {          if ((entryValue.getType()).equals(needle.getType()))            return entryKey;        }        else          return entryKey;      }    }    return BooleanValue.FALSE;  }  /**   * Shifts the elements in the array left by one, returning the leftmost value   *   * @param array the array to shift   * @return the left most value in the array   */  public Value array_shift(Env env,                           ArrayValue array)  {    if (array == null)      return NullValue.NULL;    if (array.getSize() < 1)      return NullValue.NULL;    Iterator<Value> iterator = array.keySet().iterator();    Value firstValue = array.remove(iterator.next());    array.keyReset(0, NOT_STRICT);    return firstValue;  }  /**   * Returns a chunk of the array.  The offset is the start index, elements is   * the number of values to take, and presKeys is if the keys are to be   * preserved. If offset is negative, then it's that number from the end of the   * array.  If elements is negative, then the new array will have from offset   * to elements number of values.   *   * @param array the array to take the chunk from   * @param offset the start index for the new array chunk   * @param elements the number of elements in the array chunk   * @param presKeys true if the keys of the elements are to be preserved, false   * otherwise   * @return the array chunk   */  public Value array_slice(Env env,                           ArrayValue array,                           long offset,                           @Optional("NULL") Value elements,                           @Optional("false") boolean presKeys)  {    if (array == null)      return NullValue.NULL;    long size = array.getSize();    long startIndex = offset;

⌨️ 快捷键说明

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