Advertisement
Guest User

Untitled

a guest
May 30th, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.54 KB | None | 0 0
  1. note
  2. description: "Objects that represent ADT MAP:KEY x VALUE."
  3. author: "Przemyslaw Pawluk"
  4. date: "2013 May 9"
  5. version: 1.0
  6.  
  7. class
  8. MY_MAP[K,V]
  9. create
  10. make
  11.  
  12.  
  13. feature --Representation
  14.  
  15. list:LINKED_LIST[ITEM[K,V]]
  16.  
  17.  
  18. count: INTEGER -- counter
  19.  
  20.  
  21. feature {ANY}
  22.  
  23. make
  24. --creates all required variables
  25. do
  26. --??? {LINK_LIST[ITEM]}
  27. create list.make
  28. count:=0;
  29.  
  30. ensure
  31. --??
  32. count = 0
  33. list.is_empty
  34. end
  35.  
  36.  
  37. put(key: K; value: V)
  38.  
  39. require
  40. -- key /= void
  41. -- value /= void -- ensure the value is not void because equals reqires no void values
  42.  
  43. local
  44. i: INTEGER
  45. --duplicate_key: BOOLEAN
  46. item: ITEM[K,V]
  47. do
  48. --io.put_string ("k" + key.out + "," + value.out)
  49. create item.make (key,value)
  50. --duplicate_key = true
  51.  
  52. from
  53. i := 1
  54. until
  55. i >= list.count
  56. loop
  57. --list.go_i_th (i: INTEGER_32)
  58. if list.i_th (i).getkey.is_deep_equal (key)
  59. then
  60. list.remove
  61.  
  62.  
  63. end
  64. -- io.put_string ("k" + key.out + "," + value.out)
  65. i := i + 1
  66. end
  67.  
  68.  
  69. list.extend (item)
  70.  
  71. --???
  72. ensure
  73. --???
  74. -- list.item.getvalue /= void
  75. end;
  76.  
  77. get(key:K):V
  78. require
  79. Key /= void
  80. local
  81. i : INTEGER
  82. do
  83. from
  84. i := 1
  85. until
  86. i >= list.count
  87. loop
  88. --list.go_i_th (i: INTEGER_32)
  89. if list.i_th (i).getkey.is_deep_equal (key)
  90. then
  91. result := list.item_for_iteration.getvalue
  92.  
  93. end
  94. i := i + 1
  95. end
  96. ensure
  97. list.item_for_iteration.getvalue /= void
  98. --Result/=void
  99. end
  100.  
  101. remove(key:K)
  102. local
  103. i:INTEGER
  104. do
  105. from
  106. i := 1
  107. until
  108. i >= list.count
  109. loop
  110. --list.go_i_th (i: INTEGER_32)
  111. if list.i_th (i).getkey.is_deep_equal (key)
  112. then
  113. list.remove
  114.  
  115. end
  116. i := i + 1
  117. end
  118. --???
  119. ensure
  120. --???
  121. end
  122.  
  123. has(key:K; val:V):BOOLEAN
  124. require
  125. --key /= void
  126. local
  127. i:INTEGER
  128. return : BOOLEAN
  129. do
  130. return := false
  131. from
  132. i := 1
  133. until
  134. i >= list.count
  135. loop
  136.  
  137. if list.i_th (i).getkey.is_deep_equal (key) and list.i_th (i).getvalue.is_deep_equal(val)
  138. then
  139. return := true
  140. end
  141. i := i + 1
  142. end
  143.  
  144. --???
  145. result := return
  146. ensure
  147. --???
  148. end
  149.  
  150. key_set:SET[K]
  151. -- returns a set of key values from the map
  152. local
  153. set: SET[K]
  154. i:INTEGER
  155. do
  156. --create
  157. from
  158. i := 1
  159. until
  160. i >= list.count
  161. loop
  162. list.go_i_th (i)
  163. set.extend (list.item_for_iteration.getkey)
  164. i := i + 1
  165.  
  166. end
  167. result := set
  168. --???
  169.  
  170. ensure
  171. --???
  172. end
  173.  
  174. value_set:SET[V]
  175. -- returns a set of values from the map
  176. local
  177. i:INTEGER
  178. set: SET[V]
  179. do
  180. --create set.make
  181. from
  182. i := 1
  183. until
  184. i >= list.count
  185. loop
  186.  
  187. list.go_i_th (i)
  188. set.put (list.item_for_iteration.getvalue)
  189. i := i + 1
  190. end
  191. result := set
  192. --???
  193. ensure
  194. --???
  195. end
  196.  
  197. feature --Quantifiers
  198.  
  199. exist(key:K; val:V):BOOLEAN
  200. --checks if given key/value combination exist in the map
  201. do
  202. --???
  203. ensure
  204. --???
  205. end
  206.  
  207. exist1(key:K; val:V):BOOLEAN
  208. --checks if there is only one pair with given key/value combination
  209. do
  210. --???
  211. ensure
  212. --???
  213. end
  214.  
  215. has1(key:K):BOOLEAN
  216. --checks if there is only one key with given value
  217.  
  218. do
  219. --???
  220. ensure
  221. --???
  222. end
  223.  
  224. invariant
  225. count: count>=0
  226. --???
  227.  
  228. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement