/src/lib/storage/internal/arrayed_collection.e

http://github.com/tybor/Liberty · Specman e · 152 lines · 97 code · 18 blank · 37 comment · 1 complexity · b8bd0bad7bec9931bcd05edff363f5c3 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 ARRAYED_COLLECTION[E_]
  5. --
  6. -- Common root for ARRAY, FAST_ARRAY and RING_ARRAY.
  7. --
  8. inherit
  9. COLLECTION[E_]
  10. feature {ARRAYED_COLLECTION, ARRAYED_COLLECTION_HANDLER}
  11. storage: NATIVE_ARRAY[E_]
  12. -- Internal access to storage location.
  13. from_external (a_storage: POINTER; a_capacity: like capacity)
  14. require
  15. a_capacity > 0 implies a_storage.is_not_null
  16. do
  17. storage := storage.from_pointer(a_storage)
  18. capacity := a_capacity
  19. ensure
  20. storage.to_external = a_storage
  21. capacity = a_capacity
  22. end
  23. feature {ANY}
  24. capacity: INTEGER
  25. -- Internal storage capacity in number of item.
  26. upper: INTEGER
  27. -- Upper index bound.
  28. subarray (min, max: INTEGER): like Current
  29. -- New collection consisting of items at indexes in [`min' .. `max'].
  30. -- Result has the same dynamic type as `Current'.
  31. -- See also `slice'.
  32. require
  33. lower <= min
  34. max <= upper
  35. min <= max + 1
  36. deferred
  37. ensure
  38. same_dynamic_type(Result)
  39. Result.count = max - min + 1
  40. Result.lower = min or Result.lower = 0
  41. end
  42. feature {ANY} -- Implementation of deferred:
  43. first: like item
  44. do
  45. Result := storage.item(0)
  46. end
  47. last: like item
  48. do
  49. Result := item(upper)
  50. end
  51. add (element: like item; index: INTEGER)
  52. do
  53. if index = upper + 1 then
  54. add_last(element)
  55. else
  56. add_last(element)
  57. move(index, upper - 1, 1)
  58. put(element, index)
  59. end
  60. end
  61. remove_last
  62. do
  63. upper := upper - 1
  64. end
  65. remove_tail (n: INTEGER)
  66. do
  67. upper := upper - n
  68. end
  69. replace_all (old_value, new_value: like item)
  70. do
  71. storage.replace_all(old_value, new_value, count - 1)
  72. end
  73. fast_replace_all (old_value, new_value: like item)
  74. do
  75. storage.fast_replace_all(old_value, new_value, count - 1)
  76. end
  77. reverse
  78. local
  79. i, j: INTEGER
  80. do
  81. from
  82. i := lower
  83. j := upper
  84. until
  85. i >= j
  86. loop
  87. swap(i, j)
  88. i := i + 1
  89. j := j - 1
  90. end
  91. end
  92. feature {ANY} -- Interfacing with C:
  93. to_external: POINTER
  94. -- Gives C access into the internal `storage' of the ARRAY.
  95. -- Result is pointing the element at index `lower'.
  96. --
  97. -- NOTE: do not free/realloc the Result. Resizing of the array
  98. -- can makes this pointer invalid.
  99. require
  100. not is_empty
  101. do
  102. Result := storage.to_pointer
  103. ensure
  104. Result.is_not_null
  105. end
  106. feature {ARRAYED_COLLECTION}
  107. set_upper (new_upper: like upper)
  108. do
  109. upper := new_upper
  110. end
  111. invariant
  112. capacity >= upper - lower + 1
  113. capacity > 0 implies storage.is_not_null
  114. end -- class ARRAYED_COLLECTION
  115. --
  116. -- Copyright (C) 2009-2017: by all the people cited in the AUTHORS file.
  117. --
  118. -- Permission is hereby granted, free of charge, to any person obtaining a copy
  119. -- of this software and associated documentation files (the "Software"), to deal
  120. -- in the Software without restriction, including without limitation the rights
  121. -- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  122. -- copies of the Software, and to permit persons to whom the Software is
  123. -- furnished to do so, subject to the following conditions:
  124. --
  125. -- The above copyright notice and this permission notice shall be included in
  126. -- all copies or substantial portions of the Software.
  127. --
  128. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  129. -- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  130. -- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  131. -- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  132. -- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  133. -- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  134. -- THE SOFTWARE.