/src/kodkod/util/ints/IntVector.java

https://github.com/msakai/kodkod · Java · 271 lines · 25 code · 23 blank · 223 comment · 0 complexity · ae2d6f69a28c27f52d2e65f6fd3d339c MD5 · raw file

  1. /*
  2. * Kodkod -- Copyright (c) 2005-2011, Emina Torlak
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. * THE SOFTWARE.
  21. */
  22. package kodkod.util.ints;
  23. /**
  24. * A resizable array of integers.
  25. *
  26. * @specfield length: int
  27. * @specfield elements: [0..size) ->one int
  28. *
  29. * @author Emina Torlak
  30. */
  31. public interface IntVector extends IntCollection {
  32. /**
  33. * Returns the number of elements in this vector.
  34. * @return this.length
  35. */
  36. public int size();
  37. /**
  38. * Returns <tt>true</tt> if this vector contains no elements.
  39. *
  40. * @return no this.elements
  41. */
  42. public boolean isEmpty();
  43. /**
  44. * Returns <tt>true</tt> if this vector contains the specified element.
  45. * @return element in this.elements[int]
  46. */
  47. public boolean contains(int element);
  48. /**
  49. * Returns the element at the specified position in this vector.
  50. *
  51. * @return this.elements[index]
  52. *
  53. * @throws IndexOutOfBoundsException if the index is out of range (index
  54. * &lt; 0 || index &gt;= length()).
  55. */
  56. public int get(int index);
  57. /**
  58. * Returns an iterator over the elements in this vector in proper sequence.
  59. *
  60. * @return an iterator over the elements in this vector in proper sequence.
  61. */
  62. public IntIterator iterator();
  63. /**
  64. * Returns an iterator over the elements in this vector in proper sequence,
  65. * starting <tt>fromIndex<\tt>, inclusive, and ending at <tt>toIndex<\tt>, exclusive.
  66. * If <tt>fromIndex<\tt> is less than <tt>toIndex<\tt>, then the iterator will return
  67. * the elements in the descending order.
  68. * @return an iterator over the elements in this vector in proper sequence,
  69. * starting at <tt>fromIndex<\tt>, inclusive, and ending at <tt>toIndex<\tt>.
  70. * @throws IndexOutOfBoundsException fromIndex !in [0..this.length) || toIndex !in [-1..this.length]
  71. */
  72. public IntIterator iterator(int fromIndex, int toIndex);
  73. /**
  74. * Replaces the element at the specified position in this vector with the
  75. * specified element, and returns the previous element (optional operation).
  76. *
  77. * @ensures this.elements' = this.elements' ++ index -> element
  78. * @return this.elements[index]
  79. *
  80. * @throws UnsupportedOperationException if the <tt>set</tt> method is not
  81. * supported by this vector.
  82. * @throws IllegalArgumentException if some aspect of the specified
  83. * element prevents it from being added to this vector.
  84. * @throws IndexOutOfBoundsException if the index is out of range
  85. * (index &lt; 0 || index &gt;= length()).
  86. */
  87. public int set(int index, int element);
  88. /**
  89. * Removes all of the elements from this vector (optional operation). This
  90. * vector will be empty after this call returns (unless it throws an
  91. * exception).
  92. * @ensures this.length' = 0 && no this.elements'
  93. * @throws UnsupportedOperationException if the <tt>clear</tt> method is
  94. * not supported by this vector.
  95. */
  96. public void clear();
  97. /**
  98. * Returns the index in this vector of the first occurrence of the specified
  99. * element, or -1 if this vector does not contain this element.
  100. *
  101. * @return element in this.elements[int] => min(this.elements.element), -1
  102. */
  103. public int indexOf(int element);
  104. /**
  105. * Returns the index in this vector of the last occurrence of the specified
  106. * element, or -1 if this vector does not contain this element.
  107. *
  108. * @return element in this.elements[int] => max(this.elements.element), -1
  109. */
  110. public int lastIndexOf(int element);
  111. /**
  112. * Adds the specified element to the end of this vector (optional
  113. * operation), and returns true if this vector has changed as a result
  114. * of the call.
  115. * @ensures this.length' = this.length + 1 && this.elements' = this.elements + this.length -> element
  116. * @return this.elements != this.elements'
  117. * @throws UnsupportedOperationException if the <tt>add</tt> method is not supported by this vector.
  118. * @throws IllegalArgumentException if some aspect of this element prevents it from being added to this vector.
  119. */
  120. public boolean add(int element);
  121. /**
  122. * Inserts the specified element at the specified position in this vector
  123. * (optional operation), and returns true if this vector has changed as a result of the call.
  124. * Shifts the element currently at that position
  125. * (if any) and any subsequent elements to the right (adds one to their
  126. * indices).
  127. *
  128. * @ensures this.length' = this.length + 1 &&
  129. * this.elements' = { i: [0..this.length'), e: int | i < index => e = this.elements[i],
  130. * i = index => e = element, e = this.elements[i-1] }
  131. * @throws UnsupportedOperationException if the <tt>add</tt> method is not
  132. * supported by this vector.
  133. * @throws IllegalArgumentException if some aspect of the specified
  134. * element prevents it from being added to this vector.
  135. * @throws IndexOutOfBoundsException if the index is out of range
  136. * (index &lt; 0 || index &gt; length()).
  137. */
  138. public void add(int index, int element);
  139. /**
  140. * Appends the specified elements to the end of this vector (optional
  141. * operation), and returns true if this vector has changed as a result of the call.
  142. * @ensures appends the specified elements to the end of this vector
  143. * @return this.elements != this.elements'
  144. * @throws UnsupportedOperationException if the <tt>add</tt> method is not
  145. * supported by this vector.
  146. * @throws IllegalArgumentException if some aspect of an element in the given vector
  147. * prevents it from being added to this vector.
  148. */
  149. public boolean addAll(IntCollection c);
  150. /**
  151. * Inserts the specified elements at the specified position in this vector
  152. * (optional operation), and returns true if this vector has changed as a result of the call.
  153. * Shifts the element currently at that position
  154. * (if any) and any subsequent elements to the right.
  155. *
  156. * @ensures inserts the specified elements at the specified position in this vector
  157. * @return this.elements != this.elements
  158. * @throws UnsupportedOperationException if the <tt>add</tt> method is not
  159. * supported by this vector.
  160. * @throws IllegalArgumentException if some aspect of an element in the specified
  161. * collection prevents it from being added to this vector.
  162. * @throws IndexOutOfBoundsException if the index is out of range
  163. * (index &lt; 0 || index &gt; length()).
  164. */
  165. public boolean addAll(int index, IntCollection c);
  166. /**
  167. * Removes the first occurrence of the given integer from this vector,
  168. * and returns true if this vector has changed as a result of the call.
  169. * @ensures removes the first occurrence of the given integer from this vector
  170. * @return this.elements != this.elements'
  171. * @throws UnsupportedOperationException this is an unmodifiable collection
  172. */
  173. public abstract boolean remove(int i);
  174. /**
  175. * Removes the element at the specified position in this vector (optional
  176. * operation). Shifts any subsequent elements to the left (subtracts one
  177. * from their indices). Returns the element that was removed from the
  178. * vector.
  179. * @return this.elements[index]
  180. * @ensures this.length' = this.length - 1 &&
  181. * this.elements' = { i: [0..this.length'), e: int | i < index => e = this.elements[i],
  182. * e = this.elements[i+1] }
  183. * @throws UnsupportedOperationException if the <tt>remove</tt> method is
  184. * not supported by this vector.
  185. * @throws IndexOutOfBoundsException if the index is out of range (index
  186. * &lt; 0 || index &gt;= length()).
  187. */
  188. public int removeAt(int index);
  189. /**
  190. * Removes all of this vector's elements that are also contained in the specified
  191. * collection. After this call returns, this collection will contain no elements in
  192. * common with the specified collection. Returns true if this collection has changed as a result of the call.
  193. * @ensures removes all of this vector's elements that are also contained in the specified
  194. * collection
  195. * @return this.elements != this.elements'
  196. * @throws NullPointerException c = null
  197. * @throws UnsupportedOperationException this is an unmodifiable collection
  198. */
  199. public abstract boolean removeAll(IntCollection c);
  200. /**
  201. * Retains only the elements in this vector that are contained in the specified
  202. * collection. In other words, removes from this collection all of its elements that
  203. * are not contained in the specified collection. Returns true if this collection has changed as a result of the call.
  204. * @ensures retains only the elements in this vector that are contained in the specified
  205. * collection
  206. * @return this.elements != this.elements'
  207. * @throws NullPointerException c = null
  208. * @throws UnsupportedOperationException this is an unmodifiable collection
  209. */
  210. public abstract boolean retainAll(IntCollection c);
  211. /**
  212. * Compares the specified object with this vector for equality. Returns
  213. * <tt>true</tt> if and only if the specified object is also an int vector, both
  214. * vectors have the same size, and all corresponding pairs of elements in
  215. * the two vectors are <i>equal</i>.
  216. *
  217. * @return <tt>true</tt> if the specified object is equal to this vector.
  218. */
  219. public boolean equals(Object o);
  220. /**
  221. * Returns the hash code value for this vector. The hash code of an int vector is
  222. * defined to be the {@link Ints#superFastHash(int[])} of the elements in the vector,
  223. * taken in the ascending order of indices.
  224. * This ensures that v1.equals(v2) implies that v1.hashCode()==v2.hashCode()
  225. * for any two IntVectors v1 and v2, as required by the general contract of the Object.hashCode method.
  226. * @return Ints.superFastHash(this.toArray())
  227. */
  228. public int hashCode();
  229. /**
  230. * Returns an array containing all of the elements in this vector in proper
  231. * sequence.
  232. *
  233. * @return an array containing all of the elements in this vector in proper
  234. * sequence.
  235. */
  236. public int[] toArray();
  237. /**
  238. * Copies the components of this vector into the specified array, provided that
  239. * it is large enough, and returns it. The item at index
  240. * k in this vector is copied into component k of the given array. If the
  241. * given array is not large enough, the effect of this method is the same as
  242. * calling {@linkplain #toArray()}.
  243. * @ensures array.length>=this.length => all i: [0..this.length) | array'[i] = this.elements[i]
  244. * @return array.length>=this.length => array' else this.toArray()
  245. * @throws NullPointerException array = null
  246. */
  247. public int[] toArray(int[] array);
  248. }