Ensures that a given array can hold up to minCapacity elements. : String Array « Data Types « C# / C Sharp






Ensures that a given array can hold up to minCapacity elements.

    

/*
 Copyright 1999 CERN - European Organization for Nuclear Research.
 Permission to use, copy, modify, distribute and sell this software and its documentation for any purpose 
 is hereby granted without fee, provided that the above copyright notice appear in all copies and 
 that both that copyright notice and this permission notice appear in supporting documentation. 
 CERN makes no representations about the suitability of this software for any purpose. 
 It is provided "as is" without expressed or implied warranty.
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace DiscoveryLogic.Common.Numeric
{
    public class Arrays : System.Object
    {
        /// <summary> Ensures that a given array can hold up to <tt>minCapacity</tt> elements.
        /// 
        /// Returns the identical array if it can hold at least the number of elements specified.
        /// Otherwise, returns a new array with increased capacity containing the same elements, ensuring  
        /// that it can hold at least the number of elements specified by 
        /// the minimum capacity argument.
        /// 
        /// </summary>
        /// <param name="minCapacity">  the desired minimum capacity.
        /// </param>
        public static sbyte[] ensureCapacity(sbyte[] array, int minCapacity)
        {
            int oldCapacity = array.Length;
            sbyte[] newArray;
            if (minCapacity > oldCapacity)
            {
                int newCapacity = (oldCapacity * 3) / 2 + 1;
                if (newCapacity < minCapacity)
                {
                    newCapacity = minCapacity;
                }

                newArray = new sbyte[newCapacity];
                Array.Copy(array, 0, newArray, 0, oldCapacity);
            }
            else
            {
                newArray = array;
            }
            return newArray;
        }    

        /// <summary> Ensures that a given array can hold up to <tt>minCapacity</tt> elements.
        /// 
        /// Returns the identical array if it can hold at least the number of elements specified.
        /// Otherwise, returns a new array with increased capacity containing the same elements, ensuring  
        /// that it can hold at least the number of elements specified by 
        /// the minimum capacity argument.
        /// 
        /// </summary>
        /// <param name="minCapacity">  the desired minimum capacity.
        /// </param>
        public static short[] ensureCapacity(short[] array, int minCapacity)
        {
            int oldCapacity = array.Length;
            short[] newArray;
            if (minCapacity > oldCapacity)
            {
                int newCapacity = (oldCapacity * 3) / 2 + 1;
                if (newCapacity < minCapacity)
                {
                    newCapacity = minCapacity;
                }

                newArray = new short[newCapacity];
                Array.Copy(array, 0, newArray, 0, oldCapacity);
            }
            else
            {
                newArray = array;
            }
            return newArray;
        }
        /// <summary> Ensures that a given array can hold up to <tt>minCapacity</tt> elements.
        /// 
        /// Returns the identical array if it can hold at least the number of elements specified.
        /// Otherwise, returns a new array with increased capacity containing the same elements, ensuring  
        /// that it can hold at least the number of elements specified by 
        /// the minimum capacity argument.
        /// 
        /// </summary>
        /// <param name="minCapacity">  the desired minimum capacity.
        /// </param>
        public static bool[] ensureCapacity(bool[] array, int minCapacity)
        {
            int oldCapacity = array.Length;
            bool[] newArray;
            if (minCapacity > oldCapacity)
            {
                int newCapacity = (oldCapacity * 3) / 2 + 1;
                if (newCapacity < minCapacity)
                {
                    newCapacity = minCapacity;
                }

                newArray = new bool[newCapacity];
                Array.Copy(array, 0, newArray, 0, oldCapacity);
            }
            else
            {
                newArray = array;
            }
            return newArray;
        }    
        /// <summary> Ensures that a given array can hold up to <tt>minCapacity</tt> elements.
        /// 
        /// Returns the identical array if it can hold at least the number of elements specified.
        /// Otherwise, returns a new array with increased capacity containing the same elements, ensuring  
        /// that it can hold at least the number of elements specified by 
        /// the minimum capacity argument.
        /// 
        /// </summary>
        /// <param name="minCapacity">  the desired minimum capacity.
        /// </param>
        public static char[] ensureCapacity(char[] array, int minCapacity)
        {
            int oldCapacity = array.Length;
            char[] newArray;
            if (minCapacity > oldCapacity)
            {
                int newCapacity = (oldCapacity * 3) / 2 + 1;
                if (newCapacity < minCapacity)
                {
                    newCapacity = minCapacity;
                }

                newArray = new char[newCapacity];
                Array.Copy(array, 0, newArray, 0, oldCapacity);
            }
            else
            {
                newArray = array;
            }
            return newArray;
        }

        /// <summary> Ensures that a given array can hold up to <tt>minCapacity</tt> elements.
        /// 
        /// Returns the identical array if it can hold at least the number of elements specified.
        /// Otherwise, returns a new array with increased capacity containing the same elements, ensuring  
        /// that it can hold at least the number of elements specified by 
        /// the minimum capacity argument.
        /// 
        /// </summary>
        /// <param name="minCapacity">  the desired minimum capacity.
        /// </param>
        public static double[] ensureCapacity(double[] array, int minCapacity)
        {
            int oldCapacity = array.Length;
            double[] newArray;
            if (minCapacity > oldCapacity)
            {
                int newCapacity = (oldCapacity * 3) / 2 + 1;
                if (newCapacity < minCapacity)
                {
                    newCapacity = minCapacity;
                }

                newArray = new double[newCapacity];
                //for (int i = oldCapacity; --i >= 0; ) newArray[i] = array[i];
                Array.Copy(array, 0, newArray, 0, oldCapacity);
            }
            else
            {
                newArray = array;
            }
            return newArray;
        }        

        /// <summary> Ensures that a given array can hold up to <tt>minCapacity</tt> elements.
        /// 
        /// Returns the identical array if it can hold at least the number of elements specified.
        /// Otherwise, returns a new array with increased capacity containing the same elements, ensuring  
        /// that it can hold at least the number of elements specified by 
        /// the minimum capacity argument.
        /// 
        /// </summary>
        /// <param name="minCapacity">  the desired minimum capacity.
        /// </param>
        public static float[] ensureCapacity(float[] array, int minCapacity)
        {
            int oldCapacity = array.Length;
            float[] newArray;
            if (minCapacity > oldCapacity)
            {
                int newCapacity = (oldCapacity * 3) / 2 + 1;
                if (newCapacity < minCapacity)
                {
                    newCapacity = minCapacity;
                }

                newArray = new float[newCapacity];
                Array.Copy(array, 0, newArray, 0, oldCapacity);
            }
            else
            {
                newArray = array;
            }
            return newArray;
        }
        /// <summary> Ensures that a given array can hold up to <tt>minCapacity</tt> elements.
        /// 
        /// Returns the identical array if it can hold at least the number of elements specified.
        /// Otherwise, returns a new array with increased capacity containing the same elements, ensuring  
        /// that it can hold at least the number of elements specified by 
        /// the minimum capacity argument.
        /// 
        /// </summary>
        /// <param name="minCapacity">  the desired minimum capacity.
        /// </param>
        public static int[] ensureCapacity(int[] array, int minCapacity)
        {
            int oldCapacity = array.Length;
            int[] newArray;
            if (minCapacity > oldCapacity)
            {
                int newCapacity = (oldCapacity * 3) / 2 + 1;
                if (newCapacity < minCapacity)
                {
                    newCapacity = minCapacity;
                }

                newArray = new int[newCapacity];
                Array.Copy(array, 0, newArray, 0, oldCapacity);
            }
            else
            {
                newArray = array;
            }
            return newArray;
        }        


        /// <summary> Ensures that a given array can hold up to <tt>minCapacity</tt> elements.
        /// 
        /// Returns the identical array if it can hold at least the number of elements specified.
        /// Otherwise, returns a new array with increased capacity containing the same elements, ensuring  
        /// that it can hold at least the number of elements specified by 
        /// the minimum capacity argument.
        /// 
        /// </summary>
        /// <param name="minCapacity">  the desired minimum capacity.
        /// </param>
        public static long[] ensureCapacity(long[] array, int minCapacity)
        {
            int oldCapacity = array.Length;
            long[] newArray;
            if (minCapacity > oldCapacity)
            {
                int newCapacity = (oldCapacity * 3) / 2 + 1;
                if (newCapacity < minCapacity)
                {
                    newCapacity = minCapacity;
                }

                newArray = new long[newCapacity];
                Array.Copy(array, 0, newArray, 0, oldCapacity);
            }
            else
            {
                newArray = array;
            }
            return newArray;
        }            

        /// <summary> Ensures that a given array can hold up to <tt>minCapacity</tt> elements.
        /// 
        /// Returns the identical array if it can hold at least the number of elements specified.
        /// Otherwise, returns a new array with increased capacity containing the same elements, ensuring  
        /// that it can hold at least the number of elements specified by 
        /// the minimum capacity argument.
        /// 
        /// </summary>
        /// <param name="minCapacity">  the desired minimum capacity.
        /// </param>
        public static System.Object[] ensureCapacity(System.Object[] array, int minCapacity)
        {
            int oldCapacity = array.Length;
            System.Object[] newArray;
            if (minCapacity > oldCapacity)
            {
                int newCapacity = (oldCapacity * 3) / 2 + 1;
                if (newCapacity < minCapacity)
                {
                    newCapacity = minCapacity;
                }

                newArray = new System.Object[newCapacity];
                Array.Copy(array, 0, newArray, 0, oldCapacity);
            }
            else
            {
                newArray = array;
            }
            return newArray;
        }
    }
}

   
    
    
    
  








Related examples in the same category

1.Generates a hashcode for the string array
2.removes the specified strings in the string array from the input string
3.Count how many times a word appears in an array of words.
4.Find all unique words in an array of words.
5.Gets an array of sentences from a string.
6.Array To New Line Separated String
7.New Line Separated String To Array
8.returns the elements of the array as a string, delimited with the default delimitor
9.Strings to byte array.
10.String Array To String
11.Processes a string and returns the arguments in an array.
12.Demonstrate string arraysDemonstrate string arrays
13.String To Char ArrayString To Char Array
14.Comparing string to char array