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.

Vector.h 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 VECTOR_H
  9. #define VECTOR_H
  10. #include "modules/search_engine/VectorBase.h"
  11. #include "modules/search_engine/ResultBase.h"
  12. /**
  13. * @brief General-pupose container for any types or pointers to types,
  14. * capable to replace OpVector, OpAutoVector, OpGenericVector and OpINT32Vector while providing more functionality.
  15. * @author Pavel Studeny <pavels@opera.com>
  16. */
  17. template <typename T> class TVector : public VectorBase
  18. {
  19. public:
  20. /**
  21. * no destructor for T, comparison uses a default operator<
  22. */
  23. TVector(void) : VectorBase(DefDescriptor<T>()) {}
  24. /**
  25. * @param allocator description of basic oparations on the type; use predefined PtrDescriptor<T> for pointers
  26. */
  27. TVector(const TypeDescriptor &allocator) : VectorBase(allocator) {}
  28. /**
  29. * @param compare BOOL function saying which item is less
  30. * @param destruct code to be performed as destruction of T either on a request or interanlly in various methods,
  31. * use Comparison::DefaultDelete if T is a normal pointer
  32. */
  33. TVector(TypeDescriptor::ComparePtr compare, TypeDescriptor::DestructPtr destruct = NULL) :
  34. VectorBase(TypeDescriptor(sizeof(T), &DefDescriptor<T>::Assign,
  35. compare, destruct == NULL ? &DefDescriptor<T>::Destruct : destruct
  36. #ifdef ESTIMATE_MEMORY_USED_AVAILABLE
  37. , &DefDescriptor<T>::EstimateMemoryUsed
  38. #endif
  39. )) {}
  40. /**
  41. * @param destruct code to be performed as destruction of T either on a request or interanlly in various methods,
  42. * use Comparison::DefaultDelete if T is a normal pointer
  43. */
  44. TVector(TypeDescriptor::DestructPtr destruct) :
  45. VectorBase(TypeDescriptor(sizeof(T), &DefDescriptor<T>::Assign, &DefDescriptor<T>::Compare, destruct
  46. #ifdef ESTIMATE_MEMORY_USED_AVAILABLE
  47. , &DefDescriptor<T>::EstimateMemoryUsed
  48. #endif
  49. )) {}
  50. /**
  51. * direct access to the items; can be useful together with SetCount(...) to fill the Vector with block of data from the disk
  52. */
  53. T *Ptr(void) const {return (T*)VectorBase::Ptr();}
  54. /**
  55. * destruct the item on the given place and set a new one instead
  56. */
  57. CHECK_RESULT(OP_STATUS Replace(UINT32 idx, const T &item)) {return VectorBase::Replace(idx, &item);}
  58. /**
  59. * insert an item to the given position, already present items from this position on will be moved up
  60. */
  61. CHECK_RESULT(OP_STATUS Insert(UINT32 idx, const T &item)) {return VectorBase::Insert(idx, &item);}
  62. /**
  63. * insert an item into a sorted data on its position
  64. */
  65. CHECK_RESULT(OP_STATUS Insert(const T &item)) {return VectorBase::Insert(&item);}
  66. /**
  67. * insert an item to the end of the vector
  68. */
  69. CHECK_RESULT(OP_STATUS Add(const T &item)) {return VectorBase::Add(&item);}
  70. /**
  71. * remove item from the vector if it exists; item isn't destructed.
  72. * It is assumed that you have taken over the contents of item by
  73. * copying the bits, not by using the TypeDescriptor assignment function.
  74. */
  75. BOOL RemoveByItem(const T &item) {return VectorBase::RemoveByItem(&item);}
  76. /**
  77. * remove item at the given position
  78. * @return the removed item; destruction is the responsibility of the caller
  79. */
  80. T Remove(INT32 idx) {T t; VectorBase::Remove(&t, idx); return t;}
  81. /**
  82. * remove item from the vector if it exists; item (in the vector) is destructed
  83. */
  84. BOOL DeleteByItem(T &item) {return VectorBase::DeleteByItem(&item);}
  85. /**
  86. * find an item in an unsorted Vector
  87. * @return position of the item or -1 if not found
  88. */
  89. INT32 Find(const T &item) const {return VectorBase::Find(&item);}
  90. /**
  91. * @return item at the given position
  92. */
  93. const T &Get(UINT32 idx) const {return *(T *)VectorBase::Get(idx);}
  94. /**
  95. * find an item in a sorted vector
  96. * @return position of the item or a position where the item would be if it was in the Vector
  97. */
  98. INT32 Search(const T &item) const {return VectorBase::Search(&item);}
  99. /**
  100. * find an item in a sorted vector in the given range
  101. */
  102. INT32 Search(const T &item, UINT32 start, UINT32 end) const {return VectorBase::Search(&item, start, end);}
  103. /**
  104. * Remove all items that do not match the given filter
  105. */
  106. void Filter(SearchFilter<T>& filter) {VectorBase::Filter(filter_matcher, &filter);}
  107. /**
  108. * @return a reference to the item at given position
  109. */
  110. T &operator[](UINT32 idx) const {return *(T *)VectorBase::operator[](idx);}
  111. private:
  112. static BOOL filter_matcher(const void* item, void* filter)
  113. {
  114. return reinterpret_cast<SearchFilter<T>*>(filter)->Matches(*reinterpret_cast<const T*>(item));
  115. }
  116. };
  117. #endif // VECTOR_H