arraymodule.java

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

JAVA
2,383
字号
   * Helper function for extract to determine if a variable name is valid   *   * @param variableName the name to check   * @return true if the name is valid, false otherwise   */  private static boolean validVariableName(String variableName)  {    if (variableName.length() < 1)      return false;    char checkChar = variableName.charAt(0);    if (! Character.isLetter(checkChar) && checkChar != '_')      return false;    for (int k = 1; k < variableName.length(); k++) {      checkChar = variableName.charAt(k);      if (!Character.isLetterOrDigit(checkChar) && checkChar != '_')        return false;    }    return true;  }  /**   * Returns an array with everything that is in array and not in the other   * arrays using a passed callback function for comparing   *   * @param array the primary array   * @param arrays the vector of arrays to check the primary array's values   * against   * @return an array with all of the values that are in the primary array but   *         not in the other arrays   */  public Value array_diff(Env env, ArrayValue array, Value []arrays)  {    if (array == null)      return NullValue.NULL;    if (arrays.length < 1) {      env.warning("Wrong parameter count for array_diff()");      return NullValue.NULL;    }    ArrayValue diffArray = new ArrayValueImpl();    boolean valueFound;    for (Map.Entry<Value, Value> entry : array.entrySet()) {      valueFound = false;      Value entryValue = entry.getValue();      for (int k = 0; k < arrays.length && ! valueFound; k++) {        if (! (arrays[k] instanceof ArrayValue)) {          env.warning("Argument #" + (k + 2) + " is not an array");          return NullValue.NULL;        }        valueFound =          ! ((ArrayValue) arrays[k]).contains(entryValue).isNull();      }      if (! valueFound)        diffArray.put(entry.getKey(), entryValue);    }    return diffArray;  }  /*   * Returns an array whose keys are the values of the keyArray passed in,   * and whose values are all the value passed in.   *    * @param keyArray whose values are used to populate the keys of the new   * array   * @param value used as the value of the keys   *    * @return newly filled array   */  public ArrayValue array_fill_keys(Env env, ArrayValue keyArray, Value value)  {    ArrayValue array = new ArrayValueImpl();        Iterator<Value> iter = keyArray.getValueIterator(env);        while (iter.hasNext()) {      array.put(iter.next(), value);    }        return array;  }  /**   * Returns an array with everything that is in array and not in the other   * arrays, keys also used   *   * @param array the primary array   * @param arrays the vector of arrays to check the primary array's values   * against   * @return an array with all of the values that are in the primary array but   *         not in the other arrays   */  public Value array_diff_assoc(Env env, ArrayValue array, Value []arrays)  {    if (array == null)      return NullValue.NULL;    if (arrays.length < 1) {      env.warning("Wrong parameter count for array_diff()");      return NullValue.NULL;    }    ArrayValue diffArray = new ArrayValueImpl();    for (Map.Entry<Value, Value> entry : array.entrySet()) {      boolean valueFound = false;      Value entryValue = entry.getValue();      Value entryKey = entry.getKey();      for (int k = 0; k < arrays.length && ! valueFound; k++) {        if (! (arrays[k] instanceof ArrayValue)) {          env.warning("Argument #" + (k + 2) + " is not an array");          return NullValue.NULL;        }        valueFound =          ((ArrayValue) arrays[k]).contains(entryValue).eq(entryKey);      }      if (! valueFound)        diffArray.put(entryKey, entryValue);    }    return diffArray;  }  /**   * Returns an array with everything that is in array and not in the other   * arrays, keys used for comparison   *   * @param array the primary array   * @param arrays the vector of arrays to check the primary array's values   * against   * @return an array with all of the values that are in the primary array but   *         not in the other arrays   */  public Value array_diff_key(Env env, ArrayValue array, Value []arrays)  {    if (array == null)      return NullValue.NULL;    if (arrays.length < 1) {      env.warning("Wrong parameter count for array_diff()");      return NullValue.NULL;    }    ArrayValue diffArray = new ArrayValueImpl();    for (Map.Entry<Value, Value> entry : array.entrySet()) {      boolean keyFound = false;      Value entryKey = entry.getKey();      for (int k = 0; k < arrays.length && ! keyFound; k++) {        if (! (arrays[k] instanceof ArrayValue)) {          env.warning("Argument #" + (k + 2) + " is not an array");          return NullValue.NULL;        }        keyFound = ((ArrayValue) arrays[k]).containsKey(entryKey) != null;      }      if (! keyFound)        diffArray.put(entryKey, entry.getValue());    }    return diffArray;  }  /**   * Returns an array with everything that is in array and not in the other   * arrays, keys used for comparison aswell   *   * @param array the primary array   * @param arrays the vector of arrays to check the primary array's values   * against.  The last element is the callback function.   * @return an array with all of the values that are in the primary array but   *         not in the other arrays   */  public Value array_diff_uassoc(Env env, ArrayValue array, Value []arrays)  {    if (array == null)      return NullValue.NULL;    if (arrays.length < 2) {      env.warning("Wrong parameter count for array_diff()");      return NullValue.NULL;    }    AbstractFunction func =      env.findFunction(arrays[arrays.length - 1].toString().intern());    if (func == null) {      env.warning("Invalid comparison function");      return NullValue.NULL;    }    ArrayValue diffArray = new ArrayValueImpl();    for (Map.Entry<Value, Value> entry : array.entrySet()) {      boolean ValueFound = false;      Value entryValue = entry.getValue();      Value entryKey = entry.getKey();      for (int k = 0; k < arrays.length - 1 && ! ValueFound; k++) {        if (! (arrays[k] instanceof ArrayValue)) {          env.warning("Argument #" + (k + 2) + " is not an array");          return NullValue.NULL;        }        Value searchKey = ((ArrayValue) arrays[k]).contains(entryValue);        if (! searchKey.isNull())          ValueFound = ((int) func.call(env, searchKey, entryKey).toLong()) ==                       0;      }      if (! ValueFound)        diffArray.put(entryKey, entryValue);    }    return diffArray;  }  /**   * Returns an array with everything that is in array and not in the other   * arrays, keys used for comparison only   *   * @param array the primary array   * @param arrays the vector of arrays to check the primary array's values   * against.  The last element is the callback function.   * @return an array with all of the values that are in the primary array but   *         not in the other arrays   */  public Value array_diff_ukey(Env env, ArrayValue array, Value []arrays)  {    if (array == null)      return NullValue.NULL;    if (arrays.length < 2) {      env.warning("Wrong parameter count for array_diff()");      return NullValue.NULL;    }    AbstractFunction func =      env.findFunction(arrays[arrays.length - 1].toString().intern());    if (func == null) {      env.warning("Invalid comparison function");      return NullValue.NULL;    }    ArrayValue diffArray = new ArrayValueImpl();    for (Map.Entry<Value, Value> entry : array.entrySet()) {      boolean keyFound = false;      Value entryKey = entry.getKey();      for (int k = 0; k < arrays.length - 1 && ! keyFound; k++) {        if (! (arrays[k] instanceof ArrayValue)) {          env.warning("Argument #" + (k + 2) + " is not an array");          return NullValue.NULL;        }        Iterator<Value> keyItr = ((ArrayValue) arrays[k]).keySet().iterator();        keyFound = false;        while (keyItr.hasNext() && ! keyFound) {          Value currentKey = keyItr.next();          keyFound = ((int) func.call(env, entryKey, currentKey).toLong()) == 0;        }      }      if (! keyFound)        diffArray.put(entryKey, entry.getValue());    }    return diffArray;  }  /**   * Returns an array with everything that is in array and also in the other   * arrays   *   * @param array the primary array   * @param arrays the vector of arrays to check the primary array's values   * against.  The last element is the callback function.   * @return an array with all of the values that are in the primary array and   *         in the other arrays   */  public Value array_intersect(Env env, ArrayValue array, Value []arrays)  {    if (array == null)      return NullValue.NULL;    if (arrays.length < 1) {      env.warning("Wrong parameter count for array_diff()");      return NullValue.NULL;    }    ArrayValue interArray = new ArrayValueImpl();    for (Map.Entry<Value, Value> entry : array.entrySet()) {      boolean valueFound = false;      Value entryValue = entry.getValue();      for (int k = 0; k < arrays.length; k++) {        if (! (arrays[k] instanceof ArrayValue)) {          env.warning("Argument #" + (k + 2) + " is not an array");          return NullValue.NULL;        }        if (k > 0 && ! valueFound)          break;        valueFound =          ! ((ArrayValue) arrays[k]).contains(entryValue).isNull();      }      if (valueFound)        interArray.put(entry.getKey(), entryValue);    }    return interArray;  }  /**   * Returns an array with everything that is in array and also in the other   * arrays, keys are also used in the comparison   *   * @param array the primary array   * @param arrays the vector of arrays to check the primary array's values   * against.  The last element is the callback function.   * @return an array with all of the values that are in the primary array and   *         in the other arrays   */  public Value array_intersect_assoc(Env env, ArrayValue array, Value []arrays)  {    if (array == null)      return NullValue.NULL;    if (arrays.length < 1) {      env.warning("Wrong parameter count for array_diff()");      return NullValue.NULL;    }    ArrayValue interArray = new ArrayValueImpl();    for (Map.Entry<Value, Value> entry : array.entrySet()) {      boolean valueFound = false;      Value entryKey = entry.getKey();      Value entryValue = entry.getValue();      for (int k = 0; k < arrays.length; k++) {        if (! (arrays[k] instanceof ArrayValue)) {          env.warning("Argument #" + (k + 2) + " is not an array");          return NullValue.NULL;        }        if (k > 0 && ! valueFound)          break;        Value searchValue = ((ArrayValue) arrays[k]).containsKey(entryKey);        if (searchValue != null)          valueFound = searchValue.eq(entryValue);        else          valueFound = false;      }      if (valueFound)        interArray.put(entryKey, entryValue);    }    return interArray;  }  /**   * Returns an array with everything that is in array and also in the other   * arrays, keys are only used in the comparison   *   * @param array the primary array   * @param arrays the vector of arrays to check the primary array's values   * against.  The last element is the callback function.   * @return an array with all of the values that are in the primary array and   *         in the other arrays   */  public Value array_intersect_key(Env env, ArrayValue array, Value []arrays)  {    if (array == null)      return NullValue.NULL;    if (arrays.length < 1) {      env.warning("Wrong parameter count for array_diff()");      return NullValue.NULL;    }    ArrayValue interArray = new ArrayValueImpl();    for (Map.Entry<Value, Value> entry : array.entrySet()) {      boolean keyFound = false;      Value entryKey = entry.getKey();      for (int k = 0; k < arrays.length; k++) {        if (! (arrays[k] instanceof ArrayValue)) {          env.warning("Argument #" + (k + 2) + " is not an array");          return NullValue.NULL;        }        if (k > 0 && ! keyFound)          break;        keyFound = ((ArrayValue) arrays[k]).containsKey(entryKey) != null;      }      if (keyFound)        interArray.put(entryKey, entry.getValue());    }    return interArray;  }  /**   * Returns an array with everything that is in array and also in the other   * arrays, keys are also used in the comparison.  Uses a callback function for   * evalutation the keys.   *   * @param array the primary array   * @param arrays the vector of arrays to check the primary array's values   * against.  The last element is the callback function.   * @return an array with all of the values that are in the primary array and   *         in the other arrays   */ 

⌨️ 快捷键说明

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