Skip to content

Commit 9817b74

Browse files
committed
Update utl_rbtree.inc
1 parent b58f578 commit 9817b74

File tree

1 file changed

+44
-69
lines changed

1 file changed

+44
-69
lines changed

include/tier1/utl_rbtree.inc

Lines changed: 44 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -347,23 +347,17 @@ methodmap UtlRBTree < AddressBase {
347347
}
348348

349349
public any Get(int i, int size = 4) {
350-
if (!this.IsValidIndex(i, size))
351-
ThrowError("Param index (%d) out of bounds. m_nAllocationCount: [0 - %d], m_LastAlloc: [0 - %d].", i, this.m_nAllocationCount, this.m_LastAlloc);
352-
350+
Assert(this.IsValidIndex(i, size), UTL_RBTREE_TAG, "Param index (%d) out of bounds. m_nAllocationCount: [0 - %d], m_LastAlloc: [0 - %d].", i, this.m_nAllocationCount, this.m_LastAlloc);
353351
return view_as<UtlRBTreeNode_t>(view_as<any>(this.m_pMemory) + i * size).m_Data;
354352
}
355353

356354
public void Set(int i, int size = 4, any val) {
357-
if (!this.IsValidIndex(i, size))
358-
ThrowError("Param index (%d) out of bounds. m_nAllocationCount: [0 - %d], m_LastAlloc: [0 - %d].", i, this.m_nAllocationCount, this.m_LastAlloc);
359-
355+
Assert(this.IsValidIndex(i, size), UTL_RBTREE_TAG, "Param index (%d) out of bounds. m_nAllocationCount: [0 - %d], m_LastAlloc: [0 - %d].", i, this.m_nAllocationCount, this.m_LastAlloc);
360356
view_as<UtlRBTreeNode_t>(view_as<any>(this.m_pMemory) + i * size).m_Data = val;
361357
}
362358

363359
public any GetEx(int i, int size = 4) {
364-
if (!this.IsValidIndex(i, size))
365-
ThrowError("Param index (%d) out of bounds. m_nAllocationCount: [0 - %d], m_LastAlloc: [0 - %d].", i, this.m_nAllocationCount, this.m_LastAlloc);
366-
360+
Assert(this.IsValidIndex(i, size), UTL_RBTREE_TAG, "Param index (%d) out of bounds. m_nAllocationCount: [0 - %d], m_LastAlloc: [0 - %d].", i, this.m_nAllocationCount, this.m_LastAlloc);
367361
return view_as<UtlRBTreeNode_t>(view_as<any>(this.m_pMemory) + i * size).GetDataAddress();
368362
}
369363

@@ -442,9 +436,7 @@ methodmap UtlRBTree < AddressBase {
442436
}
443437

444438
public UtlRBTreeLinks_t Links(int i, int size = 4) {
445-
if (i == UtlRBTree.InvalidIndex())
446-
ThrowError("Param index (%d) invalid", i);
447-
439+
Assert(i != UtlRBTree.InvalidIndex(), UTL_RBTREE_TAG, "Param index (%d) out of bounds", i);
448440
return view_as<UtlRBTreeLinks_t>(view_as<any>(this.m_pMemory) + i * size);
449441
}
450442

@@ -757,13 +749,14 @@ methodmap UtlRBTree < AddressBase {
757749
}
758750

