summaryrefslogtreecommitdiff
path: root/inc/PCollection_HIndexedDataMap.gxx
blob: e43afd2c1115a30e8893609088044f5ce20db258 (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
//-Copyright:	 Matra Datavision 1992
//-Version: 

//-History:
//  Version	Date         Purpose
//              14/12/92     Creation   

//-Language     C++2.0

//-Declarations
#include <Standard_OutOfRange.hxx>

//=======================================================================
// Function : PCollection_HIndexedDataMap
// Purpose  : 
//=======================================================================

PCollection_HIndexedDataMap::PCollection_HIndexedDataMap
  (const Standard_Integer NbBuckets, const KeyHash &fhKey) 
{
  myNumber = 0;
  myArrayKey = new PCollection_ArrayIndexedDataMap(1,NbBuckets);
  myArrayIndices = new PCollection_ArrayIndexedDataMap(0,NbBuckets-1);
  myKeyHash = fhKey;
}

//=======================================================================
// Function : NbBuckets
// Purpose  : 
//=======================================================================

Standard_Integer PCollection_HIndexedDataMap::NbBuckets() const
{
  return myArrayKey->Length();
}

//=======================================================================
// Function : NbKeys
// Purpose  : 
//=======================================================================

Standard_Integer PCollection_HIndexedDataMap::NbKeys() const
{
  return myNumber;
}

//=======================================================================
// Function : Bind
// Purpose  : 
//=======================================================================

Standard_Integer PCollection_HIndexedDataMap::Bind(const Key& aKey,
				   const Item& anItem,
				   const Standard_Boolean OverWrite)
{
  Standard_Integer theSize = myArrayKey->Length();

  Standard_Integer hcode = myKeyHash.HashCode(aKey,theSize);
  Handle(PCollection_IndexedDataMapNode) theNode = myArrayKey->Value(hcode);
  while (! theNode.IsNull()) {
    if ( myKeyHash.Compare(theNode->GetKey(),aKey)) {
      // if the Key already exists, update the item and  return the index
      if (OverWrite) {
	theNode->SetItem(anItem);
      }
      return theNode->Index();
    }
   else {
      theNode = theNode->NextKey();
    }
  }
  
  // the Key was not Found, create a new Elem
  myNumber++;
  Standard_Integer hint = myNumber % theSize;
  Handle(PCollection_IndexedDataMapNode) theNewNode = 
   new PCollection_IndexedDataMapNode(aKey, myNumber, anItem,
	 		            myArrayKey->Value(hcode),
				    myArrayIndices->Value(hint));
  myArrayKey->SetValue(hcode,theNewNode);
  myArrayIndices->SetValue(hint,theNewNode);

  return myNumber;
}

//=======================================================================
// Function : FindIndex
// Purpose  : returns the Index of the Key, 0 if the Key is not stored
//=======================================================================

Standard_Integer PCollection_HIndexedDataMap::FindIndex(const Key& aKey) const
{
  Standard_Integer ResHash;
  Handle(PCollection_IndexedDataMapNode) theKeyNode;

  // search the Key in the map
  ResHash = myKeyHash.HashCode(aKey,myArrayKey->Length());
  theKeyNode = myArrayKey->Value(ResHash);
  while (! theKeyNode.IsNull()) {
    if ( myKeyHash.Compare(theKeyNode->GetKey(),aKey))
      // if the Key already exists stop the search
      return theKeyNode->Index();

    else
      // go to next element
      theKeyNode = theKeyNode->NextKey();
  }
  // not found, return 0
  return 0;
}

//=======================================================================
// Function : FindKey
// Purpose  : 
//=======================================================================

Key PCollection_HIndexedDataMap::FindKey(const Standard_Integer Index) const
{
  // search the index
  Handle(PCollection_IndexedDataMapNode) theNode = LocateIndex(Index);
  // get the Key
  return theNode->GetKey();
}

//=======================================================================
// Function : FindItemFromKey
// Purpose  : return the Item stored with the  Key
//=======================================================================

Item PCollection_HIndexedDataMap::FindItemFromKey(const Key& aKey) const
{
  // find the Key
  Handle(PCollection_IndexedDataMapNode) theNode = LocateKey(aKey);

  // get the Item
  return theNode->GetItem();
}

//=======================================================================
// Function : FindItemFromIndex
// Purpose  : Find an Item from the index
//=======================================================================

Item PCollection_HIndexedDataMap::FindItemFromIndex
  (const Standard_Integer Index) const
{
  // search the index
  Handle(PCollection_IndexedDataMapNode) theNode = LocateIndex(Index);
  // get the Item
  return theNode->GetItem();
}

//=======================================================================
// Function : FindIndexAndItem
// Purpose  : find the index and the Item for a key
//=======================================================================

void PCollection_HIndexedDataMap::FindIndexAndItem(const Key& aKey,
					      Standard_Integer& Index,
					      Item& theItem) const
{
  // find the Key
  Handle(PCollection_IndexedDataMapNode) theNode = LocateKey(aKey);

  // get Index and Item
  theNode->IndexAndItem(Index,theItem);
}

//=======================================================================
// Function : FindKeyAndItem
// Purpose  : find the Key and the Item for an Index
//=======================================================================

void PCollection_HIndexedDataMap::FindKeyAndItem(const Standard_Integer Index,
					    Key& theKey, 
					    Item& theItem) const
{
  // find the index
  Handle(PCollection_IndexedDataMapNode) TheNode = LocateIndex(Index);

  // get Key and Item
  TheNode->KeyAndItem(theKey,theItem);
}

//=======================================================================
// Function : SetItemToKey
// Purpose  : change the Item stored with a Key
//=======================================================================

void PCollection_HIndexedDataMap::SetItemToKey(const Key& aKey,
					  const Item& anItem)
{
  // find the Key
  Handle(PCollection_IndexedDataMapNode) TheNode = LocateKey(aKey);

  // set the Item
  TheNode->SetItem(anItem);
}

//=======================================================================
// Function : SetItemToIndex
// Purpose  : change the Item stored with an Index
//=======================================================================

void PCollection_HIndexedDataMap::SetItemToIndex(const Standard_Integer Index,
					    const Item& anItem)
{
  // find the Key
  Handle(PCollection_IndexedDataMapNode) TheNode = LocateIndex(Index);

  // set the Item
  TheNode->SetItem(anItem);
}

//=======================================================================
// Function : Clear
// Purpose  : 
//=======================================================================

void PCollection_HIndexedDataMap::Clear()
{
  Handle(PCollection_IndexedDataMapNode) nullNode,theNode,delNode;
  Standard_Integer I;

  myNumber = 0;

  for ( I = 0 ; I < myArrayKey->Length() ; I++ ) {
    theNode = myArrayKey->Value(I+1);
    myArrayKey->SetValue(I+1,nullNode);
    myArrayIndices->SetValue(I,nullNode);
    while ( !theNode.IsNull() ) {
      delNode = theNode;
      theNode = theNode->NextKey();
      delNode->SetNextKey(nullNode);
      delNode->SetNextIndex(nullNode);
      delNode.Delete();
    }
  }
}

//=======================================================================
// Function : IsBound
// Purpose  : Standard_True if the Map  contains  the Key
//=======================================================================

Standard_Boolean PCollection_HIndexedDataMap::IsBound(const Key& aKey) const
{
  return (FindIndex(aKey) != 0);
}

//=======================================================================
// Function : LocateKey
// Purpose  : find the Map Element containing a Shape, Null if none
//=======================================================================

Handle(PCollection_IndexedDataMapNode) 
     PCollection_HIndexedDataMap::LocateKey(const Key& aKey) const
{
  Standard_Integer ResHash = myKeyHash.HashCode(aKey,myArrayKey->Length());
  // search the Key in the map
  Handle(PCollection_IndexedDataMapNode) theNode = myArrayKey->Value(ResHash);
  while (! theNode.IsNull()) {
    if ( myKeyHash.Compare(aKey,theNode->GetKey()))
      // if the Key already exists stop the search
      return theNode;
    else
      // go to next element
      theNode = theNode->NextKey();
  }

  // raises Standard_OutOfRange
  Standard_OutOfRange::Raise("HIndexedDataMap : Key not in Map");
  return theNode;
}

//=======================================================================
// Function : LocateIndex
// Purpose  : find the Map Element containing an Index, check bounds
//=======================================================================

Handle(PCollection_IndexedDataMapNode) PCollection_HIndexedDataMap::LocateIndex
       (const Standard_Integer Index) const
{
  // check bounds
  if ((Index < 1)||(Index > myNumber))
  Standard_OutOfRange::Raise("HIndexedDataMap : Bad Index");
  // search the Index in the map
  Handle(PCollection_IndexedDataMapNode) theNode = 
    myArrayIndices->Value(Index % myArrayIndices->Length());
  // the Index  SHOULD be in the list, so no NULL checking
  while (theNode->Index() != Index)
    theNode = theNode->NextIndex();

  // return the element
  return theNode;
}