RadixMap.h 20 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039
  1. #pragma once
  2. #include "BeefySysLib/Common.h"
  3. #include "BeefySysLib/util/BumpAllocator.h"
  4. NS_BF_BEGIN
  5. extern int sRadixMapCount;
  6. extern int sRootSize;
  7. extern int sMidSize;
  8. extern int sLeafSize;
  9. template <typename T>
  10. class RadixMap32
  11. {
  12. public:
  13. // Put 32 entries in the root and (2^BITS)/32 entries in each leaf.
  14. static const int BLOCK_SHIFT = 8;
  15. static const int BITS = 32 - BLOCK_SHIFT;
  16. static const int ROOT_BITS = 12;
  17. static const int ROOT_LENGTH = 1 << ROOT_BITS;
  18. static const int LEAF_BITS = BITS - ROOT_BITS;
  19. static const int LEAF_LENGTH = 1 << LEAF_BITS;
  20. struct Iterator
  21. {
  22. public:
  23. RadixMap32* mRadixMap;
  24. int mRootIdx;
  25. int mLeafIdx;
  26. public:
  27. Iterator()
  28. {
  29. }
  30. Iterator& operator++()
  31. {
  32. mLeafIdx++;
  33. while (true)
  34. {
  35. if (mLeafIdx == LEAF_LENGTH)
  36. {
  37. mLeafIdx = 0;
  38. mRootIdx++;
  39. while (true)
  40. {
  41. if (mRootIdx == ROOT_LENGTH)
  42. return *this;
  43. if (mRadixMap->mRoot[mRootIdx] != NULL)
  44. break;
  45. mRootIdx++;
  46. }
  47. }
  48. if (mRadixMap->mRoot[mRootIdx]->mValues[mLeafIdx] != NULL)
  49. return *this;
  50. mLeafIdx++;
  51. }
  52. return *this;
  53. }
  54. bool operator!=(const Iterator& itr) const
  55. {
  56. return (itr.mRootIdx != mRootIdx) || (itr.mLeafIdx != mLeafIdx);
  57. }
  58. bool operator==(const Iterator& itr) const
  59. {
  60. return (itr.mRootIdx == mRootIdx) && (itr.mLeafIdx == mLeafIdx);
  61. }
  62. T operator*()
  63. {
  64. return mRadixMap->mRoot[mRootIdx]->mValues[mLeafIdx];
  65. }
  66. };
  67. struct Leaf
  68. {
  69. T mValues[LEAF_LENGTH];
  70. T GetFirst(int startLeaf = 0)
  71. {
  72. for (int i = startLeaf; i < LEAF_LENGTH; i++)
  73. {
  74. auto value = mValues[i];
  75. if (value != NULL)
  76. {
  77. auto lowestValue = value;
  78. while (value != NULL)
  79. {
  80. if (value->mAddress < lowestValue->mAddress)
  81. lowestValue = value;
  82. value = value->mNext;
  83. }
  84. return lowestValue;
  85. }
  86. }
  87. return NULL;
  88. }
  89. };
  90. Leaf* mRoot[ROOT_LENGTH]; // Pointers to 32 child nodes
  91. typedef uint32 Number;
  92. public:
  93. RadixMap32()
  94. {
  95. memset(mRoot, 0, sizeof(mRoot));
  96. }
  97. ~RadixMap32()
  98. {
  99. int leafCount = 0;
  100. for (int i = 0; i < ROOT_LENGTH; i++)
  101. {
  102. if (mRoot[i] != NULL)
  103. {
  104. leafCount++;
  105. delete mRoot[i];
  106. }
  107. }
  108. }
  109. Iterator begin()
  110. {
  111. Iterator itr;
  112. itr.mRadixMap = this;
  113. itr.mRootIdx = -1;
  114. itr.mLeafIdx = LEAF_LENGTH - 1;
  115. return ++itr;
  116. }
  117. Iterator end()
  118. {
  119. Iterator itr;
  120. itr.mRadixMap = this;
  121. itr.mRootIdx = ROOT_LENGTH;
  122. itr.mLeafIdx = 0;
  123. return itr;
  124. }
  125. void Clear()
  126. {
  127. for (int i = 0; i < ROOT_LENGTH; i++)
  128. {
  129. if (mRoot[i] != NULL)
  130. {
  131. delete mRoot[i];
  132. mRoot[i] = NULL;
  133. }
  134. }
  135. }
  136. bool IsEmpty()
  137. {
  138. for (int i = 0; i < ROOT_LENGTH; i++)
  139. if (mRoot[i] != NULL)
  140. return false;
  141. return true;
  142. }
  143. T FindFirstLeafAt(Number addr)
  144. {
  145. int blockAddr = (int)(addr >> BLOCK_SHIFT);
  146. int i1 = (int)(blockAddr >> LEAF_BITS);
  147. int i2 = (int)(blockAddr & (LEAF_LENGTH - 1));
  148. Leaf* leaf = mRoot[i1];
  149. if (leaf == NULL)
  150. return NULL;
  151. return leaf->mValues[i2];
  152. }
  153. T Get(Number addr, int maxOffset = 0) const
  154. {
  155. int blockOffsetsLeft = (maxOffset + (1 << BLOCK_SHIFT) - 1) >> BLOCK_SHIFT;
  156. int blockAddr = (int)(addr >> BLOCK_SHIFT);
  157. int i1 = (int)(blockAddr >> LEAF_BITS);
  158. int i2 = (int)(blockAddr & (LEAF_LENGTH - 1));
  159. // Find closest entry to requested addr
  160. Leaf* leaf = mRoot[i1];
  161. int bestDist = 0x7FFFFFFF;
  162. T bestValue = NULL;
  163. if (leaf != NULL)
  164. {
  165. T curValue = leaf->mValues[i2];
  166. while (curValue != NULL)
  167. {
  168. if (addr >= curValue->mAddress)
  169. {
  170. int dist = (int)(addr - curValue->mAddress);
  171. if (dist < bestDist)
  172. {
  173. bestDist = dist;
  174. bestValue = curValue;
  175. }
  176. }
  177. curValue = curValue->mNext;
  178. }
  179. if ((bestValue != NULL) || (maxOffset == 0))
  180. return bestValue;
  181. }
  182. // Start searching for the highest address below the current i1:i2
  183. while (blockOffsetsLeft > 0)
  184. {
  185. blockOffsetsLeft--;
  186. i2--;
  187. if (i2 < 0)
  188. {
  189. i1--;
  190. if (i1 < 0)
  191. break;
  192. i2 = LEAF_LENGTH - 1;
  193. }
  194. leaf = mRoot[i1];
  195. if (leaf != NULL)
  196. {
  197. auto value = leaf->mValues[i2];
  198. if (value != NULL)
  199. {
  200. auto bestValue = value;
  201. while (value != NULL)
  202. {
  203. if (value->mAddress > bestValue->mAddress)
  204. bestValue = value;
  205. value = value->mNext;
  206. }
  207. return bestValue;
  208. }
  209. }
  210. }
  211. return NULL;
  212. }
  213. T GetNext(T value)
  214. {
  215. const Number blockAddr = value->mAddress >> BLOCK_SHIFT;
  216. const Number i1 = blockAddr >> LEAF_BITS;
  217. const Number i2 = blockAddr & (LEAF_LENGTH - 1);
  218. // Find closest entry to requested addr
  219. Leaf* leaf = mRoot[i1];
  220. if (leaf == NULL)
  221. return NULL;
  222. T curValue = leaf->mValues[i2];
  223. int bestDist = 0x7FFFFFFF;
  224. T bestValue = NULL;
  225. while (curValue != NULL)
  226. {
  227. if (curValue->mAddress > value->mAddress)
  228. {
  229. int dist = curValue->mAddress - value->mAddress;
  230. if (dist < bestDist)
  231. {
  232. bestDist = dist;
  233. bestValue = curValue;
  234. }
  235. }
  236. curValue = curValue->mNext;
  237. }
  238. if (bestValue != NULL)
  239. return bestValue;
  240. // Get lowest value in next leaf
  241. curValue = leaf->GetFirst(i2 + 1);
  242. while (curValue != NULL)
  243. {
  244. BF_ASSERT(curValue != value);
  245. if (curValue->mAddress > value->mAddress)
  246. {
  247. int dist = curValue->mAddress - value->mAddress;
  248. if (dist < bestDist)
  249. {
  250. bestDist = dist;
  251. bestValue = curValue;
  252. }
  253. }
  254. curValue = curValue->mNext;
  255. }
  256. if (bestValue != NULL)
  257. return bestValue;
  258. // Get first value in next root nodes
  259. for (int rootIdx = i1 + 1; rootIdx < ROOT_LENGTH; rootIdx++)
  260. {
  261. if (mRoot[rootIdx] != NULL)
  262. {
  263. curValue = mRoot[rootIdx]->GetFirst();
  264. if ((curValue != NULL) && (curValue->mAddress > value->mAddress))
  265. return curValue;
  266. }
  267. }
  268. return NULL;
  269. }
  270. T GetUnordered(Number addr, int maxReverseOffset) const
  271. {
  272. int blockOffsetsLeft = (maxReverseOffset + (1 << BLOCK_SHIFT) - 1) >> BLOCK_SHIFT;
  273. int blockAddr = (int)(addr >> BLOCK_SHIFT);
  274. int i1 = (int)(blockAddr >> LEAF_BITS);
  275. int i2 = (int)(blockAddr & (LEAF_LENGTH - 1));
  276. while (blockOffsetsLeft > 0)
  277. {
  278. Leaf* leaf = mRoot[i1];
  279. if (leaf != NULL)
  280. {
  281. auto value = leaf->mValues[i2];
  282. if (value != NULL)
  283. return value;
  284. i2++;
  285. if (i2 == LEAF_LENGTH)
  286. {
  287. i2 = 0;
  288. i1++;
  289. }
  290. blockOffsetsLeft--;
  291. }
  292. else
  293. {
  294. i1++;
  295. blockOffsetsLeft -= LEAF_LENGTH;
  296. }
  297. }
  298. return NULL;
  299. }
  300. void RemoveRange(Number startAddr, Number length)
  301. {
  302. Number endAddr = startAddr + length;
  303. int blockAddrStart = (int)(startAddr >> BLOCK_SHIFT);
  304. int blockAddrEnd = (int)(endAddr >> BLOCK_SHIFT);
  305. int i1Start = (int)(blockAddrStart >> LEAF_BITS);
  306. int i1End = (int)((blockAddrEnd - 1) >> LEAF_BITS) + 1;
  307. for (int i1 = i1Start; i1 < i1End; i1++)
  308. {
  309. Leaf* leaf = mRoot[i1];
  310. if (leaf == NULL)
  311. continue;
  312. int i2Start;
  313. int i2End;
  314. if (i1 == i1Start)
  315. {
  316. i2Start = (int)(blockAddrStart & (LEAF_LENGTH - 1));
  317. i2End = LEAF_LENGTH;
  318. }
  319. else if (i1 == i1End - 1)
  320. {
  321. i2Start = 0;
  322. i2End = (int)((blockAddrEnd - 1) & (LEAF_LENGTH - 1)) + 1;
  323. }
  324. else
  325. {
  326. i2Start = 0;
  327. i2End = LEAF_LENGTH;
  328. }
  329. for (int i2 = i2Start; i2 < i2End; i2++)
  330. {
  331. T curValue = leaf->mValues[i2];
  332. if (curValue == NULL)
  333. continue;
  334. T* nextPtr = &leaf->mValues[i2];
  335. while (curValue != NULL)
  336. {
  337. if ((curValue->mAddress >= startAddr) && (curValue->mAddress < endAddr))
  338. {
  339. // We're removing it implicitly
  340. }
  341. else
  342. {
  343. *nextPtr = curValue;
  344. nextPtr = &curValue->mNext;
  345. }
  346. curValue = curValue->mNext;
  347. }
  348. *nextPtr = NULL;
  349. }
  350. }
  351. }
  352. void Insert(T value)
  353. {
  354. const Number blockAddr = value->mAddress >> BLOCK_SHIFT;
  355. const Number i1 = blockAddr >> LEAF_BITS;
  356. const Number i2 = blockAddr & (LEAF_LENGTH - 1);
  357. Leaf* leaf = mRoot[i1];
  358. if (leaf == NULL)
  359. {
  360. leaf = new Leaf();
  361. sLeafSize += sizeof(Leaf);
  362. mRoot[i1] = leaf;
  363. }
  364. T prevValue = leaf->mValues[i2];
  365. mRoot[i1]->mValues[i2] = value;
  366. value->mNext = prevValue;
  367. }
  368. void Remove(T value)
  369. {
  370. const Number blockAddr = value->mAddress >> BLOCK_SHIFT;
  371. const Number i1 = blockAddr >> LEAF_BITS;
  372. const Number i2 = blockAddr & (LEAF_LENGTH - 1);
  373. // Find closest entry to requested addr
  374. Leaf* leaf = mRoot[i1];
  375. T prevValue = NULL;
  376. T curValue = leaf->mValues[i2];
  377. while (curValue != NULL)
  378. {
  379. if (curValue == value)
  380. {
  381. if (prevValue == NULL)
  382. leaf->mValues[i2] = curValue->mNext;
  383. else
  384. prevValue->mNext = curValue->mNext;
  385. curValue->mNext = NULL;
  386. return;
  387. }
  388. prevValue = curValue;
  389. curValue = curValue->mNext;
  390. }
  391. }
  392. };
  393. template <typename T>
  394. class RadixMap64
  395. {
  396. public:
  397. // Higher BLOCK_SHIFT causes more searches but reduces memory usage
  398. static const int BLOCK_SHIFT = 8;
  399. static const int BITS = 48 - BLOCK_SHIFT;
  400. static const int ROOT_BITS = 14;
  401. static const int ROOT_LENGTH = 1 << ROOT_BITS;
  402. static const int MID_BITS = 13;
  403. static const int MID_LENGTH = 1 << MID_BITS;
  404. static const int LEAF_BITS = BITS - ROOT_BITS - MID_BITS;
  405. static const int LEAF_LENGTH = 1 << LEAF_BITS;
  406. struct Iterator
  407. {
  408. public:
  409. RadixMap64* mRadixMap;
  410. int mRootIdx;
  411. int mMidIdx;
  412. int mLeafIdx;
  413. public:
  414. Iterator()
  415. {
  416. }
  417. Iterator& operator++()
  418. {
  419. mLeafIdx++;
  420. while (true)
  421. {
  422. if (mLeafIdx == LEAF_LENGTH)
  423. {
  424. mLeafIdx = 0;
  425. mMidIdx++;
  426. while (true)
  427. {
  428. if (mMidIdx == MID_LENGTH)
  429. {
  430. mMidIdx = 0;
  431. mRootIdx++;
  432. while (true)
  433. {
  434. if (mRootIdx == ROOT_LENGTH)
  435. return *this;
  436. if (mRadixMap->mRoot[mRootIdx] != NULL)
  437. break;
  438. mRootIdx++;
  439. }
  440. }
  441. if (mRadixMap->mRoot[mRootIdx]->mLeafs[mMidIdx] != NULL)
  442. break;
  443. mMidIdx++;
  444. }
  445. }
  446. if (mRadixMap->mRoot[mRootIdx]->mLeafs[mMidIdx]->mValues[mLeafIdx] != NULL)
  447. return *this;
  448. mLeafIdx++;
  449. }
  450. return *this;
  451. }
  452. bool operator!=(const Iterator& itr) const
  453. {
  454. return (itr.mRootIdx != mRootIdx) || (itr.mMidIdx != mMidIdx) || (itr.mLeafIdx != mLeafIdx);
  455. }
  456. bool operator==(const Iterator& itr) const
  457. {
  458. return (itr.mRootIdx == mRootIdx) && (itr.mMidIdx == mMidIdx) && (itr.mLeafIdx == mLeafIdx);
  459. }
  460. T operator*()
  461. {
  462. return mRadixMap->mRoot[mRootIdx]->mLeafs[mMidIdx]->mValues[mLeafIdx];
  463. }
  464. };
  465. struct Leaf
  466. {
  467. T mValues[LEAF_LENGTH];
  468. T GetFirst(int startLeaf = 0)
  469. {
  470. for (int i = startLeaf; i < LEAF_LENGTH; i++)
  471. {
  472. auto value = mValues[i];
  473. if (value != NULL)
  474. {
  475. auto lowestValue = value;
  476. while (value != NULL)
  477. {
  478. if (value->mAddress < lowestValue->mAddress)
  479. lowestValue = value;
  480. value = value->mNext;
  481. }
  482. return lowestValue;
  483. }
  484. }
  485. return NULL;
  486. }
  487. };
  488. struct Mid
  489. {
  490. Leaf* mLeafs[MID_LENGTH];
  491. ~Mid()
  492. {
  493. for (int i = 0; i < MID_LENGTH; i++)
  494. if (mLeafs[i] != NULL)
  495. delete mLeafs[i];
  496. }
  497. };
  498. Mid* mRoot[ROOT_LENGTH]; // Pointers to 32 child nodes
  499. typedef uint64 Number;
  500. int mAllocSize;
  501. public:
  502. RadixMap64()
  503. {
  504. memset(mRoot, 0, sizeof(mRoot));
  505. sRadixMapCount++;
  506. sRootSize += sizeof(RadixMap64);
  507. mAllocSize = sizeof(RadixMap64);
  508. }
  509. ~RadixMap64()
  510. {
  511. int leafCount = 0;
  512. for (int i = 0; i < ROOT_LENGTH; i++)
  513. {
  514. if (mRoot[i] != NULL)
  515. {
  516. leafCount++;
  517. delete mRoot[i];
  518. }
  519. }
  520. }
  521. Iterator begin()
  522. {
  523. Iterator itr;
  524. itr.mRadixMap = this;
  525. itr.mRootIdx = -1;
  526. itr.mMidIdx = MID_LENGTH - 1;
  527. itr.mLeafIdx = LEAF_LENGTH - 1;
  528. return ++itr;
  529. }
  530. Iterator end()
  531. {
  532. Iterator itr;
  533. itr.mRadixMap = this;
  534. itr.mRootIdx = ROOT_LENGTH;
  535. itr.mMidIdx = 0;
  536. itr.mLeafIdx = 0;
  537. return itr;
  538. }
  539. void Clear()
  540. {
  541. for (int i = 0; i < ROOT_LENGTH; i++)
  542. {
  543. if (mRoot[i] != NULL)
  544. {
  545. delete mRoot[i];
  546. mRoot[i] = NULL;
  547. }
  548. }
  549. }
  550. bool IsEmpty()
  551. {
  552. for (int i = 0; i < ROOT_LENGTH; i++)
  553. if (mRoot[i] != NULL)
  554. return false;
  555. return true;
  556. }
  557. T FindFirstLeafAt(Number addr)
  558. {
  559. if ((addr & 0xFFFF000000000000LL) != 0)
  560. {
  561. return T();
  562. }
  563. int64 blockAddr = (int64)(addr >> BLOCK_SHIFT);
  564. int i1 = (int)(blockAddr >> (LEAF_BITS + MID_BITS));
  565. int i2 = (int)((blockAddr >> LEAF_BITS) & (MID_LENGTH - 1));
  566. int i3 = (int)(blockAddr & (LEAF_LENGTH - 1));
  567. Mid* mid = mRoot[i1];
  568. if (mid == NULL)
  569. return NULL;
  570. Leaf* leaf = mid->mLeafs[i2];
  571. if (leaf == NULL)
  572. return NULL;
  573. return leaf->mValues[i3];
  574. }
  575. T Get(Number addr, int maxOffset = 0) const
  576. {
  577. if ((addr & 0xFFFF000000000000LL) != 0)
  578. {
  579. // On overflow return default
  580. return T();
  581. }
  582. int blockOffsetsLeft = (maxOffset + (1 << BLOCK_SHIFT) - 1) >> BLOCK_SHIFT;
  583. int64 blockAddr = (int64)(addr >> BLOCK_SHIFT);
  584. int i1 = (int)(blockAddr >> (LEAF_BITS + MID_BITS));
  585. int i2 = (int)((blockAddr >> LEAF_BITS) & (MID_LENGTH - 1));
  586. int i3 = (int)(blockAddr & (LEAF_LENGTH - 1));
  587. // Find closest entry to requested addr
  588. Mid* mid = mRoot[i1];
  589. Leaf* leaf = NULL;
  590. if (mid != NULL)
  591. leaf = mid->mLeafs[i2];
  592. int bestDist = 0x7FFFFFFF;
  593. T bestValue = NULL;
  594. if (leaf != NULL)
  595. {
  596. T curValue = leaf->mValues[i3];
  597. while (curValue != NULL)
  598. {
  599. if (addr >= curValue->mAddress)
  600. {
  601. int dist = (int)(addr - curValue->mAddress);
  602. if (dist < bestDist)
  603. {
  604. bestDist = dist;
  605. bestValue = curValue;
  606. }
  607. }
  608. curValue = curValue->mNext;
  609. }
  610. if ((bestValue != NULL) || (maxOffset == 0))
  611. return bestValue;
  612. }
  613. // Start searching for the highest address below the current i1:i2
  614. while (blockOffsetsLeft > 0)
  615. {
  616. blockOffsetsLeft--;
  617. i3--;
  618. if (i3 < 0)
  619. {
  620. i2--;
  621. if (i2 < 0)
  622. {
  623. i1--;
  624. if (i1 < 0)
  625. break;
  626. i2 = MID_LENGTH - 1;
  627. }
  628. i3 = LEAF_LENGTH - 1;
  629. }
  630. mid = mRoot[i1];
  631. if (mid != NULL)
  632. {
  633. leaf = mid->mLeafs[i2];
  634. if (leaf != NULL)
  635. {
  636. auto value = leaf->mValues[i3];
  637. if (value != NULL)
  638. {
  639. auto bestValue = value;
  640. while (value != NULL)
  641. {
  642. if (value->mAddress > bestValue->mAddress)
  643. bestValue = value;
  644. value = value->mNext;
  645. }
  646. return bestValue;
  647. }
  648. }
  649. }
  650. }
  651. return NULL;
  652. }
  653. T GetNext(T value)
  654. {
  655. const Number blockAddr = value->mAddress >> BLOCK_SHIFT;
  656. int i1 = (int)(blockAddr >> (LEAF_BITS + MID_BITS));
  657. int i2 = (int)((blockAddr >> LEAF_BITS) & (MID_LENGTH - 1));
  658. int i3 = (int)(blockAddr & (LEAF_LENGTH - 1));
  659. // Find closest entry to requested addr
  660. Mid* mid = mRoot[i1];
  661. if (mid == NULL)
  662. return NULL;
  663. Leaf* leaf = mid->mLeafs[i2];
  664. if (leaf == NULL)
  665. return NULL;
  666. T curValue = leaf->mValues[i3];
  667. int bestDist = 0x7FFFFFFF;
  668. T bestValue = NULL;
  669. while (curValue != NULL)
  670. {
  671. if (curValue->mAddress > value->mAddress)
  672. {
  673. int dist = curValue->mAddress - value->mAddress;
  674. if (dist < bestDist)
  675. {
  676. bestDist = dist;
  677. bestValue = curValue;
  678. }
  679. }
  680. curValue = curValue->mNext;
  681. }
  682. if (bestValue != NULL)
  683. return bestValue;
  684. // Get lowest value in next leaf
  685. curValue = leaf->GetFirst(i2 + 1);
  686. while (curValue != NULL)
  687. {
  688. BF_ASSERT(curValue != value);
  689. if (curValue->mAddress > value->mAddress)
  690. {
  691. int dist = curValue->mAddress - value->mAddress;
  692. if (dist < bestDist)
  693. {
  694. bestDist = dist;
  695. bestValue = curValue;
  696. }
  697. }
  698. curValue = curValue->mNext;
  699. }
  700. if (bestValue != NULL)
  701. return bestValue;
  702. for (int midIdx = i2 + 1; midIdx < MID_LENGTH; midIdx++)
  703. {
  704. if (mid->mLeafs[midIdx] != NULL)
  705. {
  706. curValue = mid->mLeafs[midIdx]->GetFirst();
  707. if ((curValue != NULL) && (curValue->mAddress > value->mAddress))
  708. return curValue;
  709. }
  710. }
  711. // Get first value in next root nodes
  712. for (int rootIdx = i1 + 1; rootIdx < ROOT_LENGTH; rootIdx++)
  713. {
  714. if (mRoot[rootIdx] != NULL)
  715. {
  716. mid = mRoot[rootIdx];
  717. for (int midIdx = 0; midIdx < MID_LENGTH; midIdx++)
  718. {
  719. curValue = mid->mLeafs[midIdx]->GetFirst();
  720. if ((curValue != NULL) && (curValue->mAddress > value->mAddress))
  721. return curValue;
  722. }
  723. }
  724. }
  725. return NULL;
  726. }
  727. void RemoveRange(Number startAddr, Number length)
  728. {
  729. Number endAddr = BF_MIN(startAddr + length, 0x0001000000000000LL);
  730. startAddr = BF_MIN(startAddr, 0x0000FFFFFFFFFFFFLL);
  731. Number blockAddrStart = startAddr >> BLOCK_SHIFT;
  732. Number blockAddrEnd = endAddr >> BLOCK_SHIFT;
  733. int i1Start = (int)(blockAddrStart >> (LEAF_BITS + MID_BITS));
  734. int i1End = (int)((blockAddrEnd - 1) >> (LEAF_BITS + MID_BITS)) + 1;
  735. for (int i1 = i1Start; i1 < i1End; i1++)
  736. {
  737. Mid* mid = mRoot[i1];
  738. if (mid == NULL)
  739. continue;
  740. int i2Start;
  741. int i2End;
  742. if (i1 == i1Start)
  743. {
  744. i2Start = (int)((blockAddrStart >> LEAF_BITS) & (MID_LENGTH - 1));
  745. i2End = MID_LENGTH;
  746. }
  747. else if (i1 == i1End)
  748. {
  749. i2Start = 0;
  750. i2End = (int)(((blockAddrEnd - 1) >> LEAF_BITS) & (MID_LENGTH - 1)) + 1;
  751. }
  752. else
  753. {
  754. i2Start = 0;
  755. i2End = MID_LENGTH;
  756. }
  757. for (int i2 = i2Start; i2 < i2End; i2++)
  758. {
  759. Leaf* leaf = mid->mLeafs[i2];
  760. if (leaf == NULL)
  761. continue;
  762. int i3Start;
  763. int i3End;
  764. if ((i1 == i1Start) && (i2 == i2Start))
  765. {
  766. i3Start = (int)(blockAddrStart & (LEAF_LENGTH - 1));
  767. i3End = LEAF_LENGTH;
  768. }
  769. else if ((i1 == i1End - 1) && (i2 == i2End - 1))
  770. {
  771. i3Start = 0;
  772. i3End = (int)((blockAddrEnd - 1) & (LEAF_LENGTH - 1)) + 1;
  773. }
  774. else
  775. {
  776. i3Start = 0;
  777. i3End = LEAF_LENGTH;
  778. }
  779. for (int i3 = i3Start; i3 < i3End; i3++)
  780. {
  781. T curValue = leaf->mValues[i3];
  782. if (curValue == NULL)
  783. continue;
  784. T* nextPtr = &leaf->mValues[i3];
  785. while (curValue != NULL)
  786. {
  787. if ((curValue->mAddress >= startAddr) && (curValue->mAddress < endAddr))
  788. {
  789. // We're removing it implicitly
  790. }
  791. else
  792. {
  793. *nextPtr = curValue;
  794. nextPtr = &curValue->mNext;
  795. }
  796. curValue = curValue->mNext;
  797. }
  798. *nextPtr = NULL;
  799. }
  800. }
  801. }
  802. }
  803. T GetUnordered(Number addr, int maxOffset) const
  804. {
  805. if ((addr & 0xFFFF000000000000LL) != 0)
  806. {
  807. return T();
  808. }
  809. int blockOffsetsLeft = (maxOffset + (1 << BLOCK_SHIFT) - 1) >> BLOCK_SHIFT;
  810. const Number blockAddr = addr >> BLOCK_SHIFT;
  811. int i1 = (int)(blockAddr >> (LEAF_BITS + MID_BITS));
  812. int i2 = (int)((blockAddr >> LEAF_BITS) & (MID_LENGTH - 1));
  813. int i3 = (int)(blockAddr & (LEAF_LENGTH - 1));
  814. while (blockOffsetsLeft > 0)
  815. {
  816. Mid* mid = mRoot[i1];
  817. if (mid != NULL)
  818. {
  819. Leaf* leaf = mid->mLeafs[i2];
  820. if (leaf != NULL)
  821. {
  822. auto value = leaf->mValues[i3];
  823. if (value != NULL)
  824. return value;
  825. i3++;
  826. if (i3 == LEAF_LENGTH)
  827. {
  828. i3 = 0;
  829. i2++;
  830. if (i2 == MID_LENGTH)
  831. {
  832. i2 = 0;
  833. i1++;
  834. }
  835. }
  836. blockOffsetsLeft--;
  837. }
  838. else
  839. {
  840. i2++;
  841. blockOffsetsLeft -= LEAF_LENGTH;
  842. }
  843. }
  844. else
  845. {
  846. // MID_LENGTH * LEAF_LENGTH is > 4GB, so a mRoot[i1] being NULL indicates no data
  847. return NULL;
  848. }
  849. }
  850. return NULL;
  851. }
  852. T GetNextUnordered(T value, int maxOffset) const
  853. {
  854. if (value->mNext != NULL)
  855. return value->mNext;
  856. return GetUnordered(value->mAddress + 1, maxOffset - 1);
  857. }
  858. void Insert(T value)
  859. {
  860. BF_ASSERT((value->mAddress & 0xFFFF000000000000LL) == 0);
  861. const Number blockAddr = value->mAddress >> BLOCK_SHIFT;
  862. int i1 = (int)(blockAddr >> (LEAF_BITS + MID_BITS));
  863. int i2 = (int)((blockAddr >> LEAF_BITS) & (MID_LENGTH - 1));
  864. int i3 = (int)(blockAddr & (LEAF_LENGTH - 1));
  865. BF_ASSERT((i1 >= 0) && (i1 < ROOT_LENGTH));
  866. Mid* mid = mRoot[i1];
  867. if (mid == NULL)
  868. {
  869. mid = new Mid();
  870. mAllocSize += sizeof(Mid);
  871. sMidSize += sizeof(Mid);
  872. mRoot[i1] = mid;
  873. }
  874. BF_ASSERT((i2 >= 0) && (i2 < MID_LENGTH));
  875. Leaf* leaf = mid->mLeafs[i2];
  876. if (leaf == NULL)
  877. {
  878. leaf = new Leaf();
  879. sLeafSize += sizeof(Leaf);
  880. mAllocSize += sizeof(Leaf);
  881. mid->mLeafs[i2] = leaf;
  882. }
  883. T prevValue = leaf->mValues[i3];
  884. leaf->mValues[i3] = value;
  885. value->mNext = prevValue;
  886. }
  887. void Remove(T value)
  888. {
  889. const Number blockAddr = value->mAddress >> BLOCK_SHIFT;
  890. int i1 = (int)(blockAddr >> (LEAF_BITS + MID_BITS));
  891. int i2 = (int)((blockAddr >> LEAF_BITS) & (MID_LENGTH - 1));
  892. int i3 = (int)(blockAddr & (LEAF_LENGTH - 1));
  893. // Find closest entry to requested addr
  894. Mid* mid = mRoot[i1];
  895. Leaf* leaf = mid->mLeafs[i2];
  896. T prevValue = NULL;
  897. T curValue = leaf->mValues[i3];
  898. while (curValue != NULL)
  899. {
  900. if (curValue == value)
  901. {
  902. if (prevValue == NULL)
  903. leaf->mValues[i3] = curValue->mNext;
  904. else
  905. prevValue->mNext = curValue->mNext;
  906. curValue->mNext = NULL;
  907. return;
  908. }
  909. prevValue = curValue;
  910. curValue = curValue->mNext;
  911. }
  912. }
  913. };
  914. NS_BF_END