1 module neton.util.Queue; 2 import core.sync.mutex; 3 4 class Queue(T) 5 { 6 private T[] _queue; 7 private Mutex _mutex; 8 9 this() 10 { 11 _mutex = new Mutex(); 12 } 13 14 int length() 15 { 16 return cast(int)(_queue.length); 17 } 18 19 // bool less(int i, int j) 20 // { 21 // return _queue[i].time < _queue[j].time; 22 // } 23 24 void swap(int i, int j) 25 { 26 _mutex.lock(); 27 scope(exit) _mutex.unlock(); 28 29 auto temp = _queue[j]; 30 _queue[j] = _queue[i]; 31 _queue[i] = temp; 32 } 33 34 void push(T item) 35 { 36 _mutex.lock(); 37 scope(exit) _mutex.unlock(); 38 39 _queue ~= item; 40 } 41 42 T pop() 43 { 44 _mutex.lock(); 45 scope(exit) _mutex.unlock(); 46 47 auto old = _queue.dup; 48 auto n = old.length; 49 if(n == 0) 50 return T.init; 51 auto item = old[n - 1]; 52 _queue = old[0 .. n - 1]; 53 return item; 54 } 55 56 void clear() 57 { 58 _queue.length = 0; 59 } 60 61 T get(int idx) 62 { 63 if(idx >= length()) 64 return T.init; 65 return _queue[idx]; 66 } 67 }