/src/lib/storage/collection3.e

http://github.com/tybor/Liberty · Specman e · 519 lines · 387 code · 46 blank · 86 comment · 11 complexity · 4b1d449f116e2a3cac910a40ff791614 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 COLLECTION3[E_]
  5. --
  6. -- Abstract definition of a 3 dimensional collection of elements of type E_.
  7. --
  8. -- The Liberty Eiffel standard library provides two implementations of COLLECTION3: ARRAY3 and FIXED_ARRAY3.
  9. -- All implementations have exactly the same behavior. Switching from one implementation to another only
  10. -- change the memory used and the execution time.
  11. --
  12. insert
  13. SAFE_EQUAL[E_]
  14. undefine copy, is_equal
  15. redefine fill_tagged_out_memory
  16. end
  17. feature {ANY} -- Indexing:
  18. lower1: INTEGER
  19. -- Lower index bound for dimension 1.
  20. deferred
  21. end
  22. lower2: INTEGER
  23. -- Lower index bound for dimension 2.
  24. deferred
  25. end
  26. lower3: INTEGER
  27. -- Lower index bound for dimension 3.
  28. deferred
  29. end
  30. frozen line_minimum: INTEGER
  31. -- Equivalent of `lower1'.
  32. do
  33. Result := lower1
  34. end
  35. frozen column_minimum: INTEGER
  36. -- Equivalent of `lower2'.
  37. do
  38. Result := lower2
  39. end
  40. frozen depth_minimum: INTEGER
  41. -- Equivalent of `lower3'.
  42. do
  43. Result := lower3
  44. end
  45. upper1: INTEGER
  46. -- Upper index bound for dimension 1.
  47. deferred
  48. end
  49. upper2: INTEGER
  50. -- Upper index bound for dimension 2.
  51. deferred
  52. end
  53. upper3: INTEGER
  54. -- Upper index bound for dimension 3.
  55. deferred
  56. end
  57. frozen line_maximum: INTEGER
  58. -- Equivalent of `upper1'.
  59. do
  60. Result := upper1
  61. end
  62. frozen column_maximum: INTEGER
  63. -- Equivalent of `upper2'.
  64. do
  65. Result := upper2
  66. end
  67. frozen depth_maximum: INTEGER
  68. -- Equivalent of `upper3'.
  69. do
  70. Result := upper3
  71. end
  72. feature {ANY} -- Reading:
  73. item (line, column, depth: INTEGER): E_
  74. require
  75. valid_index(line, column, depth)
  76. deferred
  77. end
  78. feature {ANY} -- Writing:
  79. put (element: like item; line, column, depth: INTEGER) assign item
  80. require
  81. valid_index(line, column, depth)
  82. deferred
  83. ensure
  84. item(line, column, depth) = element
  85. end
  86. force (element: like item; line, column, depth: INTEGER)
  87. -- Put `element' at position (`line',`column',`depth').
  88. -- Collection is resized first when (`line',`column',`depth')
  89. -- is not inside current bounds.
  90. -- New bounds are initialized with default values.
  91. require
  92. line >= 0
  93. column >= 0
  94. depth >= 0
  95. deferred
  96. ensure
  97. item(line, column, depth) = element
  98. count >= old count
  99. end
  100. feature {ANY} -- Index validity:
  101. frozen valid_line, valid_index1 (line: INTEGER): BOOLEAN
  102. do
  103. Result := lower1 <= line and then line <= upper1
  104. ensure
  105. Result = (lower1 <= line and line <= upper1)
  106. end
  107. frozen valid_column, valid_index2 (column: INTEGER): BOOLEAN
  108. do
  109. Result := lower2 <= column and then column <= upper2
  110. ensure
  111. Result = (lower2 <= column and column <= upper2)
  112. end
  113. frozen valid_depth, valid_index3 (depth: INTEGER): BOOLEAN
  114. do
  115. Result := lower3 <= depth and then depth <= upper3
  116. ensure
  117. Result = (lower3 <= depth and depth <= upper3)
  118. end
  119. frozen valid_index (line, column, depth: INTEGER): BOOLEAN
  120. do
  121. Result := lower1 <= line and then line <= upper1 and then lower2 <= column and then column <= upper2 and then lower3 <= depth and then depth <= upper3
  122. ensure
  123. Result = (valid_line(line) and valid_column(column) and valid_depth(depth))
  124. end
  125. feature {ANY} -- Counting:
  126. count1: INTEGER
  127. -- Size of the first dimension.
  128. deferred
  129. ensure
  130. Result = upper1 - lower1 + 1
  131. end
  132. frozen line_count: INTEGER
  133. -- Equivalent of `count1'.
  134. do
  135. Result := count1
  136. end
  137. count2: INTEGER
  138. -- Size of the second dimension.
  139. deferred
  140. ensure
  141. Result = upper2 - lower2 + 1
  142. end
  143. frozen column_count: INTEGER
  144. do
  145. Result := count2
  146. end
  147. count3: INTEGER
  148. -- Size of the third dimension.
  149. deferred
  150. ensure
  151. Result = upper3 - lower3 + 1
  152. end
  153. frozen depth_count: INTEGER
  154. do
  155. Result := count3
  156. end
  157. count: INTEGER
  158. -- Total number of elements.
  159. deferred
  160. ensure
  161. Result = line_count * column_count * depth_count
  162. end
  163. feature {ANY}
  164. swap (line1, column1, depth1, line2, column2, depth2: INTEGER)
  165. -- Swap the element at index (`line1',`column1',`depth1')
  166. -- with the element at index (`line2',`column2',`depth2').
  167. require
  168. valid_index(line1, column1, depth1)
  169. valid_index(line2, column2, depth2)
  170. deferred
  171. ensure
  172. item(line1, column1, depth1) = old item(line2, column2, depth2)
  173. item(line2, column2, depth2) = old item(line1, column1, depth1)
  174. count = old count
  175. end
  176. set_all_with (v: like item)
  177. -- Set all item with value `v'.
  178. deferred
  179. ensure
  180. count = old count
  181. end
  182. frozen clear_all
  183. -- Set all items to default values.
  184. local
  185. value: like item
  186. do
  187. set_all_with(value)
  188. ensure
  189. count = old count
  190. all_default
  191. end
  192. feature {ANY} -- Creating or initializing:
  193. from_collection3 (model: COLLECTION3[like item])
  194. -- Uses `model' to initialize Current.
  195. require
  196. model /= Void
  197. deferred
  198. ensure
  199. count1 = model.count1
  200. count2 = model.count2
  201. count3 = model.count3
  202. end
  203. from_model (model: COLLECTION[COLLECTION[COLLECTION[E_]]])
  204. -- The `model' is used to fill line by line Current.
  205. -- Assume all sub-collections have the same
  206. -- dimension.
  207. require
  208. model /= Void
  209. deferred
  210. ensure
  211. count1 = model.count
  212. count2 > 0 implies count2 = model.first.count
  213. count3 > 0 implies count3 = model.first.first.count
  214. end
  215. feature {ANY} -- Looking and comparison:
  216. all_default: BOOLEAN
  217. -- Do all items have their type's default value?
  218. deferred
  219. end
  220. fast_is_equal (other: like Current): BOOLEAN
  221. -- Do both collections have the same `lower1', `lower2', `lower3', `upper1', `upper2' and
  222. -- `upper3', and items?
  223. -- The basic `=' is used for comparison of items.
  224. -- See also `is_equal'.
  225. local
  226. line, column, depth: INTEGER
  227. do
  228. if lower1 /= other.lower1 then
  229. elseif upper1 /= other.upper1 then
  230. elseif lower2 /= other.lower2 then
  231. elseif upper2 /= other.upper2 then
  232. elseif lower3 /= other.lower3 then
  233. elseif upper3 /= other.upper3 then
  234. else
  235. from
  236. Result := True
  237. line := upper1
  238. until
  239. not Result or else line < lower1
  240. loop
  241. from
  242. column := upper2
  243. until
  244. not Result or else column < lower2
  245. loop
  246. from
  247. depth := upper3
  248. until
  249. not Result or else depth < lower3
  250. loop
  251. Result := item(line, column, depth) = other.item(line, column, depth)
  252. depth := depth - 1
  253. end
  254. column := column - 1
  255. end
  256. line := line - 1
  257. end
  258. end
  259. end
  260. is_equal (other: like Current): BOOLEAN
  261. -- Do both collections have the same `lower1', `lower2', `lower3', `upper1', `upper2' and `upper3',
  262. -- and items?
  263. -- Feature `is_equal' is used for comparison of items.
  264. -- See also `fast_is_equal'.
  265. local
  266. line, column, depth: INTEGER
  267. do
  268. if lower1 /= other.lower1 then
  269. elseif upper1 /= other.upper1 then
  270. elseif lower2 /= other.lower2 then
  271. elseif upper2 /= other.upper2 then
  272. elseif lower3 /= other.lower3 then
  273. elseif upper3 /= other.upper3 then
  274. else
  275. from
  276. Result := True
  277. line := upper1
  278. until
  279. not Result or else line < lower1
  280. loop
  281. from
  282. column := upper2
  283. until
  284. not Result or else column < lower2
  285. loop
  286. from
  287. depth := upper3
  288. until
  289. not Result or else depth < lower3
  290. loop
  291. Result := safe_equal(item(line, column, depth), other.item(line, column, depth))
  292. depth := depth - 1
  293. end
  294. column := column - 1
  295. end
  296. line := line - 1
  297. end
  298. end
  299. end
  300. is_equal_map (other: like Current): BOOLEAN
  301. -- Do both collections have the same `lower', `upper', and
  302. -- items?
  303. -- Feature `is_equal' is used for comparison of items.
  304. obsolete "Use `is_equal' instead."
  305. do
  306. Result := is_equal(other)
  307. end
  308. feature {ANY} -- Printing:
  309. frozen fill_tagged_out_memory
  310. local
  311. line, column, depth: INTEGER; v: like item
  312. do
  313. tagged_out_memory.append(once "lower1: ")
  314. lower1.append_in(tagged_out_memory)
  315. tagged_out_memory.append(once " upper1: ")
  316. upper1.append_in(tagged_out_memory)
  317. tagged_out_memory.append(once " lower2: ")
  318. lower2.append_in(tagged_out_memory)
  319. tagged_out_memory.append(once " upper2: ")
  320. upper2.append_in(tagged_out_memory)
  321. tagged_out_memory.append(once " lower3: ")
  322. lower3.append_in(tagged_out_memory)
  323. tagged_out_memory.append(once " upper3: ")
  324. upper3.append_in(tagged_out_memory)
  325. tagged_out_memory.append(once " [%N")
  326. from
  327. line := lower1
  328. until
  329. line > upper1 or else tagged_out_memory.count > 4096
  330. loop
  331. tagged_out_memory.append(once "line ")
  332. line.append_in(tagged_out_memory)
  333. tagged_out_memory.append(once "%T: ")
  334. from
  335. column := lower2
  336. until
  337. column > upper2
  338. loop
  339. tagged_out_memory.append(once "column ")
  340. column.append_in(tagged_out_memory)
  341. tagged_out_memory.append(once "%T: ")
  342. from
  343. depth := lower3
  344. until
  345. depth > upper3
  346. loop
  347. tagged_out_memory.append(once "depth ")
  348. depth.append_in(tagged_out_memory)
  349. tagged_out_memory.append(once "%T: ")
  350. v := item(line, column, depth)
  351. if v = Void then
  352. tagged_out_memory.append(once "Void")
  353. else
  354. v.out_in_tagged_out_memory
  355. end
  356. tagged_out_memory.extend(' ')
  357. depth := depth + 1
  358. end
  359. tagged_out_memory.extend('%N')
  360. column := column + 1
  361. end
  362. tagged_out_memory.extend('%N')
  363. line := line + 1
  364. end
  365. if valid_line(line) then
  366. tagged_out_memory.append(once "......%N")
  367. end
  368. end
  369. feature {ANY} -- Miscellaneous features:
  370. occurrences (elt: E_): INTEGER
  371. -- Number of occurrences using `is_equal'.
  372. -- See also `fast_occurrences' to choose the appropriate one.
  373. deferred
  374. ensure
  375. Result >= 0
  376. end
  377. fast_occurrences (elt: E_): INTEGER
  378. -- Number of occurrences using `='.
  379. -- See also `occurrences' to choose the appropriate one.
  380. deferred
  381. ensure
  382. Result >= 0
  383. end
  384. has (x: like item): BOOLEAN
  385. -- Search if a element x is in the array using `is_equal'.
  386. -- See also `fast_has' to choose the appropriate one.
  387. deferred
  388. end
  389. fast_has (x: like item): BOOLEAN
  390. -- Search if a element x is in the array using `='.
  391. deferred
  392. end
  393. replace_all (old_value, new_value: like item)
  394. -- Replace all occurrences of the element `old_value' by `new_value'
  395. -- using `is_equal' for comparison.
  396. -- See also `fast_replace_all' to choose the appropriate one.
  397. deferred
  398. ensure
  399. count = old count
  400. occurrences(old_value) = 0
  401. end
  402. fast_replace_all (old_value, new_value: like item)
  403. -- Replace all occurrences of the element `old_value' by `new_value'
  404. -- using operator `=' for comparison.
  405. -- See also `replace_all' to choose the appropriate one.
  406. deferred
  407. ensure
  408. count = old count
  409. fast_occurrences(old_value) = 0
  410. end
  411. sub_collection3 (line_min, line_max, column_min, column_max, depth_min, depth_max: INTEGER): like Current
  412. -- Create a new object using selected area of `Current'.
  413. require
  414. valid_index(line_min, column_min, depth_min)
  415. valid_index(line_max, column_max, depth_max)
  416. deferred
  417. ensure
  418. Result /= Void
  419. end
  420. set_area (element: like item; line_min, line_max, column_min, column_max, depth_min, depth_max: INTEGER)
  421. -- Set all the elements of the selected area rectangle with `element'.
  422. require
  423. valid_index(line_min, column_min, depth_min)
  424. valid_index(line_max, column_max, depth_max)
  425. local
  426. line, column, depth: INTEGER
  427. do
  428. from
  429. line := line_min
  430. until
  431. line > line_max
  432. loop
  433. from
  434. column := column_min
  435. until
  436. column > column_max
  437. loop
  438. from
  439. depth := depth_min
  440. until
  441. depth > depth_max
  442. loop
  443. put(element, line, column, depth)
  444. depth := depth + 1
  445. end
  446. column := column + 1
  447. end
  448. line := line + 1
  449. end
  450. ensure
  451. count = old count
  452. end
  453. end -- class COLLECTION3
  454. --
  455. -- Copyright (C) 2009-2017: by all the people cited in the AUTHORS file.
  456. --
  457. -- Permission is hereby granted, free of charge, to any person obtaining a copy
  458. -- of this software and associated documentation files (the "Software"), to deal
  459. -- in the Software without restriction, including without limitation the rights
  460. -- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  461. -- copies of the Software, and to permit persons to whom the Software is
  462. -- furnished to do so, subject to the following conditions:
  463. --
  464. -- The above copyright notice and this permission notice shall be included in
  465. -- all copies or substantial portions of the Software.
  466. --
  467. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  468. -- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  469. -- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  470. -- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  471. -- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  472. -- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  473. -- THE SOFTWARE.