@@ -143,16 +143,16 @@ class BePlusTree
143143 if (level == 0 )
144144 {
145145 if (root) {
146- ((ItemList*) root) ->clear ();
146+ root. items ->clear ();
147147 }
148148 return ;
149149 }
150150
151151 // Find first items page
152- void * temp = root;
152+ NodePtr temp = root;
153153 for (int i = level; i > 0 ; i--)
154- temp = (*(NodeList *) temp)[0 ];
155- ItemList *items = (ItemList *) temp;
154+ temp = (*temp. nodes )[0 ];
155+ ItemList *items = temp. items ;
156156
157157 // Delete all items pages
158158 NodeList *lists = items->parent ;
@@ -186,12 +186,12 @@ class BePlusTree
186186 ~BePlusTree ()
187187 {
188188 clear ();
189- pool->deallocate (root);
189+ pool->deallocate (root. pointer );
190190 }
191191
192192 bool isEmpty () const
193193 {
194- return root == NULL || (level == 0 && ((ItemList*) root) ->getCount () == 0 );
194+ return ! root || (level == 0 && root. items ->getCount () == 0 );
195195 }
196196
197197 bool add (const Value& item) { return defaultAccessor.add (item); }
@@ -232,14 +232,14 @@ class BePlusTree
232232
233233 if (level == 0 )
234234 {
235- if (root == NULL )
236- return other.root == NULL ;
237- if (other.root == NULL )
235+ if (! root)
236+ return ! other.root ;
237+ if (! other.root )
238238 return true ;
239- return ((ItemList*) root) ->getCount () > ((ItemList*) other.root ) ->getCount ();
239+ return root. items ->getCount () > other.root . items ->getCount ();
240240 }
241241
242- return ((NodeList*) root) ->getCount () > ((NodeList*) other.root ) ->getCount ();
242+ return root. nodes ->getCount () > other.root . nodes ->getCount ();
243243 }
244244
245245 // Compute approximate number of leafs in the tree
@@ -249,7 +249,7 @@ class BePlusTree
249249 return 0 ;
250250
251251 if (level == 0 )
252- return ((ItemList*) root) ->getCount ();
252+ return root. items ->getCount ();
253253
254254 // Tree is large. Roughly estimate number of leaf nodes using number of
255255 // items in root list and depth of the tree. Theoretically possible fill
@@ -261,7 +261,7 @@ class BePlusTree
261261 items_per_node *= NodeCount * 3 / 5 ;
262262
263263 fb_assert (items_per_node);
264- return ((NodeList*) root) ->getCount () * items_per_node;
264+ return root. nodes ->getCount () * items_per_node;
265265 }
266266
267267 // Compute approximate memory consumption for tree in bytes
@@ -281,7 +281,7 @@ class BePlusTree
281281 bytes_per_node *= NodeCount * 3 / 5 ;
282282
283283 fb_assert (bytes_per_node);
284- return ((NodeList*) root) ->getCount () * bytes_per_node;
284+ return root. nodes ->getCount () * bytes_per_node;
285285 }
286286
287287 void append (const BePlusTree& from)
@@ -329,7 +329,20 @@ class BePlusTree
329329#endif
330330 };
331331
332- class NodeList : public SortedVector <void *, NodeCount, Key, NodeList, Cmp>
332+ union NodePtr
333+ {
334+ NodePtr (void * p = nullptr ) : pointer (p) {}
335+
336+ operator bool () const { return pointer != nullptr ; }
337+
338+ bool operator ==(const void * ptr) const { return pointer == ptr; }
339+
340+ ItemList* items;
341+ NodeList* nodes;
342+ void * pointer;
343+ };
344+
345+ class NodeList : public SortedVector <NodePtr, NodeCount, Key, NodeList, Cmp>
333346 {
334347 public:
335348 // Adds newly created item to the doubly-linked list
@@ -347,32 +360,32 @@ class BePlusTree
347360 int level;
348361 NodeList *parent;
349362 NodeList *next, *prev;
350- static const Key& generate (const void * sender, void * item)
363+ static const Key& generate (const void * sender, NodePtr item)
351364 {
352365 for (int lev = ((NodeList *)sender)->level ; lev > 0 ; lev--)
353- item = *((NodeList *) item) ->begin ();
366+ item = *(item. nodes ->begin () );
354367
355368 // ItemList reference below is ISO C++ compliant.
356369 // If your compiler has broken name lookup sequence then conditionally
357370 // add ItemList typedef for you compiler with whichever syntax it likes
358- return KeyOfValue::generate (item, *((ItemList *) item) ->begin ());
371+ return KeyOfValue::generate (item. items , *(item. items ->begin () ));
359372 }
360- static void setNodeParentAndLevel (void * node, const int level, NodeList* parent)
373+ static void setNodeParentAndLevel (NodePtr node, const int level, NodeList* parent)
361374 {
362375 if (level)
363376 {
364- ((NodeList *) node) ->parent = parent;
365- ((NodeList *) node) ->level = level - 1 ;
377+ node. nodes ->parent = parent;
378+ node. nodes ->level = level - 1 ;
366379 }
367380 else
368- ((ItemList *) node) ->parent = parent;
381+ node. items ->parent = parent;
369382 }
370- static void setNodeParent (void * node, const int level, NodeList* parent)
383+ static void setNodeParent (NodePtr node, const int level, NodeList* parent)
371384 {
372385 if (level)
373- ((NodeList*) node) ->parent = parent;
386+ node. nodes ->parent = parent;
374387 else
375- ((ItemList*) node) ->parent = parent;
388+ node. items ->parent = parent;
376389 }
377390 };
378391
@@ -395,22 +408,22 @@ class BePlusTree
395408 {
396409 // Inlining is efficient here because LocType will be known in most cases
397410 // and compiler will be able to eliminate most of code
398- void * list = tree->root ;
411+ NodePtr list = tree->root ;
399412 if (!list)
400413 return false ; // Uninitialized tree
401414
402415 for (int lev = tree->level ; lev; lev--)
403416 {
404417 FB_SIZE_T pos;
405- if (!((NodeList *) list) ->find (key, pos))
418+ if (!list. nodes ->find (key, pos))
406419 {
407420 if (pos > 0 )
408421 pos--;
409422 }
410- list = (*(NodeList *) list)[pos];
423+ list = (*list. nodes )[pos];
411424 }
412425
413- curr = (ItemList *) list;
426+ curr = list. items ;
414427 const bool found = curr->find (key, curPos);
415428 switch (lt)
416429 {
@@ -454,30 +467,30 @@ class BePlusTree
454467 // position of accessor is not defined.
455468 bool getFirst ()
456469 {
457- void * items = tree->root ;
470+ NodePtr items = tree->root ;
458471 if (!items)
459472 return false ; // Uninitialized tree
460473
461474 for (int i = tree->level ; i > 0 ; i--)
462- items = (*(NodeList*) items)[0 ];
463- curr = (ItemList*) items;
475+ items = (*items. nodes )[0 ];
476+ curr = items. items ;
464477 curPos = 0 ;
465- return ((ItemList*) items) ->getCount () != 0 ;
478+ return items. items ->getCount () != 0 ;
466479 }
467480 // If method returns false it means list is empty and
468481 // position of accessor is not defined.
469482 bool getLast ()
470483 {
471- void * items = tree->root ;
484+ NodePtr items = tree->root ;
472485 if (!items)
473486 return false ; // Uninitialized tree
474487
475488 for (int i = tree->level ; i > 0 ; i--)
476- items = (*(NodeList*) items)[((NodeList*) items) ->getCount () - 1 ];
477- curr = (ItemList *) items;
478- if (((ItemList*) items) ->getCount ())
489+ items = (*items. nodes )[ items. nodes ->getCount () - 1 ];
490+ curr = items. items ;
491+ if (items. items ->getCount ())
479492 {
480- curPos = ((ItemList*) items) ->getCount () - 1 ;
493+ curPos = items. items ->getCount () - 1 ;
481494 return true ;
482495 }
483496 return false ;
@@ -654,10 +667,10 @@ class BePlusTree
654667private:
655668 Allocator* pool;
656669 int level;
657- void * root;
670+ NodePtr root;
658671 Accessor defaultAccessor;
659672
660- void _removePage (int level, void * node);
673+ void _removePage (int level, NodePtr node);
661674
662675 friend class MemoryPool ;
663676 friend class NodeList ;
@@ -674,20 +687,20 @@ bool BePlusTree<Value, Key, Allocator, KeyOfValue, Cmp>::add(const Value& item,
674687 root = new (pool->allocate (sizeof (ItemList) ALLOC_ARGS)) ItemList ();
675688
676689 // Find leaf page for our item
677- void * vList = this ->root ;
690+ NodePtr vList = this ->root ;
678691 const Key& key = KeyOfValue::generate (NULL , item);
679692 for (int lev = this ->level ; lev > 0 ; lev--)
680693 {
681694 FB_SIZE_T pos;
682- if (!((NodeList *) vList) ->find (key, pos))
695+ if (!vList. nodes ->find (key, pos))
683696 {
684697 if (pos > 0 )
685698 pos--;
686699 }
687- vList = (*(NodeList *) vList)[pos];
700+ vList = (*vList. nodes )[pos];
688701 }
689702
690- ItemList *leaf = (ItemList *) vList;
703+ ItemList *leaf = vList. items ;
691704
692705 FB_SIZE_T pos;
693706 if (leaf->find (key, pos))
@@ -775,7 +788,7 @@ bool BePlusTree<Value, Key, Allocator, KeyOfValue, Cmp>::add(const Value& item,
775788 recovery_map[0 ] = pos;
776789 }
777790
778- void * newNode = newLeaf;
791+ NodePtr newNode = newLeaf;
779792 NodeList *nodeList = leaf->parent ;
780793 int curLevel = 0 ;
781794 try {
@@ -803,7 +816,7 @@ bool BePlusTree<Value, Key, Allocator, KeyOfValue, Cmp>::add(const Value& item,
803816 }
804817 else
805818 {
806- void * t = (*nodeList)[NodeCount - 1 ];
819+ NodePtr t = (*nodeList)[NodeCount - 1 ];
807820 NodeList::setNodeParent (t, curLevel, list);
808821 list->insert (0 , t);
809822 nodeList->shrink (NodeCount - 1 );
@@ -823,7 +836,7 @@ bool BePlusTree<Value, Key, Allocator, KeyOfValue, Cmp>::add(const Value& item,
823836 }
824837 else
825838 {
826- void * t = (*nodeList)[0 ];
839+ NodePtr t = (*nodeList)[0 ];
827840 NodeList::setNodeParent (t, curLevel, list);
828841 list->insert (list->getCount (), t);
829842 nodeList->remove (0 );
@@ -848,7 +861,7 @@ bool BePlusTree<Value, Key, Allocator, KeyOfValue, Cmp>::add(const Value& item,
848861 }
849862 else
850863 {
851- void * t = (*nodeList)[NodeCount - 1 ];
864+ NodePtr t = (*nodeList)[NodeCount - 1 ];
852865 NodeList::setNodeParent (t, curLevel, newList);
853866 newList->insert (0 , t);
854867 nodeList->shrink (NodeCount - 1 );
@@ -877,8 +890,8 @@ bool BePlusTree<Value, Key, Allocator, KeyOfValue, Cmp>::add(const Value& item,
877890 // Recover tree to innocent state
878891 while (curLevel)
879892 {
880- NodeList *itemL = reinterpret_cast <NodeList*>( newNode) ;
881- void * lower;
893+ NodeList *itemL = newNode. nodes ;
894+ NodePtr lower;
882895 if (recovery_map[curLevel] == MAP_NEW_PAGE) {
883896 lower = (*itemL)[0 ];
884897 }
@@ -890,31 +903,31 @@ bool BePlusTree<Value, Key, Allocator, KeyOfValue, Cmp>::add(const Value& item,
890903 NodeList::setNodeParent ((*itemL)[0 ], curLevel - 1 , itemL->prev );
891904 }
892905 itemL->~NodeList ();
893- this ->pool ->deallocate (newNode);
906+ this ->pool ->deallocate (newNode. pointer );
894907 newNode = lower;
895908 curLevel--;
896909 }
897- ItemList *itemL2 = reinterpret_cast <ItemList*>( newNode) ;
910+ ItemList *itemL2 = newNode. items ;
898911 if (recovery_map[0 ] != MAP_NEW_PAGE)
899912 {
900913 itemL2->prev ->remove (recovery_map[0 ]);
901914 itemL2->prev ->insert (itemL2->prev ->getCount (), (*itemL2)[0 ]);
902915 }
903916 itemL2->~ItemList ();
904- this ->pool ->deallocate (newNode);
917+ this ->pool ->deallocate (newNode. pointer );
905918 throw ;
906919 }
907920 return true ;
908921}
909922
910923template <typename Value, typename Key, typename Allocator, typename KeyOfValue, typename Cmp>
911- void BePlusTree<Value, Key, Allocator, KeyOfValue, Cmp>::_removePage(const int nodeLevel, void * node)
924+ void BePlusTree<Value, Key, Allocator, KeyOfValue, Cmp>::_removePage(const int nodeLevel, NodePtr node)
912925{
913926 NodeList *list;
914927 // Get parent and adjust the links
915928 if (nodeLevel)
916929 {
917- NodeList *temp = (NodeList *) node;
930+ NodeList *temp = node. nodes ;
918931 if (temp->prev )
919932 temp->prev ->next = temp->next ;
920933 if (temp->next )
@@ -923,7 +936,7 @@ void BePlusTree<Value, Key, Allocator, KeyOfValue, Cmp>::_removePage(const int n
923936 }
924937 else
925938 {
926- ItemList *temp = (ItemList *) node;
939+ ItemList *temp = node. items ;
927940 if (temp->prev )
928941 temp->prev ->next = temp->next ;
929942 if (temp->next )
@@ -972,7 +985,7 @@ void BePlusTree<Value, Key, Allocator, KeyOfValue, Cmp>::_removePage(const int n
972985#endif
973986 list->remove (pos);
974987
975- if (list == root && list->getCount () == 1 )
988+ if (root == list && list->getCount () == 1 )
976989 {
977990 // We reached the top of the tree and were asked to modify root
978991 // page so only one node will be left in this case.
@@ -1007,10 +1020,10 @@ void BePlusTree<Value, Key, Allocator, KeyOfValue, Cmp>::_removePage(const int n
10071020 }
10081021
10091022 if (nodeLevel)
1010- ((NodeList *) node) ->~NodeList ();
1023+ node. nodes ->~NodeList ();
10111024 else
1012- ((ItemList *) node) ->~ItemList ();
1013- pool->deallocate (node);
1025+ node. items ->~ItemList ();
1026+ pool->deallocate (node. pointer );
10141027}
10151028
10161029} // namespace Firebird
0 commit comments