summaryrefslogtreecommitdiff
path: root/src/NCollection/NCollection_SparseArrayBase.cxx
blob: 90e6cd5159a7ea4ad534a559f5191a14a7dc94ef (plain)
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
// File:      NCollection_SparseArray.cxx
// Created:   Tue Feb  6 18:54:25 2007
// Author:    Andrey BETENEV
// Copyright: Open Cascade 2007

#include <NCollection_SparseArrayBase.hxx>
#include <Standard_ProgramError.hxx>

//=======================================================================
//function : allocData
//purpose  : 
//=======================================================================

void NCollection_SparseArrayBase::allocData (const Standard_Size iBlock)
{
  if ( iBlock < myNbBlocks )
    return;

  // the allocation of blocks starts from myBlockSize items
  // and then is multiplied by 2 every time reallocation is needed
  Standard_Size newNbBlocks = ( myNbBlocks ? myNbBlocks * 2 : myBlockSize );
  while (iBlock >= newNbBlocks) newNbBlocks *= 2;

  Standard_Address* newData = 
    (Standard_Address*)malloc(newNbBlocks*sizeof(Standard_Address));
  if ( myNbBlocks >0 )
    memcpy (newData, myData, myNbBlocks*sizeof(Standard_Address));
  memset (newData+myNbBlocks, 0, (newNbBlocks-myNbBlocks)*sizeof(Standard_Address));

  free (myData);
  myData = newData;
  myNbBlocks = newNbBlocks;
}

//=======================================================================
//function : freeBlock
//purpose  : 
//=======================================================================

void NCollection_SparseArrayBase::freeBlock (const Standard_Size iBlock)
{
  Standard_Address & anAddr = myData[iBlock];
  Block aBlock = getBlock(anAddr);
  for (Standard_Size anInd=0; anInd < myBlockSize; anInd++)
    if ( aBlock.IsSet(anInd) )
    {
      destroyItem (getItem (aBlock, anInd));
      mySize--;
    }
  free (anAddr);
  anAddr = 0;
}

//=======================================================================
//function : Clear
//purpose  : 
//=======================================================================

void NCollection_SparseArrayBase::Clear ()
{
  // free block data
  for (Standard_Size iBlock=0; iBlock < myNbBlocks; iBlock++)
    if ( myData[iBlock] )
      freeBlock (iBlock);
  
  // free blocks and reset counters
  free (myData);
  myData = 0;
  myNbBlocks = 0;
  
  // consistency check
  Standard_ProgramError_Raise_if (mySize!=0,"NCollection_SparseArrayBase: Implementation error: inconsistent items count")
}

//=======================================================================
//function : assign
//purpose  : 
//=======================================================================

void NCollection_SparseArrayBase::assign (const NCollection_SparseArrayBase& theOther)
{
  if (this == &theOther) 
    return;

  // if block size is different, clear all data
  if ( myBlockSize != theOther.myBlockSize )
    Clear();
  myBlockSize = theOther.myBlockSize;

  // iterate by blocks in theOther
  Standard_Size iBlock=0;
  for (; iBlock < theOther.myNbBlocks; iBlock++)
  {
    if ( ! theOther.myData[iBlock] )
    {
      // if other block is empty, just make sure to empty that block in "this"
      if ( iBlock < myNbBlocks && myData[iBlock] )
	freeBlock (iBlock);
      continue;
    }

    if ( iBlock >= myNbBlocks )
      allocData(iBlock);
    Block anOtherBlock = getBlock(theOther.myData[iBlock]);

    // if block not yet allocated, just allocate and fill
    Standard_Address & anAddr = myData[iBlock];
    if ( ! anAddr ) 
    {
      anAddr = calloc (Block::Size(myBlockSize, myItemSize), sizeof(char));
      Block aBlock ( getBlock(anAddr) );
      for (Standard_Size anInd=0; anInd < myBlockSize; anInd++)
	if ( anOtherBlock.IsSet(anInd) )
	{
	  Standard_Address anItem = getItem (aBlock, anInd);
	  aBlock.Set(anInd);
	  (*aBlock.Count)++;
	  mySize++;
	  createItem (anItem, getItem(anOtherBlock, anInd));
	}
    }
    // else perform copying item-by-item
    else 
    {
      Block aBlock ( getBlock(anAddr) );
      for (Standard_Size anInd=0; anInd < myBlockSize; anInd++)
      {
	Standard_Address anItem = getItem (aBlock, anInd);
	if ( anOtherBlock.IsSet(anInd) )
	{
	  Standard_Address anOtherItem = getItem (anOtherBlock, anInd);
	  if ( aBlock.IsSet(anInd) ) // copy
	  {
	    copyItem (anItem, anOtherItem);
	  }
	  else // create
	  {
	    aBlock.Set(anInd);
	    (*aBlock.Count)++;
	    mySize++;
	    createItem (anItem, getItem(anOtherBlock, anInd));
	  }
	}
	else if ( aBlock.IsSet(anInd) ) // delete 
	{
	  aBlock.Set(anInd);
	  (*aBlock.Count)--;
	  mySize--;
	  destroyItem (anItem);
	}
      }
    }
  }

  // clear any remaining blocks in this
  for (; iBlock < myNbBlocks; iBlock++)
    if ( myData[iBlock] )
      freeBlock (iBlock);
  
  // consistency check
  Standard_ProgramError_Raise_if (mySize!=theOther.mySize,
				 "NCollection_SparseArrayBase: Implementation error: inconsistent items count")
}

