]>
git.saurik.com Git - apple/mdnsresponder.git/blob - mDNSMacOSX/BonjourTop/source/LLRBTree.cpp
5 // Created by Terrin Eager on 7/9/12.
35 /*********************
36 BJ_BOOL isBST(CRBNode* pRecord, BJ_UINT64 min, BJ_UINT64 max);
37 BJ_BOOL is234(CRBNode* pRecord);
38 BJ_BOOL isBalanced(CLLRBTree* pCache);
39 BJ_BOOL isBalancedNode(CRBNode* pRecord, int black);
41 BJ_BOOL check(CLLRBTree* pCache)
42 { // Is this tree a red-black tree?
43 BJ_BOOL bBST = isBST(pCache->GetRoot(),pCache->minRecord(pCache->GetRoot())->nKey,pCache->maxRecord(pCache->GetRoot())->nKey);
44 BJ_BOOL b234 = is234(pCache->GetRoot());
45 BJ_BOOL bisBalanced = isBalanced(pCache);
47 printf("Bst=%d,234=%d, Balanced=%d",bBST,b234,bisBalanced);
49 return bBST && b234 && bisBalanced;
53 BJ_BOOL isBST(CRBNode* pRecord, BJ_UINT64 min, BJ_UINT64 max)
54 { // Are all the values in the BST rooted at x between min and max,
55 // and does the same property hold for both subtrees?
56 if (pRecord == NULL) return 1;
57 if ((pRecord->nKey > min) || (max > pRecord->nKey)) return 0;
58 return isBST(pRecord->m_rbLeft, min, pRecord->nKey) && isBST(pRecord->m_rbRight, pRecord->nKey, max);
60 BJ_BOOL is234(CRBNode* pRecord)
61 { // Does the tree have no red right links, and at most two (left)
62 // red links in a row on any path?
63 if (pRecord == NULL) return 1;
64 if (IsRed(pRecord->m_rbRight)) return 0;
66 if (IsRed(pRecord->m_rbLeft))
67 if (IsRed(pRecord->m_rbLeft->m_rbLeft)) return 0;
68 return is234(pRecord->m_rbLeft) && is234(pRecord->m_rbRight);
71 BJ_BOOL isBalanced(CLLRBTree* pCache)
72 { // Do all paths from root to leaf have same number of black edges?
73 int black = 0; // number of black links on path from root to min
74 CRBNode* pRecord = pCache->m_Root;
75 while (pRecord != NULL)
77 if (!IsRed(pRecord)) black++;
78 pRecord = pRecord->m_rbLeft;
80 return isBalancedNode(pCache->root, black);
83 BJ_BOOL isBalancedNode(CRBNode* pRecord, int black)
84 { // Does every path from the root to a leaf have the given number
86 if (pRecord == NULL && black == 0) return 1;
87 else if (pRecord == NULL && black != 0) return 0;
88 if (!IsRed(pRecord)) black--;
89 return isBalancedNode(pRecord->m_rbLeft, black) && isBalancedNode(pRecord->m_rbRight, black);
94 // sample code for testing
95 void CStringNode_test()
100 char DummyData[] = {'a','b','d','x'};
102 CStringNode* pRecord;
104 while (i++ < sizeof(DummyData))
107 pRecord = (CStringNode*)Cache.FindwithAddRecord(&i);
109 pRecord->m_Value[0] = DummyData[i];
113 pRecord = (CStringNode*)Cache.Find(&i);
122 // float nSaveCPU =0;
130 long starttime = clock();
131 float elapsedtime = 0;
132 CStringNode* pRecord;
134 // nSaveCPU = getCPUtime();
135 while (i++ < 1000000)
137 pRecord = (CStringNode*) Cache.FindwithAddRecord(&i);
139 memccpy(pRecord->m_Value, "test",4, 1);
141 // snprintf(pRecord->m_Value,sizeof(pRecord->m_Value),"%llx",key.m_nKey);
143 elapsedtime = clock() - starttime;
144 elapsedtime /= CLOCKS_PER_SEC;
146 // elapsedtime = getCPUtime() - nSaveCPU;
148 printf("Test elapsed time %f, check = %d\n",elapsedtime,0);