SysDragon
SysDragon

Reputation: 9888

Store a number with no limit (infinite) in .NET Languages

Suppose I want to store a unlimited number of elements in a class, as Excel do with the number of Sheets (limited by the memory of the computer).

Which is the way or the number type to hold the index used to get the elements?

Upvotes: 3

Views: 6240

Answers (7)

Thomas
Thomas

Reputation: 5921

Based on the answer of Tim Schmelter :

public class BigList<T>
{
    public Object[] internalArray = new Object[Int16.MaxValue];

    public T this[BigInteger i]
    {
        get
        {
            if (i < Int16.MaxValue-1)
            {
                return (T)internalArray[(int)i];
            }
            else
            {
                i = i - int.MaxValue;
                BigList<T> following = (BigList<T>)internalArray[Int16.MaxValue-1];
                if (following != null)
                {
                    return following[i];
                }
                else
                {
                    throw new KeyNotFoundException();
                }
            }
        }
        set
        {
            if (i < Int16.MaxValue-1)
            {
                internalArray[(int)i] = value;
            }
            else
            {
                i = i - int.MaxValue;
                BigList<T> following = (BigList<T>)internalArray[Int16.MaxValue-1];
                if (following == null)
                {
                    following = new BigList<T>();
                    internalArray[Int16.MaxValue - 1] = following;
                }
                following[i] = value;
            }
        }
    }
}

And a start of unit test :

[Test]
public void BigListTest()
{
    BigList<string> test = new BigList<string>();
    var bigIndex = new BigInteger(int.MaxValue);
    string value = "test";
    bigIndex *= 2;
    test[bigIndex] = value;
    Assert.AreEqual(test[bigIndex],value);
}

Upvotes: 0

Reacher Gilt
Reacher Gilt

Reputation: 1813

Tim-Schmelter and Joseph Lee have good answers for you. But consider that they're good answers for maximum value. Think about the details of implementation, though: If your data structure is just one byte, even a ULong could index more than an exabyte's worth of before considering any other factors (data structures, etc).

Upvotes: 1

Sam Harwell
Sam Harwell

Reputation: 99869

For indexing a collection of items limited only by the available system memory, you can probably use a long. The range is enough to uniquely address the individual bits of a storage bank nearly 150x the size of TACC Stampede's 14PB of drive space (or more than 10000x the size of its 207TB bank of main memory).

Upvotes: 0

Robert Harvey
Robert Harvey

Reputation: 180798

If you want to roll your own collection, you can use an Indexer. The following trivial example can handle 100 elements, but you can extend it to as many elements as you need, using a List or some other mechanism:

class SampleCollection<T>
{
    // Declare an array to store the data elements. 
    private T[] arr = new T[100];

    // Define the indexer, which will allow client code 
    // to use [] notation on the class instance itself. 
    public T this[int i]
    {
        get
        {
            // This indexer is very simple, and just returns or sets 
            // the corresponding element from the internal array. 
            return arr[i];
        }
        set
        {
            arr[i] = value;
        }
    }
}

This class shows how client code uses the indexer:

class Program
{
    static void Main(string[] args)
    {
        // Declare an instance of the SampleCollection type.
        SampleCollection<string> stringCollection = new SampleCollection<string>();

        // Use [] notation on the type.
        stringCollection[0] = "Hello, World";
        System.Console.WriteLine(stringCollection[0]);
    }
}
// Output: 
// Hello, World.

Using an int as the indexer parameter will give you access to roughly two billion discrete elements. If you need more than that, you'll have to use long or an arbitrary-precision type like BigInt as the indexer parameter.

Upvotes: 1

Tim Schmelter
Tim Schmelter

Reputation: 460138

If you want to store an arbitrarily large integer you can use BigInteger.

public BigInteger Index{ get; set; }

Note that you have to add a reference to the System.Numerics dll first.

It differs from other integral types in the .NET Framework which have a range indicated by their MinValue and MaxValue properties.

Because it has no upper or lower bounds an OutOfMemoryException can be thrown for any operation that causes a BigInteger value to grow too large.

Upvotes: 6

Lanorkin
Lanorkin

Reputation: 7504

In .Net there is a 2Gb limit for any particular object. This means that, for example, byte[] array index can not be larger than int.MaxValue - and actually it is even smaller.

So you can easily use Int32 == int for any index, OR you should think a lot about your data structures.

Upvotes: 0

jlee88my
jlee88my

Reputation: 3043

Any numeric-type variable is finite. The largest that I know of is ULong (0 through 18,446,744,073,709,551,615). If you have more elements than this, you're in trouble...

Upvotes: 0

Related Questions