Opera 12.15 Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

VectorBase.h 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /* -*- Mode: c++; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*-
  2. **
  3. ** Copyright (C) 1995-2011 Opera Software ASA. All rights reserved.
  4. **
  5. ** This file is part of the Opera web browser.
  6. ** It may not be distributed under any circumstances.
  7. */
  8. #ifndef VECTORBASE_H
  9. #define VECTORBASE_H
  10. #include "modules/search_engine/TypeDescriptor.h"
  11. /**
  12. * @brief General-pupose container for any types or pointers to types,
  13. * capable to replace OpVector, OpAutoVector, OpGenericVector and OpINT32Vector while providing more functionality.
  14. * @author Pavel Studeny <pavels@opera.com>
  15. */
  16. class VectorBase : public NonCopyable
  17. {
  18. public:
  19. /**
  20. * @param allocator description of basic oparations on the type; use predefined PtrDescriptor<T> for pointers
  21. */
  22. VectorBase(const TypeDescriptor &allocator);
  23. /**
  24. * destruct all data left in the Vector
  25. */
  26. ~VectorBase(void) {Clear();}
  27. /**
  28. * direct access to the items; can be useful together with SetCount(...) to fill the Vector with block of data from the disk
  29. */
  30. void *Ptr(void) const {return m_items;}
  31. /**
  32. * @param vec Vector to copy from
  33. */
  34. CHECK_RESULT(OP_STATUS DuplicateOf(const VectorBase& vec));
  35. /**
  36. * move all data into itself, comparison and destruction functions aren't changed
  37. * @param vec will be empty after the operation
  38. */
  39. void TakeOver(VectorBase &vec);
  40. /**
  41. * destruct all data and delete them from the Vector
  42. */
  43. void Clear(void);
  44. /**
  45. * @param size number of items to reserve space for in advance
  46. */
  47. CHECK_RESULT(OP_STATUS Reserve(UINT32 size));
  48. /**
  49. * change the number of items; doesn't initialize nor destruct any new items/left overs
  50. * @param count new number of items the Vector will contain, can be both lower and higher than the current count
  51. */
  52. CHECK_RESULT(OP_STATUS SetCount(UINT32 count));
  53. /**
  54. * destruct the item on the given place and set a new one instead
  55. */
  56. CHECK_RESULT(OP_STATUS Replace(UINT32 idx, const void *item));
  57. /**
  58. * insert an item to the given position, already present items from this position on will be moved up
  59. */
  60. CHECK_RESULT(OP_STATUS Insert(UINT32 idx, const void *item));
  61. /**
  62. * insert an item into a sorted data on its position
  63. */
  64. CHECK_RESULT(OP_STATUS Insert(const void *item));
  65. /**
  66. * insert an item to the end of the vector
  67. */
  68. CHECK_RESULT(OP_STATUS Add(const void *item)) {return Insert(m_count, item);}
  69. /**
  70. * remove item from the vector if it exists; item isn't destructed.
  71. * It is assumed that you have taken over the contents of item by
  72. * copying the bits, not by using the TypeDescriptor assignment function.
  73. */
  74. BOOL RemoveByItem(const void *item);
  75. /**
  76. * remove item at the given position. Does not call destructor.
  77. * The removed data will be placed in item.
  78. * If item==NULL, it is assumed that you have already taken over the
  79. * contents at the given index by copying the bits, not by using the
  80. * TypeDescriptor assignment function.
  81. */
  82. void Remove(void *item, INT32 idx);
  83. /**
  84. * remove item from the vector if it exists; item (in the vector) is destructed
  85. */
  86. BOOL DeleteByItem(void *item);
  87. /**
  88. * remove a range of items from the vector; items are destructed
  89. */
  90. void Delete(INT32 idx, UINT32 count = 1);
  91. /**
  92. * find an item in an unsorted Vector. Returns -1 if not found.
  93. */
  94. INT32 Find(const void *item) const;
  95. /**
  96. * @return item at the given position
  97. */
  98. const void *Get(UINT32 idx) const { return m_items + idx * m_allocator.size; }
  99. /**
  100. * @return number of items in the Vector
  101. */
  102. UINT32 GetCount(void) const { return m_count; }
  103. /**
  104. * @return maximum number of items the Vector can hold with the currently allocated memory
  105. */
  106. UINT32 GetSize(void) const { return m_size; }
  107. /**
  108. * sort the items in the Vector into an ascending order and remove duplicates
  109. */
  110. CHECK_RESULT(OP_STATUS Sort(void));
  111. /**
  112. * find an item in a sorted vector
  113. * @return position of the item or a position where the item would be if it was in the Vector
  114. */
  115. INT32 Search(const void *item) const {return Search(item, 0, m_count);}
  116. /**
  117. * find an item in a sorted vector in the given range
  118. */
  119. INT32 Search(const void *item, UINT32 start, UINT32 end) const;
  120. /**
  121. * @return a reference to the item at given position
  122. */
  123. void *operator[](UINT32 idx) const { return m_items + idx * m_allocator.size; }
  124. /**
  125. * Add the elements to a sorted vector which aren't present already. Uses the assignment function in type descriptor.
  126. */
  127. CHECK_RESULT(OP_STATUS Unite(const VectorBase &vec));
  128. CHECK_RESULT(static OP_STATUS Unite(VectorBase &result, const VectorBase &vec1, const VectorBase &vec2));
  129. /**
  130. * Remove all the elements from a sorted vector present in vec. Removed items are destructed.
  131. */
  132. void Differ(const VectorBase &vec);
  133. CHECK_RESULT(static OP_STATUS Differ(VectorBase &result, const VectorBase &vec1, const VectorBase &vec2));
  134. /**
  135. * Keep just the elements present in both. Others are destructed.
  136. */
  137. CHECK_RESULT(OP_STATUS Intersect(const VectorBase &vec));
  138. CHECK_RESULT(static OP_STATUS Intersect(VectorBase &result, const VectorBase &vec1, const VectorBase &vec2));
  139. /**
  140. * Keep just the elements for which "matches" return TRUE. Others are destructed.
  141. */
  142. void Filter(BOOL (*matches)(const void* item, void* custom_data), void* custom_data);
  143. /**
  144. * @return an estimate of the memory used by this data structure
  145. */
  146. #ifdef ESTIMATE_MEMORY_USED_AVAILABLE
  147. size_t EstimateMemoryUsed() const;
  148. #endif
  149. protected:
  150. CHECK_RESULT(OP_STATUS Grow(UINT32 new_size, UINT32 hole));
  151. CHECK_RESULT(OP_STATUS Shrink(UINT32 new_size, UINT32 hole, UINT32 count = 1));
  152. INT32 MergeSort(char* array, char* temp_array, INT32 left, INT32 right);
  153. char *m_items;
  154. TypeDescriptor m_allocator;
  155. UINT32 m_size;
  156. UINT32 m_count;
  157. };
  158. #endif // VECTORBASE_H