Counting the distribution of the values in an array. : Array Util « Collections Data Structure « C# / C Sharp






Counting the distribution of the values in an array.

  
   
//
// (C) Copyright 2009 Irantha Suwandarathna (irantha@gmail.com)
// All rights reserved.
//

/* Copyright (c) 2001-2008, The HSQL Development Group
* All rights reserved.
*
* Redistribution and use _in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions _in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer _in the documentation
* and/or other materials provided with the distribution.
*
* Neither the name of the HSQL Development Group nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EffiProz.Core.Lib
{
    /**
   * Collection of routines for counting the distribution of the values
   * _in an int[] array.
   *
   * @author fredt@users
   * @version 1.7.2
   * @since 1.7.2
   */
    public class ArrayCounter
    {

        /**
         * Returns an int[] array of length segments containing the distribution
         * count of the elements _in unsorted int[] array with values between min
         * and max (range). Values outside the min-max range are ignored<p>
         *
         * A usage example is determining the count of people of each age group
         * _in a large int[] array containing the age of each person. Called with
         * (array, 16,0,79), it will return an int[16] with the first element
         * the count of people aged 0-4, the second element the count of those
         * aged 5-9, and so on. People above the age of 79 are excluded. If the
         * range is not a multiple of segments, the last segment will be cover a
         * smaller sub-range than the rest.
         *
         */
        public static int[] countSegments(int[] array, int elements,
                                          int segments, int start, int limit)
        {

            int[] counts = new int[segments];
            long interval = calcInterval(segments, start, limit);
            int index = 0;
            int element = 0;

            if (interval <= 0)
            {
                return counts;
            }

            for (int i = 0; i < elements; i++)
            {
                element = array[i];

                if (element < start || element >= limit)
                {
                    continue;
                }

                index = (int)((element - start) / interval);

                counts[index]++;
            }

            return counts;
        }

        /**
         * With an unsorted int[] array and with target a positive integer _in the
         * range (1,array.Length), finds the value _in the range (start,limit) of the
         * largest element (rank) where the count of all smaller elements _in that
         * range is less than or equals target. Parameter margin indicates the
         * margin of error _in target<p>
         *
         * In statistics, this can be used to calculate a median or quadrile value.
         * A usage example applied to an array of age values is to determine
         * the maximum age of a given number of people. With the example array
         * given _in countSegments, rank(array, c, 6000, 18, 65, 0) will return an age
         * value between 18-64 (inclusive) and the count of all people aged between
         * 18 and the returned value(exclusive) will be less than or equal 6000.
         *
         */
        public static int rank(int[] array, int elements, int target, int start,
                               int limit, int margin)
        {

            const int segments = 256;
            int elementCount = 0;
            int currentLimit = limit;

            for (; ; )
            {
                long interval = calcInterval(segments, start, currentLimit);
                int[] counts = countSegments(array, elements, segments, start,
                                             currentLimit);

                for (int i = 0; i < counts.Length; i++)
                {
                    if (elementCount + counts[i] < target)
                    {
                        elementCount += counts[i];
                        start += (int)interval;
                    }
                    else
                    {
                        break;
                    }
                }

                if (elementCount + margin >= target)
                {
                    return start;
                }

                if (interval <= 1)
                {
                    return start;
                }

                currentLimit = start + interval < limit ? (int)(start + interval)
                                                        : limit;
            }
        }

        /**
         * Helper method to calculate the span of the sub-interval. Simply returns
         * the cieling of ((limit - start) / segments) and accounts for invalid
         * start and limit combinations.
         */
        static long calcInterval(int segments, int start, int limit)
        {

            long range = limit - start;

            if (range < 0)
            {
                return 0;
            }

            int partSegment = (range % segments) == 0 ? 0
                                                      : 1;

            return (range / segments) + partSegment;
        }
    }
}

   
    
  








Related examples in the same category

1.Return the average of the given values
2.Return the percentage of a given value.
3.return the min value in the list of double
4.return the max value in the list of double
5.Returns a string representation of the sbyte array
6.Returns a string representation of the char array
7.Returns a string representation of the double array
8.Returns a string representation of the float array
9.Returns a string representation of the int array
10.Returns a string representation of the long array
11.Returns a string representation of the object array
12.Returns a string representation of the short value array
13.Returns a string representation of the bool value array
14.Ensures that the sbyte array cannot hold more than maxCapacity elements.
15.Ensures that the char array cannot hold more than maxCapacity elements.
16.Ensures that the double array cannot hold more than maxCapacity elements.
17.Ensures that the float array cannot hold more than maxCapacity elements.
18.Ensures that the int array cannot hold more than maxCapacity elements.
19.Ensures that the long array cannot hold more than maxCapacity elements.
20.Ensures that the object array cannot hold more than maxCapacity elements.
21.Ensures that the short array cannot hold more than maxCapacity elements.
22.Ensures that the bool array cannot hold more than maxCapacity elements.
23.Copy an array into another array.
24.Swap two elements in a array
25.Generic Growable Array
26.foreach is used to display the contents of an array of integers.
27.Are two arrays equal.
28.Are thos two arrays having the save contents
29.Convert object array to string
30.Get array size
31.Convert array content to generic type array
32.Searches for the specified object and returns the index of the first occurrence within the entire one-dimensional System.Array.
33.Creates a new array with just the specified elements.
34.Array 2D
35.Get a array of object from a enum datatype.
36.Reinitializes an int array to the given value in an optimized way: intArraySet
37.Returns a Boolean indicating whether the Array is Empty (is Null or has a length of zero).
38.Places elements from an enumerable into an array.
39.Bit Array 2D
40.Collection of static methods for operations on arrays
41.Char Array Writer
42.Computes the indices
43.Compare Array
44.Remove element from Array
45.Concatenate Array
46.Get the array slice between the two indexes
47.Find subarray in the source array.
48.Check if the array contains needle on specified position