/src/lib/storage/dictionary.e

http://github.com/tybor/Liberty · Specman e · 206 lines · 116 code · 18 blank · 72 comment · 1 complexity · 47249c0f7343a50d3c229d9f79b50c95 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 DICTIONARY[V_, K_]
  5. --
  6. -- Associative memory. Values of type `V_' are stored using Keys of type `K_'.
  7. -- To make a comparison with the well known ARRAY class, with a DICTIONARY, index used are not only
  8. -- INTEGER, you can use for example a STRING to access to your information.
  9. --
  10. -- Well known implementations, see HASHED_DICTIONARY, AVL_DICTIONARY and ARRAY_DICTIONARY.
  11. --
  12. -- See also BIJECTIVE_DICTIONARY class.
  13. --
  14. inherit
  15. MAP[V_, K_]
  16. feature {ANY} -- Adding:
  17. put (v: V_; k: K_) assign at
  18. -- Change some existing entry or `add' the new one. If there is as yet no key `k' in the dictionary,
  19. -- enter it with item `v'. Otherwise overwrite the item associated with key `k'.
  20. -- As the `put' procedure actually uses `is_equal', you may consider to use `fast_put' for expanded
  21. -- objects as well while trying to get the very best performances.
  22. --
  23. -- See also `fast_put', `add'.
  24. require
  25. k /= Void
  26. deferred
  27. ensure
  28. v = at(k)
  29. end
  30. fast_put (v: V_; k: K_) assign fast_at
  31. -- Same job as `put', but uses basic `=' for comparison.
  32. -- If you are sure that `k' is not an existing entry, please consider using `add'
  33. -- to get very best performances.
  34. --
  35. -- See also `put', `add'.
  36. require
  37. k /= Void
  38. deferred
  39. ensure
  40. v = at(k)
  41. end
  42. add (v: V_; k: K_)
  43. -- To add a new entry `k' with its associated value `v'.
  44. -- Actually, this is equivalent to call `put', but it may run a little bit faster.
  45. --
  46. -- See also `put', `fast_put'.
  47. require
  48. not has(k)
  49. deferred
  50. ensure
  51. count = 1 + old count
  52. v = at(k)
  53. end
  54. set_item (v: V_; index: INTEGER)
  55. require
  56. valid_index(index)
  57. deferred
  58. ensure
  59. count = old count
  60. v = item(index)
  61. end
  62. feature {ANY} -- Removing:
  63. remove (k: K_)
  64. -- Remove entry `k' (which may exist or not before this call).
  65. -- As the `remove' procedure actually uses `is_equal', you may consider to use `fast_remove' for expanded
  66. -- objects as well while trying to get the very best performances.
  67. --
  68. -- See also `fast_remove', `clear_count'.
  69. require
  70. k /= Void
  71. deferred
  72. ensure
  73. not has(k)
  74. end
  75. fast_remove (k: K_)
  76. -- Same job as `remove', but uses basic `=' for comparison.
  77. --
  78. -- See also `remove', `clear_count'.
  79. require
  80. k /= Void
  81. deferred
  82. ensure
  83. not fast_has(k)
  84. end
  85. clear_count
  86. -- Discard all items (`is_empty' is True after that call). The internal `capacity' is not changed
  87. -- by this call.
  88. --
  89. -- See also `clear_count_and_capacity', `remove'.
  90. deferred
  91. ensure
  92. is_empty: count = 0
  93. capacity = old capacity
  94. end
  95. clear_count_and_capacity
  96. -- Discard all items (`is_empty' is True after that call). The internal `capacity' may also be
  97. -- reduced after this call.
  98. --
  99. -- See also `clear_count', `remove'.
  100. deferred
  101. ensure
  102. is_empty: count = 0
  103. capacity <= old capacity
  104. end
  105. capacity: INTEGER
  106. -- Approximation of the actual internal storage `capacity'. The `capacity' will grow automatically
  107. -- when needed (i.e. `capacity' is not a limit for the number of values stored). Also note that
  108. -- the `capacity' value may not be always accurate depending of the implementation (anyway, this
  109. -- `capacity' value is at least equals to `count').
  110. deferred
  111. end
  112. feature {ANY}
  113. copy (other: like Current)
  114. -- Reinitialize by copying all associations of `other'.
  115. local
  116. i: INTEGER
  117. do
  118. clear_count
  119. from
  120. i := 1
  121. until
  122. i > other.count
  123. loop
  124. put(other.item(i), other.key(i))
  125. i := i + 1
  126. end
  127. end
  128. new_iterator_on_items: ITERATOR[V_]
  129. do
  130. create {ITERATOR_ON_DICTIONARY_ITEMS[V_, K_]} Result.make(Current)
  131. ensure then
  132. Result /= Void
  133. end
  134. feature {}
  135. make
  136. -- Creates an empty dictionary.
  137. deferred
  138. ensure
  139. is_empty
  140. end
  141. feature {} -- Implement manifest generic creation:
  142. manifest_make (needed_capacity: INTEGER)
  143. -- Manifest creation of a dictionary.
  144. do
  145. make
  146. end
  147. manifest_put (index: INTEGER; v: V_; k: K_)
  148. require
  149. not has(k)
  150. do
  151. add(v, k)
  152. end
  153. manifest_semicolon_check: INTEGER 2
  154. -- Put semicolons between successive value-key pairs.
  155. feature {}
  156. key_safe_equal (k1, k2: K_): BOOLEAN
  157. -- Because keys are never Void, we do not rely on the SAFE_EQUAL class.
  158. require
  159. k1 /= Void
  160. k2 /= Void
  161. do
  162. if k1 = k2 then
  163. Result := True
  164. elseif k1.same_dynamic_type(k2) then
  165. Result := k1.is_equal(k2)
  166. end
  167. end
  168. end -- class DICTIONARY
  169. --
  170. -- Copyright (C) 2009-2017: by all the people cited in the AUTHORS file.
  171. --
  172. -- Permission is hereby granted, free of charge, to any person obtaining a copy
  173. -- of this software and associated documentation files (the "Software"), to deal
  174. -- in the Software without restriction, including without limitation the rights
  175. -- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  176. -- copies of the Software, and to permit persons to whom the Software is
  177. -- furnished to do so, subject to the following conditions:
  178. --
  179. -- The above copyright notice and this permission notice shall be included in
  180. -- all copies or substantial portions of the Software.
  181. --
  182. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  183. -- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  184. -- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  185. -- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  186. -- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  187. -- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  188. -- THE SOFTWARE.