jt
2021-06-10 5d0d028456874576560552f5a5c4e8b801786f11
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/*******************************************************************************
 * You may amend and distribute as you like, but don't remove this header!
 *
 * EPPlus provides server-side generation of Excel 2007/2010 spreadsheets.
 * See http://www.codeplex.com/EPPlus for details.
 *
 * Copyright (C) 2011  Jan Källman
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
 * See the GNU Lesser General Public License for more details.
 *
 * The GNU Lesser General Public License can be viewed at http://www.opensource.org/licenses/lgpl-license.php
 * If you unfamiliar with this license or have questions about it, here is an http://www.gnu.org/licenses/gpl-faq.html
 *
 * All code and executables are provided "as is" with no warranty either express or implied. 
 * The author accepts no liability for any damage or loss of business that this product may cause.
 *
 * Code change notes:
 * 
 * Author                            Change                        Date
 * ******************************************************************************
 * Jan Källman            Added                       2010-02-04
 * Jan Källman            License changed GPL-->LGPL  2011-12-27
 *******************************************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using HH.WMS.Utils.EPPlus.Drawing.Vml;
namespace HH.WMS.Utils.EPPlus
{
    /// <summary>
    /// This is the store for all Rows, Columns and Cells.
    /// It is a Dictionary implementation that allows you to change the Key (the RowID, ColumnID or CellID )
    /// </summary>
    internal class RangeCollection : IEnumerator<IRangeID>, IEnumerable
    {
        private class IndexItem
        {
            internal IndexItem(ulong cellId)
            {
                RangeID = cellId;
            }
            internal IndexItem(ulong cellId, int listPointer)
            {
                RangeID = cellId;
                ListPointer=listPointer;
            }
            internal ulong RangeID;
            internal int ListPointer;
        }
        /// <summary>
        /// Compares an IndexItem
        /// </summary>
        internal class Compare : IComparer<IndexItem>
        {
            #region IComparer<IndexItem> Members
            int IComparer<IndexItem>.Compare(IndexItem x, IndexItem y)
            {
                return x.RangeID < y.RangeID ? -1 : x.RangeID > y.RangeID ? 1 : 0;
            }
 
            #endregion
        }
        IndexItem[] _cellIndex;
        List<IRangeID> _cells;
        Compare _comparer;
        /// <summary>
        /// Creates a new collection
        /// </summary>
        /// <param name="cells">The Cells. This list must be sorted</param>
        internal RangeCollection(List<IRangeID> cells)
        {   
            _cells = cells;
            _comparer = new Compare();
            InitSize(_cells);
            for (int i = 0; i < _cells.Count; i++)
            {
                _cellIndex[i] = new IndexItem(cells[i].RangeID, i);
            }
        }
        ~RangeCollection()
        {
            _cellIndex = null;
            _cells = null;
        }
        /// <summary>
        /// Return the item with the RangeID
        /// </summary>
        /// <param name="RangeID"></param>
        /// <returns></returns>
        internal IRangeID this[ulong RangeID]
        {
            get
            {
                return _cells[_cellIndex[IndexOf(RangeID)].ListPointer];
            }
        }
        /// <summary>
        /// Return specified index from the sorted list
        /// </summary>
        /// <param name="Index"></param>
        /// <returns></returns>
        internal IRangeID this[int Index]
        {
            get
            {
                return _cells[_cellIndex[Index].ListPointer];
            }
        }
        internal int Count
        {
            get
            {
                return _cells.Count;
            }
        }
        internal void Add(IRangeID cell)
        {
            var ix = IndexOf(cell.RangeID);
            if (ix >= 0)
            {
                throw (new Exception("Item already exists"));
            }
            Insert(~ix, cell);
        }
        internal void Delete(ulong key)
        {
            var ix = IndexOf(key);
            if (ix < 0)
            {
                throw (new Exception("Key does not exists"));
            }
            int listPointer = _cellIndex[ix].ListPointer;
            Array.Copy(_cellIndex, ix + 1, _cellIndex, ix, _cells.Count - ix - 1);
            _cells.RemoveAt(listPointer);
 
            //Item is removed subtract one from all items with greater ListPointer
            for (int i = 0; i < _cells.Count; i++)
            {
                if (_cellIndex[i].ListPointer >= listPointer)
                {
                    _cellIndex[i].ListPointer--;
                }
 
            }
        }
        internal int IndexOf(ulong key)
        {
            return Array.BinarySearch<IndexItem>(_cellIndex, 0, _cells.Count, new IndexItem(key), _comparer);
        }
        internal bool ContainsKey(ulong key)
        {
            return IndexOf(key) < 0 ? false : true;
        }
        int _size { get; set; }
        #region "RangeID manipulation methods"
        /// <summary>
        /// Insert a number of rows in the collecion but dont update the cell only the index
        /// </summary>
        /// <param name="rowID"></param>
        /// <param name="rows"></param>
        /// <returns>Index of first rangeItem</returns>
        internal int InsertRowsUpdateIndex(ulong rowID, int rows)
        {
            int index = IndexOf(rowID);
            if (index < 0) index = ~index; //No match found invert to get start cell
            ulong rowAdd = (((ulong)rows) << 29);
            for (int i = index; i < _cells.Count; i++)
            {
                _cellIndex[i].RangeID += rowAdd;
            }
            return index;
        }
        /// <summary>
        /// Insert a number of rows in the collecion
        /// </summary>
        /// <param name="rowID"></param>
        /// <param name="rows"></param>
        /// <returns>Index of first rangeItem</returns>
        internal int InsertRows(ulong rowID, int rows)
        {
            int index = IndexOf(rowID);
            if (index < 0) index = ~index; //No match found invert to get start cell
            ulong rowAdd=(((ulong)rows) << 29);
            for (int i = index; i < _cells.Count; i++)
            {
                _cellIndex[i].RangeID += rowAdd;
                _cells[_cellIndex[i].ListPointer].RangeID += rowAdd;
            }
            return index;
        }
        /// <summary>
        /// Delete rows from the collecion
        /// </summary>
        /// <param name="rowID"></param>
        /// <param name="rows"></param>
        /// <param name="updateCells">Update range id's on cells</param>
        internal int DeleteRows(ulong rowID, int rows, bool updateCells)
        {
            ulong rowAdd = (((ulong)rows) << 29);
            var index = IndexOf(rowID);
            if (index < 0) index = ~index; //No match found invert to get start cell
 
            if (index >= _cells.Count || _cellIndex[index] == null) return -1;   //No row above this row
            while (index < _cells.Count && _cellIndex[index].RangeID < rowID + rowAdd)
            {
                Delete(_cellIndex[index].RangeID);
            }
 
            int updIndex = IndexOf(rowID + rowAdd);
            if (updIndex < 0) updIndex = ~updIndex; //No match found invert to get start cell
 
            for (int i = updIndex; i < _cells.Count; i++)
            {
                _cellIndex[i].RangeID -= rowAdd;                        //Change the index
                if (updateCells) _cells[_cellIndex[i].ListPointer].RangeID -= rowAdd;    //Change the cell/row or column object
            }
            return index;
        }
        internal void InsertColumn(ulong ColumnID, int columns)
        {
            throw (new Exception("Working on it..."));
        }
        internal void DeleteColumn(ulong ColumnID,int columns)
        {
            throw (new Exception("Working on it..."));
        }
        #endregion
        #region "Private Methods"
        /// <summary>
        /// Init the size starting from 128 items. Double the size until the list fits.
        /// </summary>
        /// <param name="_cells"></param>
        private void InitSize(List<IRangeID> _cells)
        {
            _size = 128;
            while (_cells.Count > _size) _size <<= 1;
            _cellIndex = new IndexItem[_size];
        }
        /// <summary>
        /// Check the size and double the size if out of bound
        /// </summary>
        private void CheckSize()
        {
            if (_cells.Count >= _size)
            {
                _size <<= 1;
                Array.Resize(ref _cellIndex, _size);
            }
        }
        private void Insert(int ix, IRangeID cell)
        {
            CheckSize();
            Array.Copy(_cellIndex, ix, _cellIndex, ix + 1, _cells.Count - ix);
            _cellIndex[ix] = new IndexItem(cell.RangeID, _cells.Count);
            _cells.Add(cell);
        }
        #endregion
 
        #region IEnumerator<IRangeID> Members
 
        IRangeID IEnumerator<IRangeID>.Current
        {
            get { throw new NotImplementedException(); }
        }
 
        #endregion
 
        #region IDisposable for the enumerator Members
 
        void IDisposable.Dispose()
        {
            _ix = -1;
        }
 
        #endregion
 
        #region IEnumerator Members
        int _ix = -1;
        object IEnumerator.Current
        {
            get 
            {
                return _cells[_cellIndex[_ix].ListPointer];
            }
        }
 
        bool IEnumerator.MoveNext()
        {
           _ix++;
           return _ix < _cells.Count;
        }
 
        void IEnumerator.Reset()
        {
            _ix = -1;
        }
 
        #endregion
 
        #region IEnumerable Members
 
        IEnumerator IEnumerable.GetEnumerator()
        {
            return this;
        }
 
        #endregion
    }
}