directvect.h 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. #include <sys/types.h>
  20. // For some vectors we use our DirectVector instead of STL's vector.
  21. //
  22. // Why's that?
  23. //
  24. // In this application I use vector<T> mostly as an "enhanced" array.
  25. // Unfortunately, STL's vector<T>::iterator is not necessarily a pointer to
  26. // T. That's the case in newer STL libraries provided with GNU C++.
  27. //
  28. // DirectVector accepts T* arguments.
  29. template <class T>
  30. class DirectVector {
  31. std::vector<T> vec;
  32. public:
  33. typedef size_t size_type;
  34. DirectVector(): vec() {}
  35. DirectVector(size_type n): vec(n) {}
  36. DirectVector(size_type n, const T& t): vec(n, t) {}
  37. DirectVector(const DirectVector& other_vec): vec(other_vec.vec) {}
  38. DirectVector(const T *first, const T *last): vec(first, last) {}
  39. DirectVector& operator=(const DirectVector& other_vec) {
  40. vec.operator=(other_vec.vec);
  41. return *this;
  42. }
  43. size_type size() const { return vec.size(); }
  44. size_type capacity() const { return vec.capacity(); }
  45. bool empty() const { return vec.empty(); }
  46. T* begin() { return &vec[0]; }
  47. T* end() { return (begin() + size()); }
  48. const T* begin() const { return &vec[0]; }
  49. const T* end() const { return (begin() + size()); }
  50. T& operator[] (size_type n) { return vec[n]; }
  51. const T& operator[] (size_type n) const { return vec[n]; }
  52. void reserve(size_type n) { vec.reserve(n); }
  53. void resize(size_type n) { vec.resize(n); }
  54. T& back() { return vec.back(); }
  55. T& front() { return vec.front(); }
  56. const T& front() const { return vec.front(); }
  57. const T& back() const { return vec.back(); }
  58. void push_back(const T& x) { vec.push_back(x); }
  59. void pop_back() { vec.pop_back(); }
  60. void swap(DirectVector& other_vec) { vec.swap(other_vec); }
  61. void clear() { vec.clear(); }
  62. // Note the "&*expression" syntax. "*" dereferences the iterator
  63. // and "&" gives us the pointer we need.
  64. T* insert(T* pos, const T& x) {
  65. return &*vec.insert(vec.begin() + (pos - begin()), x);
  66. }
  67. void insert(T* pos, const T* first, const T* last) {
  68. vec.insert(vec.begin() + (pos - begin()), first, last);
  69. }
  70. void insert(T* pos, size_type n, const T& x) {
  71. vec.insert(vec.begin() + (pos - begin()), n, x);
  72. }
  73. T* erase(T* pos) {
  74. return &*vec.erase(vec.begin() + (pos - begin()));
  75. }
  76. T* erase(T* first, T* last) {
  77. return &*vec.erase(vec.begin() + (first - begin()),
  78. vec.begin() + (last - begin()));
  79. }
  80. bool operator==(const DirectVector &other) const { return vec == other.vec; }
  81. bool operator!=(const DirectVector &other) const { return vec != other.vec; }
  82. bool operator<(const DirectVector &other) const { return vec < other.vec; }
  83. };
  84. #endif