directvect.h 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // Copyright (C) 2003 Mooffie <mooffie@typo.co.il>
  2. //
  3. // This program is free software; you can redistribute it and/or modify
  4. // it under the terms of the GNU General Public License as published by
  5. // the Free Software Foundation; either version 2 of the License, or
  6. // (at your option) any later version.
  7. //
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
  16. #ifndef BDE_SIMPLEVEC_H
  17. #define BDE_SIMPLEVEC_H
  18. #include <vector>
  19. // For some vectors we use our DirectVector instead of STL's vector.
  20. //
  21. // Why's that?
  22. //
  23. // In this application I use vector<T> mostly as an "enhanced" array.
  24. // Unfortunately, STL's vector<T>::iterator is not necessarily a pointer to
  25. // T. That's the case in newer STL libraries provided with GNU C++.
  26. //
  27. // DirectVector accepts T* arguments.
  28. template <class T>
  29. class DirectVector {
  30. std::vector<T> vec;
  31. public:
  32. typedef size_t size_type;
  33. DirectVector(): vec() {}
  34. DirectVector(size_type n): vec(n) {}
  35. DirectVector(size_type n, const T& t): vec(n, t) {}
  36. DirectVector(const DirectVector& other_vec): vec(other_vec.vec) {}
  37. DirectVector(const T *first, const T *last): vec(first, last) {}
  38. DirectVector& operator=(const DirectVector& other_vec) {
  39. vec.operator=(other_vec.vec);
  40. return *this;
  41. }
  42. size_type size() const { return vec.size(); }
  43. size_type capacity() const { return vec.capacity(); }
  44. bool empty() const { return vec.empty(); }
  45. T* begin() { return &vec[0]; }
  46. T* end() { return (begin() + size()); }
  47. const T* begin() const { return &vec[0]; }
  48. const T* end() const { return (begin() + size()); }
  49. T& operator[] (size_type n) { return vec[n]; }
  50. const T& operator[] (size_type n) const { return vec[n]; }
  51. void reserve(size_type n) { vec.reserve(n); }
  52. void resize(size_type n) { vec.resize(n); }
  53. T& back() { return vec.back(); }
  54. T& front() { return vec.front(); }
  55. const T& front() const { return vec.front(); }
  56. const T& back() const { return vec.back(); }
  57. void push_back(const T& x) { vec.push_back(x); }
  58. void pop_back() { vec.pop_back(); }
  59. void swap(DirectVector& other_vec) { vec.swap(other_vec); }
  60. void clear() { vec.clear(); }
  61. // Note the "&*expression" syntax. "*" dereferences the iterator
  62. // and "&" gives us the pointer we need.
  63. T* insert(T* pos, const T& x) {
  64. return &*vec.insert(vec.begin() + (pos - begin()), x);
  65. }
  66. void insert(T* pos, const T* first, const T* last) {
  67. vec.insert(vec.begin() + (pos - begin()), first, last);
  68. }
  69. void insert(T* pos, size_type n, const T& x) {
  70. vec.insert(vec.begin() + (pos - begin()), n, x);
  71. }
  72. T* erase(T* pos) {
  73. return &*vec.erase(vec.begin() + (pos - begin()));
  74. }
  75. T* erase(T* first, T* last) {
  76. return &*vec.erase(vec.begin() + (first - begin()),
  77. vec.begin() + (last - begin()));
  78. }
  79. bool operator==(const DirectVector &other) const { return vec == other.vec; }
  80. bool operator!=(const DirectVector &other) const { return vec != other.vec; }
  81. bool operator<(const DirectVector &other) const { return vec < other.vec; }
  82. };
  83. #endif