Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

155 lines
2.3 KiB

  1. //-----------------------------------------------------------------------------
  2. // File: bidirlookup.h
  3. //
  4. // Desc: This file implements a bi-directional map class template. It does
  5. // this by using two CMap classes that each handles the look up in one
  6. // direction.
  7. //
  8. // Copyright (C) 1999-2000 Microsoft Corporation. All Rights Reserved.
  9. //-----------------------------------------------------------------------------
  10. #ifndef __BIDIRLOOKUP_H__
  11. #define __BIDIRLOOKUP_H__
  12. #ifndef NULL
  13. #define NULL 0
  14. #endif
  15. template <class L, class R>
  16. class bidirlookup
  17. {
  18. private:
  19. CMap<L, const L &, R, const R &> l2r;
  20. CMap<R, const R &, L, const L &> r2l;
  21. bool addnode(const L &l, const R &r)
  22. {
  23. l2r.SetAt(l, r);
  24. r2l.SetAt(r, l);
  25. return true;
  26. }
  27. public:
  28. void clear()
  29. {
  30. l2r.RemoveAll();
  31. r2l.RemoveAll();
  32. }
  33. bidirlookup() {}
  34. ~bidirlookup() {clear();}
  35. bool add(const L &l, const R &r)
  36. {
  37. L tl;
  38. R tr;
  39. if (l2r.Lookup(l, tr) || r2l.Lookup(r, tl))
  40. return false;
  41. return addnode(l, r);
  42. }
  43. bool getleft(L &l, const R &r)
  44. {
  45. return r2l.Lookup(r, l) ? true : false;
  46. }
  47. bool getright(const L &l, R &r)
  48. {
  49. return l2r.Lookup(l, r) ? true : false;
  50. }
  51. };
  52. #if 0
  53. template <class L, class R>
  54. class bidirlookup
  55. {
  56. private:
  57. struct node {
  58. node(const L &a, const R &b) : l(a), r(b), next(NULL) {}
  59. node *next;
  60. L l;
  61. R r;
  62. } *head;
  63. bool addnode(const L &l, const R &r)
  64. {
  65. node *old = head;
  66. head = new node(l, r);
  67. if (!head)
  68. return false;
  69. head->next = old;
  70. return true;
  71. }
  72. node *getleftnode(const L &l)
  73. {
  74. for (node *on = head; on; on = on->next)
  75. if (on->l == l)
  76. return on;
  77. return NULL;
  78. }
  79. node *getrightnode(const R &r)
  80. {
  81. for (node *on = head; on; on = on->next)
  82. if (on->r == r)
  83. return on;
  84. return NULL;
  85. }
  86. public:
  87. void clear()
  88. {
  89. while (head)
  90. {
  91. node *next = head->next;
  92. delete head;
  93. head = next;
  94. }
  95. }
  96. bidirlookup() : head(NULL) {}
  97. ~bidirlookup() {clear();}
  98. bool add(const L &l, const R &r)
  99. {
  100. if (getleftnode(l) || getrightnode(r))
  101. return false;
  102. return addnode(l, r);
  103. }
  104. bool getleft(L &l, const R &r)
  105. {
  106. node *n = getrightnode(r);
  107. if (!n)
  108. return false;
  109. l = n->l;
  110. return true;
  111. }
  112. bool getright(const L &l, R &r)
  113. {
  114. node *n = getleftnode(l);
  115. if (!n)
  116. return false;
  117. r = n->r;
  118. return true;
  119. }
  120. };
  121. #endif
  122. #endif //__BIDIRLOOKUP_H__