759751
public void Link(int elem, int size = 4) {
760-
if (elem != UtlRBTree.InvalidIndex())
761-
{
752+
if (elem != UtlRBTree.InvalidIndex()) {
762753
int parent = UtlRBTree.InvalidIndex();
763754
bool leftchild = false;
764755

765756
this.FindInsertionPosition(this.Element(elem, size), parent, leftchild, size);
766757
this.LinkToParent(elem, parent, leftchild, size);
758+
759+
Assert(this.IsValid(), UTL_RBTREE_TAG, "Invalid RBTree after Link");
767760
}
768761
}
769762

@@ -780,8 +773,8 @@ methodmap UtlRBTree < AddressBase {
780773

781774
public int NextInorder(int i, int size = 4) {
782775
// Don't go into an infinite loop if it's a bad index
776+
Assert(this.IsValidIndex(i, size), UTL_RBTREE_TAG, "Param index (%d) invalid.", i);
783777
if (!this.IsValidIndex(i, size)) {
784-
ThrowError("Param index (%d) invalid.", i);
785778
return UtlRBTree.InvalidIndex();
786779
}
787780

@@ -811,8 +804,8 @@ methodmap UtlRBTree < AddressBase {
811804

812805
public int PrevInorder(int i, int size = 4) {
813806
// Don't go into an infinite loop if it's a bad index
807+
Assert(this.IsValidIndex(i, size), UTL_RBTREE_TAG, "Param index (%d) invalid.", i);
814808
if (!this.IsValidIndex(i, size)) {
815-
ThrowError("Param index (%d) invalid.", i);
816809
return UtlRBTree.InvalidIndex();
817810
}
818811

@@ -877,13 +870,11 @@ methodmap UtlRBTree < AddressBase {
877870
return UtlRBTree.InvalidIndex();
878871
}
879872

880-
/*
881-
I CUtlRBTree<T, I, L, M>::PrevPreorder( I i ) const
882-
{
883-
Assert(0); // not implemented yet
884-
return InvalidIndex();
873+
public int PrevPreorder(int i) {
874+
Assert_Zero(UTL_RBTREE_TAG, "UtlRBtree.PrevPreorder() Not implemented yet.");
875+
i = UtlRBTree.InvalidIndex();
876+
return i;
885877
}
886-
*/
887878

888879
public int LastPreorder(int size = 4) {
889880
int i = this.m_Root;
@@ -990,15 +981,13 @@ methodmap UtlRBTree < AddressBase {
990981
this.m_LessFunc = lessFunc;
991982
} else if (this.Count() > 0){
992983
// need to re-sort the tree here....
993-
ThrowError("LessFunc is already set.");
984+
Assert_Zero(UTL_RBTREE_TAG, "Can't change the lessFunc after inserting elements.");
994985
}
995986
}
996987

997988
// Inserts a node into the tree, doesn't copy the data in.
998989
public void FindInsertionPosition(any insert, int &parent, bool &leftchild, int size = 4) {
999-
if (!(!!this.m_LessFunc)) {
1000-
ThrowError("LessFunc is not set.");
1001-
}
990+
Assert(!!this.m_LessFunc, UTL_RBTREE_TAG, "LessFunc is not set.");
1002991

1003992
/* find where node belongs */
1004993
int current = this.m_Root;
@@ -1018,9 +1007,7 @@ methodmap UtlRBTree < AddressBase {
10181007
}
10191008

10201009
public int Find(any search, int size = 4) {
1021-
if (!(!!this.m_LessFunc)) {
1022-
ThrowError("LessFunc is not set.");
1023-
}
1010+
Assert(!!this.m_LessFunc, UTL_RBTREE_TAG, "LessFunc is not set.");
10241011

10251012
int current = this.m_Root;
10261013
while (current != UtlRBTree.InvalidIndex()) {
@@ -1037,9 +1024,7 @@ methodmap UtlRBTree < AddressBase {
10371024
}
10381025

10391026
public int FindFirst(any search, int size = 4) {
1040-
if (!(!!this.m_LessFunc)) {
1041-
ThrowError("LessFunc is not set.");
1042-
}
1027+
Assert(!!this.m_LessFunc, UTL_RBTREE_TAG, "LessFunc is not set.");
10431028

10441029
int current = this.m_Root;
10451030
int best = UtlRBTree.InvalidIndex();
@@ -1058,13 +1043,10 @@ methodmap UtlRBTree < AddressBase {
10581043
}
10591044

10601045
public int FindClosest(any search, int size = 4, CompareOperands_t eFindCriteria) {
1061-
if (!(!!this.m_LessFunc)) {
1062-
ThrowError("LessFunc is not set.");
1063-
}
1046+
Assert(!!this.m_LessFunc, UTL_RBTREE_TAG, "LessFunc is not set.");
10641047

1065-
if (! (( eFindCriteria & ( k_EGreaterThan | k_ELessThan ) ) ^ ( k_EGreaterThan | k_ELessThan )) ) {
1066-
ThrowError("Invalid FindCriteria (%d).", eFindCriteria);
1067-
}
1048+
// error 450: no viable conversion from "CompareOperands_t" to "bool"
1049+
Assert( view_as<bool>(( eFindCriteria & ( k_EGreaterThan | k_ELessThan ) ) ^ ( k_EGreaterThan | k_ELessThan )), UTL_RBTREE_TAG, "Invalid FindCriteria (%d).", eFindCriteria);
10681050

10691051
int current = this.m_Root;
10701052
int best = UtlRBTree.InvalidIndex();
@@ -1105,11 +1087,10 @@ methodmap UtlRBTree < AddressBase {
11051087
// Allocates / deallocates nodes
11061088
//-----------------------------------------------------------------------------
11071089
public void Grow(int alloccount = 1, int size = 4) {
1108-
if (alloccount <= 0)
1109-
ThrowError("Param alloccount (%d) invalid.", alloccount);
1090+
Assert((alloccount > 0), UTL_RBTREE_TAG, "Param alloccount (%d) invalid.", alloccount);
11101091

11111092
if (this.IsExternallyAllocated()) {
1112-
ThrowError("Can't grow a buffer whose memory was externally allocated .");
1093+
Assert_Zero(UTL_RBTREE_TAG, "Can't grow a buffer whose memory was externally allocated.");
11131094
return;
11141095
}
11151096

@@ -1130,6 +1111,7 @@ methodmap UtlRBTree < AddressBase {
11301111
if ( nAllocationRequested != nAllocationRequested )
11311112
{
11321113
// we've been asked to grow memory to a size s.t. the index type can't address the requested amount of memory
1114+
Assert_Zero(UTL_RBTREE_TAG, "The index type can't address the requested amount of memory.");
11331115
return;
11341116
}
11351117
while ( nNewAllocationCount < nAllocationRequested )
@@ -1142,9 +1124,14 @@ methodmap UtlRBTree < AddressBase {
11421124
this.m_nAllocationCount += nNewAllocationCount;
11431125

11441126
if (this.m_pMemory) {
1145-
this.m_pMemory = realloc(this.m_pMemory, this.m_nAllocationCount * size);
1127+
// do not load or set first! check the allocated memory.
1128+
any p = realloc(this.m_pMemory, this.m_nAllocationCount * size);
1129+
Assert(!p ? 0 : 1, UTL_RBTREE_TAG, "realloc failed");
1130+
this.m_pMemory = p;
11461131
} else {
1147-
this.m_pMemory = malloc(this.m_nAllocationCount * size);
1132+
any p = malloc(this.m_nAllocationCount * size);
1133+
Assert(!p ? 0 : 1, UTL_RBTREE_TAG, "malloc failed");
1134+
this.m_pMemory = p;
11481135
}
11491136
}
11501137

@@ -1154,26 +1141,19 @@ methodmap UtlRBTree < AddressBase {
11541141

11551142
// Nothing in the free list; add.
11561143
if (this.m_FirstFree != UtlRBTree.InvalidIndex()) {
1157-
if (!(this.IsIdxValid(this.m_LastAlloc) || this.m_NumElements == 0))
1158-
ThrowError("Invalid LastAlloc (%d) or NumElements (%d).", this.m_LastAlloc, this.m_NumElements);
1159-
1144+
Assert((this.IsIdxValid(this.m_LastAlloc) || this.m_NumElements == 0), UTL_RBTREE_TAG, "Invalid LastAlloc (%d) or NumElements (%d).", this.m_LastAlloc, this.m_NumElements)
11601145
int it = this.IsIdxValid(this.m_LastAlloc) ? (this.IsIdxValid(this.m_LastAlloc + 1) ? this.m_LastAlloc + 1 : UtlRBTree.InvalidIndex()) : (this.IsIdxValid(0) ? 0 : UtlRBTree.InvalidIndex());
11611146

11621147
if (!this.IsIdxValid(it)) {
11631148
this.Grow(_, size);
11641149

11651150
it = this.IsIdxValid(this.m_LastAlloc) ? (this.IsIdxValid(this.m_LastAlloc + 1) ? this.m_LastAlloc + 1 : UtlRBTree.InvalidIndex()) : (this.IsIdxValid(0) ? 0 : UtlRBTree.InvalidIndex());
1166-
if (!this.IsIdxValid(it)) {
1167-
// obviousely we should crash the server at this time, othwerwise this will cause undefined behavior!
1168-
ThrowError("CUtlRBTree overflow!");
1169-
}
1151+
Assert(this.IsIdxValid(it), UTL_RBTREE_TAG, "CUtlRBTree overflow!");
11701152
}
11711153

11721154
this.m_LastAlloc = it;
11731155
elem = this.m_LastAlloc;
1174-
if (!this.IsIdxValid(this.m_LastAlloc)) {
1175-
ThrowError("CUtlRBTree overflow!");
1176-
}
1156+
Assert(this.IsIdxValid(this.m_LastAlloc), UTL_RBTREE_TAG, "CUtlRBTree overflow!");
11771157
} else {
11781158
elem = this.m_FirstFree;
11791159
this.m_FirstFree = this.RightChild(this.m_FirstFree, size);
@@ -1187,10 +1167,7 @@ methodmap UtlRBTree < AddressBase {
11871167
}
11881168

11891169
public void FreeNode(int i, int size = 4, UtlRBTreeDestructorFunc destructor) {
1190-
if (! (this.IsValidIndex(i, size) && (i != UtlRBTree.InvalidIndex())) ) {
1191-
ThrowError("Param index (%d) invalid.", i);
1192-
return;
1193-
}
1170+
Assert((this.IsValidIndex(i, size) && (i != UtlRBTree.InvalidIndex())), UTL_RBTREE_TAG, "Param index (%d) invalid.", i);
11941171

11951172
this.Destruct(i, size, destructor);
11961173
this.SetLeftChild(i, i, size);
@@ -1202,7 +1179,7 @@ methodmap UtlRBTree < AddressBase {
12021179
int i = this.NewNode(size, constructor);
12031180
this.LinkToParent(i, parent, leftchild, size);
12041181
++this.m_NumElements;
1205-
1182+
Assert(this.IsValid(), UTL_RBTREE_TAG, "Invalid tree after insert.");
12061183
return i;
12071184
}
12081185

@@ -1211,6 +1188,7 @@ methodmap UtlRBTree < AddressBase {
12111188
bool leftchild = false;
12121189
this.FindInsertionPosition(insert, parent, leftchild, size);
12131190
int newNode = this.InsertAt(parent, leftchild, size, constructor);
1191+
12141192
// CopyConstruct( &Element( newNode ), insert );
12151193
return newNode;
12161194
}
@@ -1248,6 +1226,8 @@ methodmap UtlRBTree < AddressBase {
12481226

12491227
this.FreeNode(elem, size, destructor);
12501228
--this.m_NumElements;
1229+
1230+
Assert(this.IsValid(), UTL_RBTREE_TAG, "Invalid tree after remove.");
12511231
}
12521232
}
12531233

@@ -1266,18 +1246,11 @@ methodmap UtlRBTree < AddressBase {
12661246
// valid elements for the multilist case (since we don't have all elements
12671247
// connected to each other in a list).
12681248

1249+
12691250
if (this.m_LastAlloc == UtlRBTree.InvalidIndex()) {
1270-
if (this.m_Root == UtlRBTree.InvalidIndex()) {
1271-
ThrowError("No elements to remove.");
1272-
}
1273-
1274-
if (this.m_FirstFree == UtlRBTree.InvalidIndex()) {
1275-
ThrowError("No free elements to remove.");
1276-
}
1277-
1278-
if (this.m_NumElements == 0) {
1279-
ThrowError("No elements to remove.");
1280-
}
1251+
Assert(this.m_Root == UtlRBTree.InvalidIndex(), UTL_RBTREE_TAG, "No elements to remove. m_Root = %d", this.m_Root);
1252+
Assert(this.m_FirstFree == UtlRBTree.InvalidIndex(), UTL_RBTREE_TAG, "No free elements to remove. m_FirstFree = %d", this.m_FirstFree);
1253+
Assert(this.m_NumElements == 0, UTL_RBTREE_TAG, "No elements to remove. m_NumElements = %d", this.m_NumElements);
12811254

12821255
return;
12831256
}
@@ -1304,6 +1277,8 @@ methodmap UtlRBTree < AddressBase {
13041277
this.m_LastAlloc = UtlRBTree.InvalidIndex();
13051278
this.m_FirstFree = UtlRBTree.InvalidIndex();
13061279
this.m_NumElements = 0;
1280+
1281+
Assert(this.IsValid(), UTL_RBTREE_TAG, "Invalid tree after remove all.");
13071282
}
13081283

13091284
public void Purge__(int size = 4, UtlRBTreeDestructorFunc destructor) {

0 commit comments

Comments
 (0)