/src/lib/storage/internal/abstract_avl_dictionary_node.e

http://github.com/tybor/Liberty · Specman e · 158 lines · 115 code · 14 blank · 29 comment · 11 complexity · c8e06423ecc006fa53ff75fb75997049 MD5 · raw file

  1. -- This file is part of a Liberty Eiffel library.
  2. -- See the full copyright at the end.
  3. --
  4. deferred class ABSTRACT_AVL_DICTIONARY_NODE[V_, K_]
  5. --
  6. -- Auxiliary class to implement AVL_DICTIONARY.
  7. --
  8. inherit
  9. AVL_TREE_NODE[K_]
  10. rename
  11. item as key,
  12. set_item as set_key,
  13. safe_equal as safe_equal_key
  14. redefine
  15. recycle
  16. end
  17. ANY_AVL_DICTIONARY_NODE
  18. feature {ABSTRACT_AVL_DICTIONARY, ABSTRACT_AVL_DICTIONARY_NODE, ITERATOR_ON_AVL_DICTIONARY_ITEMS, ITERATOR_ON_AVL_DICTIONARY}
  19. safe_equal_value: SAFE_EQUAL[V_]
  20. value: V_
  21. set_value (v: like value)
  22. do
  23. value := v
  24. ensure
  25. value = v
  26. end
  27. fast_at (k: like key): like Current
  28. -- Is element `e' in the tree?
  29. do
  30. if key = k then
  31. Result := Current
  32. elseif safe_equal_key.test(key, k) then
  33. -- because otherwise there would be an infinite recursion
  34. --Result := Void
  35. elseif ordered(k, key) then
  36. if left /= Void then
  37. Result := left.fast_at(k)
  38. end
  39. else
  40. if right /= Void then
  41. Result := right.fast_at(k)
  42. end
  43. end
  44. end
  45. occurrences (v: V_): INTEGER
  46. do
  47. Result := occurrences_(v, 0)
  48. end
  49. fast_occurrences (v: V_): INTEGER
  50. do
  51. Result := fast_occurrences_(v, 0)
  52. end
  53. key_at (v: V_): K_
  54. do
  55. if safe_equal_value.test(v, value) then
  56. Result := key
  57. elseif left /= Void then
  58. Result := left.key_at(v)
  59. elseif right /= Void then
  60. Result := right.key_at(v)
  61. end
  62. end
  63. fast_key_at (v: V_): K_
  64. do
  65. if v = value then
  66. Result := key
  67. elseif left /= Void then
  68. Result := left.fast_key_at(v)
  69. elseif right /= Void then
  70. Result := right.fast_key_at(v)
  71. end
  72. end
  73. feature {ABSTRACT_AVL_DICTIONARY_NODE}
  74. occurrences_ (v: V_; cnt: INTEGER): INTEGER
  75. do
  76. Result := cnt
  77. if safe_equal_value.test(v, value) then
  78. Result := Result + 1
  79. end
  80. if left /= Void then
  81. Result := left.occurrences_(v, Result)
  82. end
  83. if right /= Void then
  84. Result := right.occurrences_(v, Result)
  85. end
  86. ensure
  87. Result >= cnt
  88. end
  89. fast_occurrences_ (v: V_; cnt: INTEGER): INTEGER
  90. do
  91. Result := cnt
  92. if v = value then
  93. Result := Result + 1
  94. end
  95. if left /= Void then
  96. Result := left.fast_occurrences_(v, Result)
  97. end
  98. if right /= Void then
  99. Result := right.fast_occurrences_(v, Result)
  100. end
  101. ensure
  102. Result >= cnt
  103. end
  104. feature {ABSTRACT_AVL_DICTIONARY}
  105. set (v: like value; k: like key)
  106. do
  107. set_balance(balanced)
  108. left := Void
  109. right := Void
  110. set_value(v)
  111. set_key(k)
  112. ensure
  113. value = v
  114. key = k
  115. end
  116. feature {RECYCLING_POOL}
  117. recycle
  118. local
  119. v: V_
  120. do
  121. Precursor
  122. value := v
  123. end
  124. end -- class ABSTRACT_AVL_DICTIONARY_NODE
  125. --
  126. -- Copyright (C) 2009-2017: by all the people cited in the AUTHORS file.
  127. --
  128. -- Permission is hereby granted, free of charge, to any person obtaining a copy
  129. -- of this software and associated documentation files (the "Software"), to deal
  130. -- in the Software without restriction, including without limitation the rights
  131. -- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  132. -- copies of the Software, and to permit persons to whom the Software is
  133. -- furnished to do so, subject to the following conditions:
  134. --
  135. -- The above copyright notice and this permission notice shall be included in
  136. -- all copies or substantial portions of the Software.
  137. --
  138. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  139. -- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  140. -- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  141. -- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  142. -- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  143. -- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  144. -- THE SOFTWARE.