Can you use List> to get around the 2gb object limit?

后端 未结 5 1119
逝去的感伤
逝去的感伤 2021-02-18 22:28

I\'m running up against the 2gb object limit in c# (this applies even in 64 bit for some annoying reason) with a large collection of structs (est. size of 4.2 gig in total).

5条回答
  •  半阙折子戏
    2021-02-18 23:30

    class HugeList
    {
        private const int PAGE_SIZE = 102400;
        private const int ALLOC_STEP = 1024;
    
        private T[][] _rowIndexes;
    
        private int _currentPage = -1;
        private int _nextItemIndex = PAGE_SIZE;
    
        private int _pageCount = 0;
        private int _itemCount = 0;
    
        #region Internals
    
        private void AddPage()
        {
            if (++_currentPage == _pageCount)
                ExtendPages();
    
            _rowIndexes[_currentPage] = new T[PAGE_SIZE];
            _nextItemIndex = 0;
        }
    
        private void ExtendPages()
        {
            if (_rowIndexes == null)
            {
                _rowIndexes = new T[ALLOC_STEP][];
            }
            else
            {
                T[][] rowIndexes = new T[_rowIndexes.Length + ALLOC_STEP][];
    
                Array.Copy(_rowIndexes, rowIndexes, _rowIndexes.Length);
    
                _rowIndexes = rowIndexes;
            }
    
            _pageCount = _rowIndexes.Length;
        }
    
        #endregion Internals
    
        #region Public
    
        public int Count
        {
            get { return _itemCount; }
        }
    
        public void Add(T item)
        {
            if (_nextItemIndex == PAGE_SIZE)
                AddPage();
    
            _itemCount++;
            _rowIndexes[_currentPage][_nextItemIndex++] = item;
        }
    
        public T this[int index]
        {
            get { return _rowIndexes[index / PAGE_SIZE][index % PAGE_SIZE]; }
            set { _rowIndexes[index / PAGE_SIZE][index % PAGE_SIZE] = value; }
        }
    
        #endregion Public
    }
    

提交回复
热议问题