/*/// <summary>Maps a list to another list of the same length.</summary> * /// <param name="map">A function that transforms each item in the list.</param> * /// <returns>The list after the map function is applied to each item. The * /// original VList structure is not modified.</returns> * public WList<Out> Select<Out>(Func<T, Out> map) * { * WList<Out> newList = new WList<Out>(); * VListBlock<T>.Select<Out>(Block, LocalCount, map, newList); * return newList; * }*/ /// <summary>Transforms a list (combines filtering with selection and more).</summary> /// <param name="x">Method to apply to each item in the list</param> /// <returns>A list formed from transforming all items in the list</returns> /// <remarks>See the documentation of FVList.Transform() for more information.</remarks> public WList <T> Transform(VListTransformer <T> x) { WList <T> newList = new WList <T>(); VListBlock <T> .Transform(Block, LocalCount, x, true, newList); return(newList); }
/*/// <summary>Maps a list to another list of the same length.</summary> * /// <param name="map">A function that transforms each item in the list.</param> * /// <returns>The list after the map function is applied to each item. The * /// original VList structure is not modified.</returns> * public FWList<Out> Select<Out>(Func<T, Out> map) * { * FWList<Out> newList = new FWList<Out>(); * VListBlock<T>.Select<Out>(Block, LocalCount, map, newList); * return newList; * }*/ /// <summary>Transforms a list (combines filtering with selection and more).</summary> /// <param name="x">Method to apply to each item in the list</param> /// <returns>A list formed from transforming all items in the list</returns> /// <remarks>See the documentation of FVList.Transform() for more information.</remarks> public FWList <T> Transform(VListTransformer <T> x) { FWList <T> newList = new FWList <T>(); VListBlock <T> .Transform(Block, LocalCount, x, false, newList); return(newList); }
/// <summary>Transforms a list (combines filtering with selection and more).</summary> /// <param name="x">Method to apply to each item in the list</param> /// <returns>A list formed from transforming all items in the list</returns> /// <remarks>See the documentation of FVList.Transform() for more information.</remarks> public VList <T> Transform(VListTransformer <T> x) { return((VList <T>) VListBlock <T> .Transform(_block, _localCount, x, true, null)); }
/// <summary>Transforms a list (combines filtering with selection and more).</summary> /// <param name="x">Method to apply to each item in the list</param> /// <returns>A list formed from transforming all items in the list</returns> /// <remarks> /// This is my attempt to make an optimized multi-purpose routine for /// transforming a FVList or RVList. It is slightly cumbersome to use, /// but allows you to do several common operations in one transformer /// method. /// <para/> /// The VListTransformer method takes two arguments: an item and its index /// in the FVList or RVList. It can modify the item if desired, and then it /// returns a XfAction value, which indicates the action to take. Most /// often you will return XfAction.Drop, XfAction.Keep, XfAction.Change, /// which, repectively, drop the item from the output list, copy the item /// to the output list unchanged (even if you modified the item), and /// copy the item to the output list (assuming you changed it). /// <para/> /// Transform() needs to know if the item changed, at least at first, /// because if the first items are kept without changes, then the output /// list can share a common tail with the input list. If the transformer /// method returns XfAction.Keep for every element, then the output list /// is exactly the same (operator== returns true). /// <para/> /// Of course, it would have been simpler just to return a boolean /// indicating whether to keep the item, and the Transform method itself /// could check whether the item changed. But checking for equality is /// a tad slow in the .NET framework, because there is no bitwise /// equality operator in .NET, so a virtual function would have to be /// called instead to test equality, which is especially slow if T is a /// value type that does not implement IEquatable(of T). /// <para/> /// The final possible action, XfAction.Repeat, is like XfAction.Change /// except that Transform() calls the VListTransformer again. The second /// call has the form x(~i, ref item), where ~i is the bitwise NOT of the /// index i, and item is the same item that x returned the first time it /// was called. On the second call, x() can return XfAction.Change again /// to get a third call, if it wants. /// <para/> /// XfAction.Repeat is best explained by example. In the following /// examples, assume "list" is an RVList holding the numbers (1, 2, 3): /// <example> /// output = list.Transform((i, ref n) => { /// // This example produces (1, 1, 2, 2, 3, 3) /// return i >= 0 ? XfAction.Repeat : XfAction.Keep; /// }); /// /// output = list.Transform((i, ref n) => { /// // This example produces (1, 10, 2, 20, 3, 30) /// if (i >= 0) /// return XfAction.Repeat; /// n *= 10; /// return XfAction.Change; /// }); /// /// output = list.Transform((i, ref n) => { /// // This example produces (10, 1, 20, 2, 30, 3) /// if (i >= 0) { /// n *= 10; /// return XfAction.Repeat; /// } /// return XfAction.Keep; /// }); /// /// output = list.Transform((i, ref n) => { /// // This example produces (10, 100, 1000, 20, 200, 30, 300) /// n *= 10; /// if (n > 1000) /// return XfAction.Drop; /// return XfAction.Repeat; /// }); /// </example> /// And now for some examples using XfAction.Keep, XfAction.Drop and /// XfAction.Change. Assume list is an RVList holding the following /// integers: (-1, 2, -2, 13, 5, 8, 9) /// <example> /// output = list.Transform((i, ref n) => /// { // Keep every second item: (2, 13, 8) /// return (i % 2) == 1 ? XfAction.Keep : XfAction.Drop; /// }); /// /// output = list.Transform((i, ref n) => /// { // Keep odd numbers: (-1, 13, 5, 9) /// return (n % 2) != 0 ? XfAction.Keep : XfAction.Drop; /// }); /// /// output = list.Transform((i, ref n) => /// { // Keep and square all odd numbers: (1, 169, 25, 81) /// if ((n % 2) != 0) { /// n *= n; /// return XfAction.Change; /// } else /// return XfAction.Drop; /// }); /// /// output = list.Transform((i, ref n) => /// { // Increase each item by its index: (-1, 3, 0, 16, 9, 13, 15) /// n += i; /// return i == 0 ? XfAction.Keep : XfAction.Change; /// }); /// </example> /// </remarks> public FVList <T> Transform(VListTransformer <T> x) { return(VListBlock <T> .Transform(_block, _localCount, x, false, null)); }