//=======================================================================
//function : exchange
//purpose  : 
//=======================================================================

template<class T> static inline void sswap (T &a, T &b) { T c = a; a = b; b = c; }

void NCollection_SparseArrayBase::exchange (NCollection_SparseArrayBase& theOther)
{
  if (this == &theOther) 
    return;

  // swap fields of this and theOther
  sswap (myItemSize, theOther.myItemSize);
  sswap (myBlockSize,theOther.myBlockSize);
  sswap (myNbBlocks, theOther.myNbBlocks);
  sswap (mySize,     theOther.mySize);
  sswap (myData,     theOther.myData);
}

//=======================================================================
//function : setValue
//purpose  : 
//=======================================================================

Standard_Address NCollection_SparseArrayBase::setValue (const Standard_Integer theIndex,
							const Standard_Address theValue) 
{
  Standard_OutOfRange_Raise_if (theIndex<0,"NCollection_SparseArray::SetValue()")
  Standard_Size anIndex = (Standard_Size)theIndex;
  Standard_Size iBlock = anIndex / myBlockSize;
    
  // resize blocks array if necessary
  if ( iBlock >= myNbBlocks )
    allocData (iBlock);

  // allocate block if necessary
  Standard_Address & anAddr = myData[iBlock];
  if ( ! anAddr )
    anAddr = calloc (Block::Size(myBlockSize, myItemSize), sizeof(char));

  // get a block
  Block aBlock (getBlock (anAddr));

  // mark item as defined 
  Standard_Size anInd = anIndex % myBlockSize;
  Standard_Address anItem = getItem (aBlock, anInd);

  // either create an item by copy constructor if it is new, or assign it
  if ( aBlock.Set(anInd) )
  {
    (*aBlock.Count)++;
    mySize++;
    createItem (anItem, theValue);
  }
  else
    copyItem (anItem, theValue);
    
  return anItem;
}

//=======================================================================
//function : HasValue
//purpose  : 
//=======================================================================

Standard_Boolean NCollection_SparseArrayBase::HasValue (const Standard_Integer theIndex) const
{
  Standard_Size anIndex = (Standard_Size)theIndex;
  Standard_Size iBlock = anIndex / myBlockSize;
  if ( theIndex < 0 || iBlock >= myNbBlocks ||
       ! myData[iBlock] )
    return Standard_False;
  return getBlock(myData[iBlock]).IsSet(anIndex % myBlockSize) ? Standard_True : Standard_False;
}

//=======================================================================
//function : UnsetValue
//purpose  : 
//=======================================================================

Standard_Boolean NCollection_SparseArrayBase::UnsetValue (const Standard_Integer theIndex)
{
  // check that the item is defined
  Standard_Size anIndex = (Standard_Size)theIndex;
  Standard_Size iBlock = anIndex / myBlockSize;
  if ( theIndex < 0 || iBlock >= myNbBlocks || ! myData[iBlock] )
    return Standard_False;

  Block aBlock (getBlock(myData[iBlock]));
  Standard_Size anInd = anIndex % myBlockSize;
  if ( ! aBlock.Unset(anInd) )
    return Standard_False;

  // destroy the item
  destroyItem (getItem (aBlock, anInd));
  (*aBlock.Count)--;
  mySize--;

  // free block if it becomes empty
  if ( ! (*aBlock.Count) )
    freeBlock (iBlock);

  return Standard_True;
}

//=======================================================================
//function : Iterator::Iterator
//purpose  : 
//=======================================================================

NCollection_SparseArrayBase::Iterator::Iterator (const NCollection_SparseArrayBase* theArray)
: myArr((NCollection_SparseArrayBase*)theArray),
  myHasMore(Standard_False), myIBlock(0), myInd(0), 
  myBlock(0,0,0)
{
  init(theArray);
}

//=======================================================================
//function : Iterator::Next
//purpose  : 
//=======================================================================

void NCollection_SparseArrayBase::Iterator::Next ()
{
  if ( ! myArr || ! myHasMore )
    return;

  // iterate by items and blocks  
  for ( myInd++; ; myInd++ ) {
    // if index is over the block size, advance to the next non-empty block
    if ( myInd >= myArr->myBlockSize )
    {
      for ( myIBlock++; ; myIBlock++ ) {
	if ( myIBlock >= myArr->myNbBlocks ) // end
	{
	  myHasMore = Standard_False;
	  return;
	}
	if ( myArr->myData[myIBlock] )
	{
	  myInd = 0;
	  myBlock = Block (myArr->myData[myIBlock], myArr->myBlockSize, myArr->myItemSize );
	  break;
	}
      }
    }
    // check if item is defined
    if ( myBlock.IsSet (myInd) )
      return;
  }
}

//=======================================================================
//function : Iterator::init
//purpose  : 
//=======================================================================

void NCollection_SparseArrayBase::Iterator::init (const NCollection_SparseArrayBase* theArray)
{
  myArr = (NCollection_SparseArrayBase*)theArray;
  myHasMore = Standard_False;
  if ( myArr ) 
  {
    myInd = 0;
    // find first non-empty block
    for ( myIBlock=0; myIBlock < myArr->myNbBlocks; myIBlock++ )
    {
      if ( ! myArr->myData[myIBlock] ) 
	continue;
      myHasMore = Standard_True;
      myBlock = Block (myArr->myData[myIBlock], myArr->myBlockSize, myArr->myItemSize );
      // if first item in the block is not set, advance to the next defined item
      if ( ! myBlock.IsSet(myInd) )
	Next();
      return;
    }
  }
}