Mem Simple.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /******************************************************************************
  2. Use 'Mems' for simple continuous memory based dynamic array container.
  3. 'Mems' stores elements in continuous memory, for example:
  4. [ABCDE]
  5. 'Mems' memory container works very similar to 'Memc' except:
  6. -'Mems' uses less memory than 'Memc'
  7. -'Mems' is slower for adding/removing elements than 'Memc'
  8. It is perfectly suited for data initialized only once.
  9. 'Mems' will allocate only as much memory as needed, this means
  10. that when creating new elements, the whole array needs to be
  11. reallocated, thus changing the memory address of all elements.
  12. /******************************************************************************/
  13. T1(const_mem_addr TYPE) struct Mems // Simple Continuous Memory Based Container
  14. {
  15. // manage
  16. Mems& clear(); // remove all elements and free helper memory
  17. Mems& del (); // remove all elements and free helper memory
  18. // get / set
  19. Int elms ()C; // number of elements
  20. UInt elmSize ()C; // size of element
  21. UInt memUsage()C; // memory usage
  22. TYPE* data ( ) ; // get pointer to the start of the elements
  23. C TYPE* data ( )C; // get pointer to the start of the elements
  24. TYPE* addr (Int i) ; // get i-th element address, null is returned if index is out of range
  25. C TYPE* addr (Int i)C; // get i-th element address, null is returned if index is out of range
  26. TYPE& operator[](Int i) ; // get i-th element, accessing element out of range is an invalid operation and may cause undefined behavior
  27. C TYPE& operator[](Int i)C; // get i-th element, accessing element out of range is an invalid operation and may cause undefined behavior
  28. TYPE& operator()(Int i) ; // get i-th element, accessing element out of range will cause creation of all elements before it, memory of those elements will be first zeroed before calling their constructor
  29. TYPE& first ( ) ; // get first element
  30. C TYPE& first ( )C; // get first element
  31. TYPE& last ( ) ; // get last element
  32. C TYPE& last ( )C; // get last element
  33. TYPE& New ( ) ; // create new element at the end , this method changes the memory address of all elements
  34. TYPE& NewAt (Int i) ; // create new element at i-th position, all old elements starting from i-th position will be moved to the right, this method changes the memory address of all elements
  35. Int index (C TYPE *elm)C; // get index of element in container, -1 on fail , testing is done by comparing elements memory address only
  36. Bool contains(C TYPE *elm)C; // check if memory container actually contains element, testing is done by comparing elements memory address only
  37. // remove
  38. Mems& removeLast( ); // remove last element , all remaining elements are kept in the same order, this method changes the memory address of all elements
  39. Mems& remove ( Int i , Bool keep_order=false); // remove i-th element , all remaining elements are kept in the same order, this method changes the memory address of all elements, 'keep_order'=this parameter is ignored for 'Mems' because it always keeps order (it is kept here only for compatibility with other memory containers)
  40. Mems& removeData(C TYPE *elm, Bool keep_order=false); // remove element by giving its memory address, all remaining elements are kept in the same order, this method changes the memory address of all elements, 'keep_order'=this parameter is ignored for 'Mems' because it always keeps order (it is kept here only for compatibility with other memory containers)
  41. TYPE popFirst( Bool keep_order=true); // get first element and remove it from the container, if 'keep_order'=true then moves all elements after i-th to the left (keeping order)
  42. TYPE pop (Int i, Bool keep_order=true); // get i-th element and remove it from the container, if 'keep_order'=true then moves all elements after i-th to the left (keeping order)
  43. TYPE pop ( ); // get last element and remove it from the container
  44. Mems& setNum (Int num); // set number of elements to 'num' , this method changes the memory address of all elements
  45. Mems& setNumZero(Int num); // set number of elements to 'num', memory of new elements will be first zeroed before calling their constructor, this method changes the memory address of all elements
  46. Int addNum (Int num); // add 'num' elements, return index of first added element , this method changes the memory address of all elements
  47. // values
  48. T1(VALUE) Int find (C VALUE &value)C {REPA(T)if(T[i]==value)return i; return -1; } // check if 'value' is present in container and return its index, -1 if not found
  49. T1(VALUE) Bool has (C VALUE &value)C {return find(value)>=0; } // check if 'value' is present in container
  50. T1(VALUE) Mems& add (C VALUE &value) {New()=value; return T; } // add 'value' to container , this method changes the memory address of all elements
  51. T1(VALUE) Bool include(C VALUE &value) {if(!has(value)){add(value); return true;} return false; } // include 'value' if it's not already present in container, returns true if value wasn't present and has been added , this method changes the memory address of all elements
  52. T1(VALUE) Bool exclude(C VALUE &value) {Int i=find(value); if(i>=0){remove(i); return true ;} return false;} // exclude 'value' if present in container , returns true if value was present and has been removed, this method changes the memory address of all elements
  53. T1(VALUE) Bool toggle (C VALUE &value) {Int i=find(value); if(i>=0){remove(i); return false;} add(value); return true ;} // toggle 'value' presence in container , returns true if value is now present in container , this method changes the memory address of all elements
  54. T1(VALUE) Bool binarySearch (C VALUE &value, Int &index, Int compare(C TYPE &a, C VALUE &b)=Compare)C; // search sorted container for presence of 'value' and return if it was found in the container, 'index'=if the function returned true then this index points to the location where the 'value' is located in the container, if the function returned false then it means that 'value' was not found in the container however the 'index' points to the place where it should be added in the container while preserving sorted data, 'index' will always be in range (0..elms) inclusive
  55. T1(VALUE) Bool binaryHas (C VALUE &value, Int compare(C TYPE &a, C VALUE &b)=Compare)C {Int i; return binarySearch(value, i, compare); } // check if 'value' (using binary search) is present in container
  56. T1(VALUE) TYPE* binaryFind (C VALUE &value, Int compare(C TYPE &a, C VALUE &b)=Compare) {Int i; return binarySearch(value, i, compare) ? &T[i] : null; } // check if 'value' (using binary search) is present in container and return it, null on fail
  57. T1(VALUE) C TYPE* binaryFind (C VALUE &value, Int compare(C TYPE &a, C VALUE &b)=Compare)C {return ConstCast(T).binaryFind(value, compare); } // check if 'value' (using binary search) is present in container and return it, null on fail
  58. T1(VALUE) Mems& binaryAdd (C VALUE &value, Int compare(C TYPE &a, C VALUE &b)=Compare) {Int i; binarySearch(value, i, compare); NewAt (i)=value; return T;} // add 'value' (using binary search) , this method changes the memory address of all elements
  59. T1(VALUE) Bool binaryInclude(C VALUE &value, Int compare(C TYPE &a, C VALUE &b)=Compare) {Int i; if( !binarySearch(value, i, compare)){NewAt (i)=value; return true;} return false;} // include 'value' (using binary search) if it's not already present in container, returns true if value wasn't present and has been added , this method changes the memory address of all elements
  60. T1(VALUE) Bool binaryExclude(C VALUE &value, Int compare(C TYPE &a, C VALUE &b)=Compare) {Int i; if( binarySearch(value, i, compare)){remove(i) ; return true;} return false;} // exclude 'value' (using binary search) if present in container , returns true if value was present and has been removed, this method changes the memory address of all elements
  61. T1(VALUE) Bool binaryToggle (C VALUE &value, Int compare(C TYPE &a, C VALUE &b)=Compare) {Int i; if( !binarySearch(value, i, compare)){NewAt (i)=value; return true;} remove(i); return false;} // toggle 'value' (using binary search) presence in container , returns true if value is now present in container , this method changes the memory address of all elements
  62. // order
  63. Mems& sort(Int compare(C TYPE &a, C TYPE &b)); // sort elements with custom comparing function
  64. Mems& reverseOrder( ); // reverse order of elements
  65. Mems& randomizeOrder( ); // randomize order of elements
  66. Mems& rotateOrder(Int offset ); // rotate order of elements, changes the order of elements so "new_index=old_index+offset", 'offset'=offset of moving the original indexes into target indexes (-Inf..Inf)
  67. Mems& swapOrder(Int i , Int j ); // swap order of 'i' and 'j' elements
  68. Mems& moveElm (Int elm, Int new_index ); // move 'elm' element to new position located at 'new_index'
  69. // misc
  70. Mems& operator=(C Mems <TYPE > &src); // copy elements using assignment operator
  71. Mems& operator=(C Memc <TYPE > &src); // copy elements using assignment operator
  72. template<Int size> Mems& operator=(C Memt <TYPE, size> &src); // copy elements using assignment operator
  73. Mems& operator=(C Memb <TYPE > &src); // copy elements using assignment operator
  74. Mems& operator=(C Memx <TYPE > &src); // copy elements using assignment operator
  75. Mems& operator=(C Meml <TYPE > &src); // copy elements using assignment operator
  76. template<Int size> Mems& operator=(C MemPtr<TYPE, size> &src); // copy elements using assignment operator
  77. Mems& operator=( Mems <TYPE > &&src); // copy elements using assignment operator
  78. #if EE_PRIVATE
  79. void copyTo ( TYPE *dest)C; // copy raw memory of all elements to 'dest'
  80. Mems& copyFrom(C TYPE *src ) ; // copy raw memory of all elements from 'src'
  81. void setFrom( TYPE* &data, Int elms); // this takes ownership of 'data' and sets that pointer to null
  82. void setTemp( TYPE* data, Int elms); // this is not safe !!
  83. #endif
  84. // io
  85. Bool save(File &f); Bool save(File &f)C; // save elements with their own 'save' method, this method first saves number of current elements, and then for each element calls its 'save' method, false on fail
  86. Bool load(File &f); // load elements with their own 'load' method, this method first loads number of saved elements, and then for each element calls its 'load' method, false on fail
  87. T1(USER) Bool save(File &f, C USER &user)C; // save elements with their own 'save' method and 'user' parameter, this method first saves number of current elements, and then for each element calls its 'save' method, false on fail
  88. T1(USER) Bool load(File &f, C USER &user) ; // load elements with their own 'load' method and 'user' parameter, this method first loads number of saved elements, and then for each element calls its 'load' method, false on fail
  89. T2(USER, USER1) Bool save(File &f, C USER &user, C USER1 &user1)C; // save elements with their own 'save' method and 'user, user1' parameter, this method first saves number of current elements, and then for each element calls its 'save' method, false on fail
  90. T2(USER, USER1) Bool load(File &f, C USER &user, C USER1 &user1) ; // load elements with their own 'load' method and 'user, user1' parameter, this method first loads number of saved elements, and then for each element calls its 'load' method, false on fail
  91. Bool saveRaw (File &f)C; // save raw memory of elements (number of elements + elements raw memory), false on fail
  92. Bool loadRaw (File &f) ; // load raw memory of elements (number of elements + elements raw memory), false on fail
  93. Bool saveRawData(File &f)C; // save raw memory of elements ( elements raw memory), false on fail
  94. Bool loadRawData(File &f) ; // load raw memory of elements ( elements raw memory), false on fail
  95. #if EE_PRIVATE
  96. Bool _saveRaw(File &f)C; // save raw memory of elements (number of elements + elements raw memory), false on fail, deprecated - do not use
  97. Bool _loadRaw(File &f) ; // load raw memory of elements (number of elements + elements raw memory), false on fail, deprecated - do not use
  98. Bool _save (File &f)C; // save elements with their own 'save' method, this method first saves number of current elements, and then for each element calls its 'save' method, false on fail, deprecated - do not use
  99. Bool _load (File &f) ; // load elements with their own 'load' method, this method first loads number of saved elements, and then for each element calls its 'load' method, false on fail, deprecated - do not use
  100. #endif
  101. ~Mems( );
  102. Mems( );
  103. Mems(C Mems &src);
  104. Mems( Mems &&src);
  105. private:
  106. TYPE *_data;
  107. Int _elms;
  108. };
  109. /******************************************************************************/
  110. #if EE_PRIVATE
  111. T1(const_mem_addr TYPE) STRUCT(FixedMems , Mems<TYPE>) // Unresizable Mems container
  112. #else
  113. T1(const_mem_addr TYPE) STRUCT_PRIVATE(FixedMems , Mems<TYPE>) // Unresizable Mems container
  114. #endif
  115. //{
  116. // get / set
  117. Int elms ()C {return super::elms ();} // number of elements
  118. UInt elmSize ()C {return super::elmSize ();} // size of element
  119. UInt memUsage()C {return super::memUsage();} // memory usage
  120. TYPE* data ( ) {return super::data ( );} // get pointer to the start of the elements
  121. C TYPE* data ( )C {return super::data ( );} // get pointer to the start of the elements
  122. TYPE* addr (Int i) {return super::addr (i);} // get i-th element address, null is returned if index is out of range
  123. C TYPE* addr (Int i)C {return super::addr (i);} // get i-th element address, null is returned if index is out of range
  124. TYPE& operator[](Int i) {return super::operator[](i);} // get i-th element, accessing element out of range is an invalid operation and may cause undefined behavior
  125. C TYPE& operator[](Int i)C {return super::operator[](i);} // get i-th element, accessing element out of range is an invalid operation and may cause undefined behavior
  126. TYPE& first ( ) {return super::first ( );} // get first element
  127. C TYPE& first ( )C {return super::first ( );} // get first element
  128. TYPE& last ( ) {return super::last ( );} // get last element
  129. C TYPE& last ( )C {return super::last ( );} // get last element
  130. Int index (C TYPE *elm)C {return super::index (elm);} // get index of element in container, -1 on fail , testing is done by comparing elements memory address only
  131. Bool contains(C TYPE *elm)C {return super::contains(elm);} // check if memory container actually contains element, testing is done by comparing elements memory address only
  132. };
  133. /******************************************************************************/
  134. T1(TYPE) Int Elms(C Mems<TYPE> &mems) {return mems.elms();}
  135. T1(TYPE) Int Elms(C FixedMems<TYPE> &mems) {return mems.elms();}
  136. /******************************************************************************/