-
Notifications
You must be signed in to change notification settings - Fork 0
/
stMStarLogicNode.h
573 lines (492 loc) · 17.4 KB
/
stMStarLogicNode.h
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
#ifndef __stMStarLogicNode__
#define __stMStarLogicNode__
#include "arboretum/stDBMNode.h"
#include "MStarNode.h"
template<class ObjectType, class EvaluatorType>
class stMStarTree;
template<class ObjectType, class EvaluatorType>
class stMStarLogicNode {
public:
typedef stGenericMatrix<stDistance> tDistanceMatrix;
typedef stMStarTree<ObjectType, EvaluatorType>::stSubtreeInfo tSubtreeInfo;
typedef stDBMMemNode < ObjectType > tDBMMemNode;
/**
* Creates a new instance of this node with no object
* @param maxOccupation The maximum number of entries.
*/
stMStarLogicNode(stCount maxOccupation);
~stMStarLogicNode();
/**
* Adds an object to this node. this method will claim the ownership
* of the object instance.
*/
stCount AddEntry(ObjectType* obj);
int AddEntry(stSize size, const stByte* object);
stCount GetNumberOfEntries() {
return Count;
}
ObjectType* GetObject(stCount idx) {
return Entries[idx].Object;
}
ObjectType* operator [] (stCount idx) {
return Entries[idx].Object;
}
stPageID GetPageID(stCount idx) {
return Entries[idx].PageID;
}
stCount GetNEntries(stCount idx) {
return Entries[idx].NEntries;
}
stDistance GetRadius(stCount idx) {
return Entries[idx].Radius;
}
stCount GetNumberOfFreeObjects();
void SetEntry(stCount idx, stPageID pageID, stCount nEntries, stDistance radius);
void AddNode(stMStarNode* node);
stCount GetRepresentativeIndex(stCount idx){
return this->RepIndex[idx];
}
ObjectType* GetRepresentative(stCount idx) {
return Entries[RepIndex[idx]].Object;
}
void SetRepresentative(stCount rep0, stCount rep1) {
this->RepIndex[0] = rep0;
this->RepIndex[1] = rep1;
}
void Distribute(stMStarNode* node0, stMStarNode* node1, tSubtreeInfo* promo, EvaluatorType* metricEvaluator);
void SetMinOccupation(stCount min) {
this->MinOccupation = min;
if ( (this->MinOccupation > (this->MaxEntries/2)) || (MinOccupation == 0) ) {
this->MinOccupation = 2;
}
}
bool IsRepresentative(stCount idx) {
return (idx == this->RepIndex[0]) || (idx == this->RepIndex[1]);
}
/**
* Gets the ownership of an object associated with a given entry
* This method will avoid the automatic destruction of the object
* instance by the destructor of this node.
*
* <p> This method can be used to buy the representative object to avoid
* unnecessary replications
*/
ObjectType* BuyObject(stCount idx) {
Entries[idx].Mine = false;
return Entries[idx].Object;
}
stCount BuildDistanceMatrix(stMStarNode* node, EvaluatorType * metricEvaluator);
void TestDistribution(tDBMMemNode * node0, tDBMMemNode * node1,
EvaluatorType * metricEvaluator);
void MaxDistancePromote();
private:
struct stMStarLogicEntry {
ObjectType* Object;
stPageID PageID;
stCount NEntries;
stDistance Radius;
stDistance Distance[2];
bool Mine;
bool Mapped;
};
stCount MinOccupation;
stCount MaxEntries;
stMStarLogicEntry* Entries;
stCount Count;
stCount RepIndex[2];
//tDistanceMatrix DMat;
stCount UpdateDistances(EvaluatorType* metricEvaluator);
public:
tDistanceMatrix DMat;
};
#include <algorithm>
template<class ObjectType, class EvaluatorType>
stMStarLogicNode<ObjectType, EvaluatorType>::stMStarLogicNode(stCount maxOccupation)
{
this->MaxEntries = maxOccupation;
this->Entries = new stMStarLogicEntry[MaxEntries];
this->RepIndex[0] = 0;
this->RepIndex[0] = 0;
this->Count = 0;
this->MinOccupation = (stCount)(0.25*MaxEntries);
if ( (MinOccupation > MaxEntries/2) || (MinOccupation == 0) ) {
MinOccupation = 2;
}
this->DMat.SetSize(MaxEntries, MaxEntries);
}
template<class ObjectType, class EvaluatorType>
stMStarLogicNode<ObjectType, EvaluatorType>::~stMStarLogicNode()
{
if (Entries != NULL) {
for (int idx = 0; idx < MaxEntries; idx++) {
if ( (Entries[idx].Object != NULL) && (Entries[idx].Mine == true) ) {
delete Entries[idx].Object;
}
}
}
this->Count = 0;
delete [] Entries;
}
template<class ObjectType, class EvaluatorType>
stCount stMStarLogicNode<ObjectType, EvaluatorType>::AddEntry(ObjectType* obj)
{
this->Entries[Count].Object = obj;
this->Entries[Count].Mine = true;
++Count;
return Count - 1;
}
template<class ObjectType, class EvaluatorType>
int stMStarLogicNode<ObjectType, EvaluatorType>::AddEntry(stSize size, const stByte* object)
{
if (this->Count < MaxEntries){
Entries[Count].Object = new ObjectType();
Entries[Count].Object->Unserialize(object, size);
Entries[Count].Mine = true;
++Count;
return Count - 1;
}
else {
return -1;
}
}
template<class ObjectType, class EvaluatorType>
stCount stMStarLogicNode<ObjectType, EvaluatorType>::GetNumberOfFreeObjects()
{
stCount idx;
stCount count = 0;
for (idx = 0; idx < GetNumberOfEntries(); idx++) {
if ( !this->Entries[idx].PageID ){
++count;
}
}
return count;
}
template<class ObjectType, class EvaluatorType>
void stMStarLogicNode<ObjectType, EvaluatorType>::SetEntry(stCount idx, stPageID pageID, stCount nEntries, stDistance radius)
{
this->Entries[idx].PageID = pageID;
this->Entries[idx].NEntries = nEntries;
this->Entries[idx].Radius = radius;
}
template<class ObjectType, class EvaluatorType>
void stMStarLogicNode<ObjectType, EvaluatorType>::AddNode(stMStarNode* node)
{
stCount idx;
#ifdef __stDEBUG__
if( this->Count + node->GetNumberOfEntries() > this->MaxEntries ) {
throw invalid_argument("Insuficient space to store the node ");
}
#endif
for (idx = 0; idx < node->GetNumberOfEntries(); idx++) {
AddEntry(node->GetObjectSize(idx), node->GetObject(idx));
this->Entries[idx].Radius = node->GetRadius(idx);
this->Entries[idx].NEntries = node->GetNEntries(idx);
this->Entries[idx].PageID = node->GetEntry(idx).PageID;
}
}
template<class ObjectType, class EvaluatorType>
stCount stMStarLogicNode<ObjectType, EvaluatorType>::BuildDistanceMatrix(stMStarNode* node, EvaluatorType * metricEvaluator)
{
for (int i = 0; i < this->Count; i++) {
DMat[i][i] = 0;
for (int j = 0; j < i; j++) {
if( node->at(i, j) < 0) {
DMat[i][j] = metricEvaluator->GetDistance(GetObject(i), GetObject(j));
}
else {
#ifdef __stDEBUB__
if ( metricEvaluator->GetDistance(GetObject(i), GetObject(j)) != node->at(i,j) ) {
printf("error builtDCMatrix, node: %d\n", node->GetPageID() );
cout << node->GetRepresentativeIndex() << endl;
printf("i=%d, j=%d\n", i, j);
cin.get();
}
#endif
DMat[i][j] = node->at(i, j);
}
DMat[j][i] = DMat[i][j];
}
}
return (this->Count * (this->Count - 1) )/2;//(N*(N-1))/2
}
template<class ObjectType, class EvaluatorType>
void stMStarLogicNode<ObjectType, EvaluatorType>::MaxDistancePromote()
{
}
//------------------------------------------------------------------------------
template <class ObjectType, class EvaluatorType>
void stMStarLogicNode<ObjectType, EvaluatorType>::Distribute(
stMStarNode * node0, stMStarNode * node1,
tSubtreeInfo * promo, EvaluatorType * metricEvaluator){
int i, j;
int idxRep0, idxRep1;
int idx;
int idx0, idx1;
int currObj;
stDistanceIndex * distanceIndex0,* distanceIndex1;
std::vector<int> cluster[2];
idxRep0 = GetRepresentativeIndex(0);
idxRep1 = GetRepresentativeIndex(1);
distanceIndex0 = new stDistanceIndex[this->Count];
distanceIndex1 = new stDistanceIndex[this->Count];
for (i=0; i < this->Count; i++) {
distanceIndex0[i].Index = i;
distanceIndex0[i].Distance = DMat[i][idxRep0];
distanceIndex1[i].Index = i;
distanceIndex1[i].Distance = DMat[i][idxRep1];
Entries[i].Mapped = false;
Entries[i].Mine = true;
}
//Sorting by distance...
sort(distanceIndex0, distanceIndex0 + this->Count);
sort(distanceIndex1, distanceIndex1 + this->Count);
// Make one of then get the minimum occupation.
idx0 = idx1 = 0;
// FIns a candidate for node 1
while ( Entries[distanceIndex0[idx0].Index].Mapped) {
idx0++;
}
// Add to node 1
currObj = distanceIndex0[idx0].Index;
Entries[currObj].Mapped = true;
cluster[0].push_back(currObj);
idx = node0->AddEntry(Entries[currObj].Object->GetSerializedSize(),
Entries[currObj].Object->Serialize(),
Entries[currObj].PageID);
node0->GetEntry(idx).Distance = distanceIndex0[idx0].Distance;
node0->SetNEntries(idx, Entries[currObj].NEntries);
node0->SetRadius(idx, Entries[currObj].Radius);
#ifdef __stDBMHEIGHT__
node0->GetEntry(idx).Height = Entries[currObj].Height;
#endif //__stDBMHEIGHT__
// Find a candidate for node 2
while (Entries[distanceIndex1[idx1].Index].Mapped){
idx1++;
}//end while
currObj = distanceIndex1[idx1].Index;
Entries[currObj].Mapped = true;
cluster[1].push_back(currObj);
idx = node1->AddEntry(Entries[currObj].Object->GetSerializedSize(),
Entries[currObj].Object->Serialize(),
Entries[currObj].PageID);
node1->GetEntry(idx).Distance = distanceIndex1[idx1].Distance;
node1->SetNEntries(idx, Entries[currObj].NEntries);
node1->SetRadius(idx, Entries[currObj].Radius);
#ifdef __stDBMHEIGHT__
node1->GetEntry(idx).Height = Entries[currObj].Height;
#endif //__stDBMHEIGHT__
//Distribute the others.
for (i = 0; i < this->Count; i++) {
if(Entries[i].Mapped == false) {
Entries[i].Mapped = true;
if(DMat[i][idxRep0] < DMat[i][idxRep1]){
cluster[0].push_back(i);
idx = node0->AddEntry(Entries[i].Object->GetSerializedSize(),
Entries[i].Object->Serialize(),
Entries[i].PageID);
if (idx >= 0){
node0->GetEntry(idx).Distance = DMat[i][idxRep0];
node0->SetNEntries(idx, Entries[i].NEntries);
node0->SetRadius(idx, Entries[i].Radius);
#ifdef __stDBMHEIGHT__
node0->GetEntry(idx).Height = Entries[i].Height;
#endif //__stDBMHEIGHT__
}else {
}
}
else {
cluster[1].push_back(i);
idx = node1->AddEntry(Entries[i].Object->GetSerializedSize(),
Entries[i].Object->Serialize(),
Entries[i].PageID);
if (idx >= 0){
node1->GetEntry(idx).Distance = DMat[i][idxRep1];
node1->SetNEntries(idx, Entries[i].NEntries);
node1->SetRadius(idx, Entries[i].Radius);
#ifdef __stDBMHEIGHT__
node1->GetEntry(idx).Height = Entries[i].Height;
#endif //__stDBMHEIGHT__
}else {
}
}
}
}//end for
delete [] distanceIndex0;
delete [] distanceIndex1;
#ifdef CHECK_LEMA1
// Distribute distance en DCmatrix of node0
for ( i = 1; i < cluster[0].size(); i++) {
for (j = 0; j < i; j++) {
idx0 = cluster[0][i];
idx1 = cluster[0][j];
(*node0)(i, j) = DMat[idx0][idx1];
}
}
// Distribute distance en DCmatrix of node1
for ( i = 1; i < cluster[1].size(); i++) {
for (j = 0; j < i; j++) {
idx0 = cluster[1][i];
idx1 = cluster[1][j];
(*node1)(i, j) = DMat[idx0][idx1];
}
}
#endif
// Representatives
promo[0].Rep = BuyObject(GetRepresentativeIndex(0));
promo[0].Radius = node0->GetMinimumRadius();
promo[0].RootID = node0->GetPageID();
promo[0].NObjects = node0->GetTotalObjectCount();
promo[1].Rep = BuyObject(GetRepresentativeIndex(1));
promo[1].Radius = node1->GetMinimumRadius();
promo[1].RootID = node1->GetPageID();
promo[1].NObjects = node1->GetTotalObjectCount();
}//end stDBMLogicNode::Distribute
template <class ObjectType, class EvaluatorType>
void stMStarLogicNode<ObjectType, EvaluatorType>::TestDistribution(
tDBMMemNode * node0, tDBMMemNode * node1,
EvaluatorType * metricEvaluator){
int idx, i;
int idx0, idx1;
int currObj;
stDistanceIndex * distanceIndex0, * distanceIndex1;
stCount idxRep0, idxRep1;
// Clean before use.
node0->RemoveAll();
node1->RemoveAll();
// Get the index of representatives.
idxRep0 = GetRepresentativeIndex(0);
idxRep1 = GetRepresentativeIndex(1);
// Get space to store.
distanceIndex0 = new stDistanceIndex[this->Count];
distanceIndex1 = new stDistanceIndex[this->Count];
// Init Map
for (i = 0; i < this->Count; i++){
distanceIndex0[i].Index = i;
distanceIndex0[i].Distance = DMat[i][idxRep0];
distanceIndex1[i].Index = i;
distanceIndex1[i].Distance = DMat[i][idxRep1];
Entries[i].Mapped = false;
Entries[i].Mine = true;
}//end for
// Sorting by distance...
sort(distanceIndex0, distanceIndex0 + this->Count);
sort(distanceIndex1, distanceIndex1 + this->Count);
// Make one of then get the minimum occupation.
idx0 = idx1 = 0;
// Add at least this->MinOccupation objects to each node.
// for (i = 0; i < 2; i++){
// Find a candidate for node 1
while (Entries[distanceIndex0[idx0].Index].Mapped){
idx0++;
}//end while
// Add to node 1
currObj = distanceIndex0[idx0].Index;
Entries[currObj].Mapped = true;
idx = node0->AddEntry(Entries[currObj].Object->GetSerializedSize(),
Entries[currObj].Object,
Entries[currObj].PageID);
node0->SetEntry(idx, distanceIndex0[idx0].Distance
#ifdef __stDBMNENTRIES__
, Entries[currObj].NEntries
#endif //__stDBMNENTRIES__
, Entries[currObj].Radius
#ifdef __stDBMHEIGHT__
, Entries[currObj].Height
#endif //__stDBMHEIGHT__
);
// Find a candidate for node 2
while (Entries[distanceIndex1[idx1].Index].Mapped){
idx1++;
}//end while
// Add to node 2
currObj = distanceIndex1[idx1].Index;
Entries[currObj].Mapped = true;
idx = node1->AddEntry(Entries[currObj].Object->GetSerializedSize(),
Entries[currObj].Object,
Entries[currObj].PageID);
node1->SetEntry(idx, distanceIndex1[idx1].Distance
#ifdef __stDBMNENTRIES__
, Entries[currObj].NEntries
#endif //__stDBMNENTRIES__
, Entries[currObj].Radius
#ifdef __stDBMHEIGHT__
, Entries[currObj].Height
#endif //__stDBMHEIGHT__
);
// }//end for
// Distribute the others.
for (i = 0; i < this->Count; i++){
// If this entry was not mapped.
if (Entries[i].Mapped == false){
// Set this entry to mapped.
Entries[i].Mapped = true;
// Where I will put it? In node 1 or 2?
if (DMat[i][idxRep0] < DMat[i][idxRep1]){
// This new entry has a distance to node 1 lesser than to node 2.
// Try to put on node 1 first
idx = node0->AddEntry(Entries[i].Object->GetSerializedSize(),
Entries[i].Object,
Entries[i].PageID);
if (idx >= 0){
node0->SetEntry(idx, DMat[i][idxRep0]
#ifdef __stDBMNENTRIES__
, Entries[i].NEntries
#endif //__stDBMNENTRIES__
, Entries[i].Radius
#ifdef __stDBMHEIGHT__
, Entries[i].Height
#endif //__stDBMHEIGHT__
);
}else {
// Let's put it in the node 2 since it doesn't fit in the node 1
idx = node1->AddEntry(Entries[i].Object->GetSerializedSize(),
Entries[i].Object,
Entries[i].PageID);
node1->SetEntry(idx, DMat[i][idxRep1]
#ifdef __stDBMNENTRIES__
, Entries[i].NEntries
#endif //__stDBMNENTRIES__
, Entries[i].Radius
#ifdef __stDBMHEIGHT__
, Entries[i].Height
#endif //__stDBMHEIGHT__
);
}//end if
}else{
// Try to put on node 2 first
idx = node1->AddEntry(Entries[i].Object->GetSerializedSize(),
Entries[i].Object,
Entries[i].PageID);
if (idx >= 0){
node1->SetEntry(idx, DMat[i][idxRep1]
#ifdef __stDBMNENTRIES__
, Entries[i].NEntries
#endif //__stDBMNENTRIES__
, Entries[i].Radius
#ifdef __stDBMHEIGHT__
, Entries[i].Height
#endif //__stDBMHEIGHT__
);
}else {
// Let's put it in the node 1 since it doesn't fit in the node 2
idx = node0->AddEntry(Entries[i].Object->GetSerializedSize(),
Entries[i].Object,
Entries[i].PageID);
node0->SetEntry(idx, DMat[i][idxRep0]
#ifdef __stDBMNENTRIES__
, Entries[i].NEntries
#endif //__stDBMNENTRIES__
, Entries[i].Radius
#ifdef __stDBMHEIGHT__
, Entries[i].Height
#endif //__stDBMHEIGHT__
);
}//end if
}//end if
}//end if
}//end for
// Clean home before go away...
delete[] distanceIndex0;
delete[] distanceIndex1;
}//end stDBMLogicNode::TestDistribution
#endif //__stMStarLogicNode__