123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238 |
- namespace Invercargill.DataStructures {
- public class Vector<T> : Enumerable<T>, Lot<T>, ReadOnlyCollection<T>, ReadOnlyAddressable<T>, Collection<T>, Addressable<T> {
- private T[] array;
- private int n_items = 0;
- private SafeReadFunc<T>? safe_read;
- private SafeWriteFunc<T>? safe_write;
- private RWLock rw_lock;
- private const int INITIAL_SIZE = 2;
- public Vector() {
- rw_lock = RWLock();
- array = new T[INITIAL_SIZE];
- safe_read = get_safe_read_function_for<T>();
- safe_write = get_safe_write_function_for<T>();
- }
- public override int? peek_count() {
- return n_items;
- }
- public override int count(PredicateDelegate<T>? predicate = null) {
- if(predicate == null) {
- return n_items;
- }
- return base.count(predicate);
- }
- public override EnumerableInfo get_info() {
- return new EnumerableInfo.infer_ultimate (this, EnumerableCategory.IN_MEMORY);
- }
- public override Tracker<T> get_tracker() {
- return new AddressableTracker<T>(this);
- }
- public override T[] to_array () {
- rw_lock.reader_lock();
- var a2 = new T[n_items];
- if(safe_write != null) {
- for(var i = 0; i < n_items; i++) {
- safe_write(a2, i, array_read(i));
- }
- }
- else {
- for(var i = 0; i < n_items; i++) {
- a2[i] = array[i];
- }
- }
- rw_lock.reader_unlock();
- return a2;
- }
- public void add(T item) {
- rw_lock.writer_lock();
- ensure_room(1);
- array_write(n_items, item);
- n_items++;
- rw_lock.writer_unlock();
- }
-
- public override void add_all(Enumerable<T> items) {
- rw_lock.writer_lock();
- foreach (var item in items) {
- ensure_room(1);
- array_write(n_items, item);
- n_items++;
- }
- rw_lock.writer_unlock();
- }
- public new T @get(uint index) throws IndexError {
- IndexError? e = null;
- rw_lock.reader_lock();
- if(index < 0) {
- e = new IndexError.INDEX_EXCEEDS_LOWER_BOUNDS("Index is less than 0");
- }
- else if(index >= n_items) {
- e = new IndexError.INDEX_EXCEEDS_UPPER_BOUNDS(@"Tried to access index $(index) on a vector with $(n_items) item(s)");
- }
- else {
- var item = array_read(index);
- rw_lock.reader_unlock();
- return item;
- }
- rw_lock.reader_unlock();
- throw e;
- }
- public bool try_get(uint index, out T value) {
- rw_lock.reader_lock();
- if(index >= 0 && index < n_items) {
- value = array_read(index);
- rw_lock.reader_unlock();
- return true;
- }
- rw_lock.reader_unlock();
- value = null;
- return false;
- }
- public new void @set(int index, T value) throws IndexError {
- IndexError? e = null;
- rw_lock.writer_lock();
- if(index < 0) {
- e = new IndexError.INDEX_EXCEEDS_LOWER_BOUNDS("Index is less than 0");
- }
- else if(index >= n_items) {
- e = new IndexError.INDEX_EXCEEDS_UPPER_BOUNDS(@"Tried to set index $(index) on a vector with $(n_items) item(s)");
- }
- else {
- array_write(index, value);
- rw_lock.writer_unlock();
- return;
- }
- rw_lock.writer_unlock();
- throw e;
- }
- private void ensure_room(int items) {
- if(array.length <= n_items + items) {
- array.resize(array.length * 2);
- }
- }
- public void remove_at(int index) throws IndexError {
- var e = remove_internal(index);
- if(e != null) {
- throw e;
- }
- }
- private IndexError? remove_internal(uint index) {
- IndexError? e = null;
- rw_lock.writer_lock();
- if(index < 0) {
- e = new IndexError.INDEX_EXCEEDS_LOWER_BOUNDS("Index is less than 0");
- }
- else if(index >= n_items) {
- e = new IndexError.INDEX_EXCEEDS_UPPER_BOUNDS(@"Tried to set index $(index) on a vector with $(n_items) item(s)");
- }
- else {
- for(uint i = index; i < n_items; i++) {
- if(i+1 < n_items) {
- array_write(i, array_read(i+1));
- continue;
- }
- array[i] = null;
- }
- n_items --;
- }
- rw_lock.writer_unlock();
- return e;
- }
- public override T last(owned PredicateDelegate<T>? predicate = null) throws SequenceError {
- if(predicate != null) {
- return base.last((owned)predicate);
- }
- SequenceError? e = null;
- rw_lock.reader_lock();
- if(n_items == 0) {
- e = new SequenceError.NO_ELEMENTS("The sequence contains no elements");
- }
- else {
- var item = array_read(n_items -1);
- rw_lock.reader_unlock();
- return item;
- }
- rw_lock.reader_unlock();
- throw e;
- }
- public override T? last_or_default(owned PredicateDelegate<T>? predicate = null) {
- if(predicate != null) {
- return base.last_or_default((owned)predicate);
- }
- rw_lock.reader_lock();
- if(n_items == 0) {
- rw_lock.reader_unlock();
- return null;
- }
- else {
- var item = array_read(n_items -1);
- rw_lock.reader_unlock();
- return item;
- }
- }
- private void array_write(uint index, T item) {
- if(safe_write != null) {
- safe_write(array, index, item);
- return;
- }
- array[index] = item;
- }
- private T array_read(uint index) {
- if(safe_read != null) {
- return safe_read(array, index);
- }
- return array[index];
- }
- public uint? first_index_of(PredicateDelegate<T> predicate) {
- var i = -1;
- foreach (var item in this) {
- i++;
- if(predicate(item)) {
- return i;
- }
- }
- return null;
- }
- public void remove_first_where(Invercargill.PredicateDelegate<T> predicate) {
- remove_internal(first_index_of(predicate));
- }
- public void remove_all_where(Invercargill.PredicateDelegate<T> predicate) {
- with_positions()
- .where(i => predicate(i.item))
- .select<int>(i => i.position)
- .iterate(i => remove_internal(i));
- }
- public void clear() {
- n_items = 0;
- array = new T[INITIAL_SIZE];
- }
- }
- }
|