Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
02/27/15 13:45:57 (10 years ago)
Author:
pfleck
Message:

#2344 Added []-operator for accessing arrays.
Note that a more generic implementation is difficult because there is no non-generic implementation of ValueTypeArray and casting to IEnumerable<object> does not work due to the generic constraint of ValueTypeArray.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Optimization/3.3/RunCollectionModification/Calculator.cs

    r12091 r12096  
    151151        case "todouble": Apply(stack, x => Convert.ToDouble(x)); break;
    152152
     153        case "[]": Apply(stack, (a, i) => GetArrayValueAtIndex(a, Convert.ToInt32(i))); break;
     154
    153155        case "==": Apply(stack, (x, y) => Equal(x, y)); break;
    154156        case "not": Apply(stack, x => !Convert.ToBoolean(x)); break;
     
    181183      var v = value as BoolValue;
    182184      if (v != null) return v.Value;
     185      return null;
     186    }
     187
     188    private static object GetArrayValue(IItem value) {
     189      if (value is IntArray || value is DoubleArray || value is BoolArray || value is StringArray)
     190        return value;
    183191      return null;
    184192    }
     
    191199          GetDoubleValue(item) ??
    192200          GetBoolValue(item) ??
     201          GetArrayValue(item) ??
    193202          item.ToString();
    194203      }
    195204      return null;
    196205    }
     206
     207    private static object GetArrayValueAtIndex(object array, int index) {
     208      if (array is IntArray)
     209        return ((IntArray)array)[index];
     210      if (array is DoubleArray)
     211        return ((DoubleArray)array)[index];
     212      if (array is BoolArray)
     213        return ((BoolArray)array)[index];
     214      if (array is StringArray)
     215        return ((StringArray)array)[index];
     216      throw new NotSupportedException(string.Format("Type {0} is not a supported array type", array.GetType().Name));
     217    }
    197218    #endregion
    198219
    199220    #region variadic equality
    200     private static bool Equal(object a, object b) { return EqualNumber(a, b) || EqualBool(a, b) || EqualString(a, b) || a == b; }
    201     private static bool EqualNumber(object a, object b) { return a is double && b is double && (double)a == (double)b; }
     221    private static bool Equal(object a, object b) { return EqualIntegerNumber(a, b) || EqualFloatingNumber(a, b) || EqualBool(a, b) || EqualString(a, b) || a == b; }
     222    private static bool EqualIntegerNumber(object a, object b) { return a is int && b is int && (int)a == (int)b; }
     223    private static bool EqualFloatingNumber(object a, object b) { return a is double && b is double && (double)a == (double)b; }
    202224    private static bool EqualBool(object a, object b) { return a is bool && b is bool && (bool)a == (bool)b; }
    203225    private static bool EqualString(object a, object b) { return a is string && b is string && ((string)a).Equals((string)b); }
Note: See TracChangeset for help on using the changeset viewer.