Vector.vala 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. namespace Invercargill.DataStructures {
  2. public class Vector<T> : Enumerable<T>, Lot<T>, ReadOnlyCollection<T>, ReadOnlyAddressable<T>, Collection<T>, Addressable<T> {
  3. private T[] array;
  4. private int n_items = 0;
  5. private SafeReadFunc<T>? safe_read;
  6. private SafeWriteFunc<T>? safe_write;
  7. private RWLock rw_lock;
  8. private const int INITIAL_SIZE = 2;
  9. public Vector() {
  10. rw_lock = RWLock();
  11. array = new T[INITIAL_SIZE];
  12. safe_read = get_safe_read_function_for<T>();
  13. safe_write = get_safe_write_function_for<T>();
  14. }
  15. public override int? peek_count() {
  16. return n_items;
  17. }
  18. public override int count(PredicateDelegate<T>? predicate = null) {
  19. if(predicate == null) {
  20. return n_items;
  21. }
  22. return base.count(predicate);
  23. }
  24. public override EnumerableInfo get_info() {
  25. return new EnumerableInfo.infer_ultimate (this, EnumerableCategory.IN_MEMORY);
  26. }
  27. public override Tracker<T> get_tracker() {
  28. return new AddressableTracker<T>(this);
  29. }
  30. public override T[] to_array () {
  31. rw_lock.reader_lock();
  32. var a2 = new T[n_items];
  33. if(safe_write != null) {
  34. for(var i = 0; i < n_items; i++) {
  35. safe_write(a2, i, array_read(i));
  36. }
  37. }
  38. else {
  39. for(var i = 0; i < n_items; i++) {
  40. a2[i] = array[i];
  41. }
  42. }
  43. rw_lock.reader_unlock();
  44. return a2;
  45. }
  46. public void add(T item) {
  47. rw_lock.writer_lock();
  48. ensure_room(1);
  49. array_write(n_items, item);
  50. n_items++;
  51. rw_lock.writer_unlock();
  52. }
  53. public override void add_all(Enumerable<T> items) {
  54. rw_lock.writer_lock();
  55. foreach (var item in items) {
  56. ensure_room(1);
  57. array_write(n_items, item);
  58. n_items++;
  59. }
  60. rw_lock.writer_unlock();
  61. }
  62. public new T @get(uint index) throws IndexError {
  63. IndexError? e = null;
  64. rw_lock.reader_lock();
  65. if(index < 0) {
  66. e = new IndexError.INDEX_EXCEEDS_LOWER_BOUNDS("Index is less than 0");
  67. }
  68. else if(index >= n_items) {
  69. e = new IndexError.INDEX_EXCEEDS_UPPER_BOUNDS(@"Tried to access index $(index) on a vector with $(n_items) item(s)");
  70. }
  71. else {
  72. var item = array_read(index);
  73. rw_lock.reader_unlock();
  74. return item;
  75. }
  76. rw_lock.reader_unlock();
  77. throw e;
  78. }
  79. public bool try_get(uint index, out T value) {
  80. rw_lock.reader_lock();
  81. if(index >= 0 && index < n_items) {
  82. value = array_read(index);
  83. rw_lock.reader_unlock();
  84. return true;
  85. }
  86. rw_lock.reader_unlock();
  87. value = null;
  88. return false;
  89. }
  90. public new void @set(int index, T value) throws IndexError {
  91. IndexError? e = null;
  92. rw_lock.writer_lock();
  93. if(index < 0) {
  94. e = new IndexError.INDEX_EXCEEDS_LOWER_BOUNDS("Index is less than 0");
  95. }
  96. else if(index >= n_items) {
  97. e = new IndexError.INDEX_EXCEEDS_UPPER_BOUNDS(@"Tried to set index $(index) on a vector with $(n_items) item(s)");
  98. }
  99. else {
  100. array_write(index, value);
  101. rw_lock.writer_unlock();
  102. return;
  103. }
  104. rw_lock.writer_unlock();
  105. throw e;
  106. }
  107. private void ensure_room(int items) {
  108. if(array.length <= n_items + items) {
  109. array.resize(array.length * 2);
  110. }
  111. }
  112. public void remove_at(int index) throws IndexError {
  113. var e = remove_internal(index);
  114. if(e != null) {
  115. throw e;
  116. }
  117. }
  118. private IndexError? remove_internal(uint index) {
  119. IndexError? e = null;
  120. rw_lock.writer_lock();
  121. if(index < 0) {
  122. e = new IndexError.INDEX_EXCEEDS_LOWER_BOUNDS("Index is less than 0");
  123. }
  124. else if(index >= n_items) {
  125. e = new IndexError.INDEX_EXCEEDS_UPPER_BOUNDS(@"Tried to set index $(index) on a vector with $(n_items) item(s)");
  126. }
  127. else {
  128. for(uint i = index; i < n_items; i++) {
  129. if(i+1 < n_items) {
  130. array_write(i, array_read(i+1));
  131. continue;
  132. }
  133. array[i] = null;
  134. }
  135. n_items --;
  136. }
  137. rw_lock.writer_unlock();
  138. return e;
  139. }
  140. public override T last(owned PredicateDelegate<T>? predicate = null) throws SequenceError {
  141. if(predicate != null) {
  142. return base.last((owned)predicate);
  143. }
  144. SequenceError? e = null;
  145. rw_lock.reader_lock();
  146. if(n_items == 0) {
  147. e = new SequenceError.NO_ELEMENTS("The sequence contains no elements");
  148. }
  149. else {
  150. var item = array_read(n_items -1);
  151. rw_lock.reader_unlock();
  152. return item;
  153. }
  154. rw_lock.reader_unlock();
  155. throw e;
  156. }
  157. public override T? last_or_default(owned PredicateDelegate<T>? predicate = null) {
  158. if(predicate != null) {
  159. return base.last_or_default((owned)predicate);
  160. }
  161. rw_lock.reader_lock();
  162. if(n_items == 0) {
  163. rw_lock.reader_unlock();
  164. return null;
  165. }
  166. else {
  167. var item = array_read(n_items -1);
  168. rw_lock.reader_unlock();
  169. return item;
  170. }
  171. }
  172. private void array_write(uint index, T item) {
  173. if(safe_write != null) {
  174. safe_write(array, index, item);
  175. return;
  176. }
  177. array[index] = item;
  178. }
  179. private T array_read(uint index) {
  180. if(safe_read != null) {
  181. return safe_read(array, index);
  182. }
  183. return array[index];
  184. }
  185. public uint? first_index_of(PredicateDelegate<T> predicate) {
  186. var i = -1;
  187. foreach (var item in this) {
  188. i++;
  189. if(predicate(item)) {
  190. return i;
  191. }
  192. }
  193. return null;
  194. }
  195. public void remove_first_where(Invercargill.PredicateDelegate<T> predicate) {
  196. remove_internal(first_index_of(predicate));
  197. }
  198. public void remove_all_where(Invercargill.PredicateDelegate<T> predicate) {
  199. with_positions()
  200. .where(i => predicate(i.item))
  201. .select<int>(i => i.position)
  202. .iterate(i => remove_internal(i));
  203. }
  204. public void clear() {
  205. n_items = 0;
  206. array = new T[INITIAL_SIZE];
  207. }
  208. }
  209. }