/src/lib/storage/dictionary/zip.e

http://github.com/tybor/Liberty · Specman e · 178 lines · 129 code · 23 blank · 26 comment · 4 complexity · 2b8978279d4a185cfdaad69ac2298c79 MD5 · raw file

  1. -- This file is part of a Liberty Eiffel library.
  2. -- See the full copyright at the end.
  3. --
  4. class ZIP[V_, K_]
  5. --
  6. -- Makes a dictionary of two independent hoards.
  7. --
  8. inherit
  9. MAP[V_, K_]
  10. redefine
  11. lower, upper, keys, items
  12. end
  13. insert
  14. SAFE_EQUAL[K_]
  15. undefine
  16. is_equal, out_in_tagged_out_memory
  17. redefine
  18. copy
  19. end
  20. create {ANY}
  21. make
  22. feature {ANY}
  23. lower: INTEGER 0
  24. upper: INTEGER
  25. do
  26. Result := count - 1
  27. end
  28. count: INTEGER
  29. do
  30. Result := keys.count
  31. end
  32. copy (other: like Current)
  33. do
  34. make(other.items, other.keys)
  35. end
  36. has (k: K_): BOOLEAN
  37. do
  38. Result := keys.valid_index(index_of(k))
  39. end
  40. at, reference_at (k: K_): V_
  41. local
  42. index: INTEGER
  43. do
  44. index := index_of(k)
  45. Result := items.item(index - keys.lower + items.lower)
  46. end
  47. fast_has (k: K_): BOOLEAN
  48. do
  49. Result := keys.valid_index(fast_index_of(k))
  50. end
  51. fast_at, fast_reference_at (k: K_): V_
  52. local
  53. index: INTEGER
  54. do
  55. index := fast_index_of(k)
  56. Result := items.item(index - keys.lower + items.lower)
  57. end
  58. item (index: INTEGER): V_
  59. do
  60. Result := items.item(index + items.lower)
  61. end
  62. key (index: INTEGER): K_
  63. do
  64. Result := keys.item(index + keys.lower)
  65. end
  66. new_iterator_on_items: ITERATOR[V_]
  67. do
  68. Result := items.new_iterator
  69. end
  70. new_iterator_on_keys: ITERATOR[K_]
  71. do
  72. Result := keys.new_iterator
  73. end
  74. new_iterator: ITERATOR[TUPLE[V_, K_]]
  75. do
  76. create {ITERATOR_ON_ZIP[V_, K_]} Result.make(new_iterator_on_items, new_iterator_on_keys)
  77. end
  78. feature {ANY} -- Other features:
  79. internal_key (k: K_): K_
  80. local
  81. index: INTEGER
  82. do
  83. index := index_of(k)
  84. Result := keys.item(index)
  85. end
  86. feature {ANY}
  87. make (a_items: like items; a_keys: like keys)
  88. require
  89. a_items.count = a_keys.count
  90. do
  91. items := a_items
  92. keys := a_keys
  93. ensure
  94. items = a_items
  95. keys = a_keys
  96. end
  97. feature {ANY}
  98. items: TRAVERSABLE[V_]
  99. keys: TRAVERSABLE[K_]
  100. feature {}
  101. index_of (k: K_): INTEGER
  102. local
  103. i: INTEGER
  104. do
  105. from
  106. Result := keys.lower - 1
  107. i := keys.lower
  108. until
  109. keys.valid_index(Result) or else i > keys.upper
  110. loop
  111. if safe_equal(k, keys.item(i)) then
  112. Result := i
  113. end
  114. i := i + 1
  115. end
  116. end
  117. fast_index_of (k: K_): INTEGER
  118. local
  119. i: INTEGER
  120. do
  121. from
  122. Result := keys.lower - 1
  123. i := keys.lower
  124. until
  125. keys.valid_index(Result) or else i > keys.upper
  126. loop
  127. if k = keys.item(i) then
  128. Result := i
  129. end
  130. i := i + 1
  131. end
  132. end
  133. invariant
  134. items.count = keys.count
  135. end -- class ZIP
  136. --
  137. -- Copyright (C) 2009-2017: by all the people cited in the AUTHORS file.
  138. --
  139. -- Permission is hereby granted, free of charge, to any person obtaining a copy
  140. -- of this software and associated documentation files (the "Software"), to deal
  141. -- in the Software without restriction, including without limitation the rights
  142. -- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  143. -- copies of the Software, and to permit persons to whom the Software is
  144. -- furnished to do so, subject to the following conditions:
  145. --
  146. -- The above copyright notice and this permission notice shall be included in
  147. -- all copies or substantial portions of the Software.
  148. --
  149. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  150. -- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  151. -- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  152. -- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  153. -- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  154. -- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  155. -- THE SOFTWARE.