1 /** 2 Module with multithreated vectors. 3 */ 4 module mutils.container_shared.shared_vector; 5 6 import std.algorithm : remove; 7 import std.experimental.allocator; 8 import std.experimental.allocator.mallocator; 9 10 import mutils.container_shared.shared_allocator; 11 import mutils.container.vector; 12 13 //////////////////// 14 15 class LockedVectorBuildIn(T){ 16 T[] array; 17 public: 18 bool empty(){ 19 return(array.length==0); 20 } 21 22 void add( T t ) { 23 synchronized( this ){ 24 array.assumeSafeAppend~=t; 25 } 26 } 27 void add( T[] t ) { 28 synchronized( this ){ 29 array.assumeSafeAppend~=t; 30 } 31 } 32 33 T pop( ) { 34 synchronized( this ){ 35 if(array.length==0)return T.init; 36 T obj=array[$-1]; 37 array=array.remove(array.length-1); 38 return obj; 39 } 40 } 41 42 } 43 44 class LockedVector(T){ 45 Vector!T array; 46 public: 47 this(){ 48 //array=Mallocator.instance.make!(Vector!T)(16); 49 } 50 ~this(){ 51 //Mallocator.instance.dispose(array); 52 } 53 bool empty(){ 54 return(array.length==0); 55 } 56 57 void add( T t ) { 58 synchronized( this ){ 59 array~=t; 60 } 61 } 62 void add( T[] t ) { 63 synchronized( this ){ 64 array~=t; 65 } 66 } 67 void removeElement( T elem ) { 68 synchronized( this ){ 69 array.removeElement(elem); 70 } 71 } 72 73 T pop( ) { 74 synchronized( this ){ 75 if(array.length==0)return T.init; 76 T obj=array[$-1]; 77 array.remove(array.length-1); 78 return obj; 79 } 80 } 81 auto opSlice(){ 82 return array[]; 83 } 84 85 //allocated by Mallocator.instance 86 Vector!T vectorCopy(){ 87 synchronized( this ){ 88 Vector!T vec;//=Mallocator.instance.make!(Vector!T)(array.length); 89 vec~=array[]; 90 return vec; 91 } 92 } 93 Vector!T vectorCopyWithReset(){ 94 if(array.length==0)return Vector!T(); 95 synchronized( this ){ 96 Vector!T vec;//=Mallocator.instance.make!(Vector!T)(array.length); 97 vec~=array[]; 98 array.reset; 99 return vec; 100 } 101 } 102 103 }