Пример #1
0
        /**
         * Constructs a new {@code GroupBy}
         *
         * @param l     the {@link List} containing the items used as input to the
         *              key generating function.
         * @param fn    the {@link Function} to be used to generate the keys which describe
         *              the like contents of each grouping.
         */
        public GroupBy(List <T> l, Func <T, R> fn)
        {
            this.iter  = l;
            this.fn    = fn;
            this.range = IntGenerator.Of(0, iter.Count);

            if (range.MoveNext())
            {
                T t = iter[range.Current];
                //next = new Pair<T, R>(t, fn.apply(t));
                //T t = (T) Convert.ChangeType(range.Current, typeof(T));
                next = new Tuple <T, R>(t, fn(t));
            }
        }
Пример #2
0
        /**
         * Like {@link #neighborhood(int, int)}, except that the neighborhood isn't truncated when it's
         * near an edge. It wraps around to the other side.
         *
         * @param centerIndex       The index of the point. The coordinates are expressed as a single index by
         *                          using the dimensions as a mixed radix definition. For example, in dimensions
         *                          42x10, the point [1, 4] is index 1*420 + 4*10 = 460.
         * @param radius            The radius of this neighborhood about the centerIndex.
         * @return  The points in the neighborhood, including centerIndex.
         */
        public int[] WrappingNeighborhood(int centerIndex, int radius)
        {
            int[] cp = CoordinatesFromIndex(centerIndex);

            IntGenerator[] igs = ArrayUtils.Range(0, dimensions.Length)
                                 .Select(i => new IntGenerator(cp[i] - radius, Math.Min((cp[i] - radius) + dimensions[i] - 1, cp[i] + radius) + 1))
                                 .ToArray();//IntGenerator[]::new

            List <List <int> > result = new List <List <int> >();

            result.Add(new List <int>());
            List <List <int> > interim = new List <List <int> >();

            for (int i = 0; i < igs.Length; i++)
            {
                IntGenerator pool = igs[i];
                int          size = result.Count;
                interim.Clear();
                interim.AddRange(result);
                result.Clear();
                for (int x = 0; x < size; x++)
                {
                    List <int> lx = interim[x];
                    pool.Reset();
                    for (int y = 0; y < pool.Count; y++)
                    {
                        pool.MoveNext();
                        int        py = ArrayUtils.Modulo(pool.Current, dimensions[i]);
                        List <int> tl = new List <int>();
                        tl.AddRange(lx);
                        tl.Add(py);
                        result.Add(tl);
                    }
                }
            }

            return(result.AsParallel().Select(tl => IndexFromCoordinates(tl.ToArray())).ToArray());
        }
Пример #3
0
        /**
         * Get the points in the neighborhood of a point.
         *
         * A point's neighborhood is the n-dimensional hypercube with sides ranging
         * [center - radius, center + radius], inclusive. For example, if there are two
         * dimensions and the radius is 3, the neighborhood is 6x6. Neighborhoods are
         * truncated when they are near an edge.
         *
         * @param centerIndex       The index of the point. The coordinates are expressed as a single index by
         *                          using the dimensions as a mixed radix definition. For example, in dimensions
         *                          42x10, the point [1, 4] is index 1*420 + 4*10 = 460.
         * @param radius            The radius of this neighborhood about the centerIndex.
         * @return  The points in the neighborhood, including centerIndex.
         */
        public int[] Neighborhood(int centerIndex, int radius)
        {
            centerPosition = CoordinatesFromIndex(centerIndex);

            igs = ArrayUtils.Range(0, dimensions.Length)
                  .Select(i => IntGenerator.Of(Math.Max(0, centerPosition[i] - radius), Math.Min(dimensions[i] - 1, centerPosition[i] + radius) + 1))
                  .ToArray();//IntGenerator[].@new

            List <List <int> > result = new List <List <int> >();

            result.Add(new List <int>());
            List <List <int> > interim = new List <List <int> >();

            foreach (IntGenerator pool in igs)
            {
                int size = result.Count;
                interim.Clear();
                interim.AddRange(result);
                result.Clear();
                for (int x = 0; x < size; x++)
                {
                    List <int> lx = interim[x];
                    pool.Reset();
                    for (int y = 0; y < pool.Count; y++)
                    {
                        pool.MoveNext();
                        int        py = pool.Current;
                        List <int> tl = new List <int>();
                        tl.AddRange(lx);
                        tl.Add(py);
                        result.Add(tl);
                    }
                }
            }

            return(result.Select(tl => IndexFromCoordinates(tl.ToArray())).ToArray());
        }