/// <summary> /// Reduces the specified OrderedMap to a single value using a callback function. /// </summary> /// <param name="input">The OrderedMap that contains the values to be reduced.</param> /// <param name="methodName">The method name to use for reduction.</param> /// <param name="initial">The initial value of the resulting reduced value.</param> /// <param name="instance">The instance that contains the definition of the method used for reduction.</param> /// <returns>Returns a single value which represents the reduction of the OrderedMap according to the specified callback method.</returns> public static double Reduce(OrderedMap input, string methodName, int initial, object instance) { double result = initial; if (input != null && input.Count > 0) { try { System.Type theType = instance.GetType(); System.Reflection.MethodInfo callbackMethod = theType.GetMethod(methodName); if (input != null && input.Count > 0) { object[] parameters = new object[] {initial, input.GetValueAt(0)}; result = (double) callbackMethod.Invoke(instance, parameters); for (int index = 1; index < input.Count; index++) { object theValue = input.GetValueAt(index); if (System.Text.RegularExpressions.Regex.IsMatch(theValue.ToString(), OrderedMap._NUMBERREGULAREXPRESSION)) { parameters[0] = result; parameters[1] = theValue; result = (double) callbackMethod.Invoke(instance, parameters); } } } } catch (System.Exception exception) { throw exception; } } return result; }
/// <summary> /// Shifts (removes) the first element of the specified OrderedMap and returns the value. All the numeric keys are recalculated. /// </summary> /// <param name="input">The OrderedMap that will be shifted.</param> /// <returns>Returns the element that was removed from the specified OrderedMap.</returns> public static object Shift(ref OrderedMap input) { object result = null; if (input.Count > 0) { OrderedMap newOrderedMap = new OrderedMap(); result = input.GetValueAt(0); input.RemoveAt(0); foreach (string key in input.Keys) { if (OrderedMap.IsKeyInteger(key)) newOrderedMap[null] = input[key]; else newOrderedMap[key] = input[key]; } input = newOrderedMap; input.Reset(); } return result; }
/// <summary> /// Splits the specified OrderedMap into chunks /// </summary> /// <param name="input">The OrderedMap to be splitted.</param> /// <param name="size">The size of each chunk.</param> /// <param name="preserveKeys">If true, the original keys will be preserved, otherwise new integer indices will be used.</param> /// <returns>Returns a new OrderedMap containing all the chunks.</returns> public static OrderedMap Chunk(OrderedMap input, int size, bool preserveKeys) { OrderedMap newOrderedMap = null; if (input != null && input.Count > 0) { newOrderedMap = new OrderedMap(); int totalChunks = input.Count/size; totalChunks += (input.Count%size) > 0 ? 1 : 0; for (int index = 0; index < totalChunks; index++) { OrderedMap theChunk = new OrderedMap(); for (int chunkIndex = 0; chunkIndex < size; chunkIndex++) { if ((index*size) + chunkIndex < input.Count) { string key = ""; if (preserveKeys) key = input.GetKeyAt((index*size) + chunkIndex); else key = chunkIndex.ToString(); theChunk[key] = input.GetValueAt((index*size) + chunkIndex); } else break; } newOrderedMap[index] = theChunk; } } return newOrderedMap; }