c# - How to create large array of integers to test LongCount? -
i want allocate large array of integers test longcount operator. longcount operator used when, quote:
you expect result greater maxvalue.
so prepare test, want allocate array of integers little larger int32.maxvalue
:
int64[] arr = new int64[int32.maxvalue + 10ul];
but throws overflowexception
.
what want this:
int64[] arr = new int64[int32.maxvalue + 10ul]; var res = arr.longcount();
and expect res
2147483657
(which int32.maxvalue + 10
).
how can this?
you write own list implementation store several arrays , link them (or there's better 1 where..). map huge ulong
int 2 int32
indexes there, implement ienumerable
interface on it, , test away.
ulong listsize = int32.maxvalue + 10ul; biglist<bool> mylist = new biglist<bool>(listsize); debug.assert(mylist.longcount() == (long)listsize); console.readkey();
example implementation..
public class biglist<t> : ienumerable<t> { private list<t[]> _storage = new list<t[]>(); private const int _maxstoragearraysize = 1000; public ulong capacity { get; private set; } public biglist(ulong capacity) { _storage = new list<t[]>(); capacity = capacity; int arraysrequired = (int)math.ceiling((double)capacity / (double)_maxstoragearraysize); int lastarraysize = (int)(capacity % (ulong)_maxstoragearraysize); (int = 0; < arraysrequired; i++) _storage.add(new t[(i + 1) < arraysrequired ? _maxstoragearraysize : lastarraysize]); } public t this[ulong idx] { { int arrayidx = (int)(idx / (ulong)_maxstoragearraysize); int arrayoff = (int)(idx % (ulong)_maxstoragearraysize); return _storage[arrayidx][arrayoff]; } set { int arrayidx = (int)(idx / (ulong)_maxstoragearraysize); int arrayoff = (int)(idx % (ulong)_maxstoragearraysize); _storage[arrayidx][arrayoff] = value; } } public class biglistenumerator : ienumerator<t> { private biglist<t> _biglist; private ulong _idx; public biglistenumerator(biglist<t> biglist) { _biglist = biglist; } public t current { { return _biglist[_idx]; } } public void dispose() { _biglist = null; } object system.collections.ienumerator.current { { return current; } } public bool movenext() { return _idx++ < _biglist.capacity; } public void reset() { _idx = 0; } } public ienumerator<t> getenumerator() { return new biglistenumerator(this); } system.collections.ienumerator system.collections.ienumerable.getenumerator() { return new biglistenumerator(this); } }
Comments
Post a Comment