00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #ifndef GROWABLE_VECTOR_H
00013 #define GROWABLE_VECTOR_H
00014
00015 #include <vector>
00016 #include <algorithm>
00017 #include <functional>
00018
00019
00020
00021
00022 template <typename T> class growable_vector {
00023 public:
00024 typedef std::vector<T> container_type;
00025 typedef typename container_type::size_type size_type;
00026
00027
00028
00029
00030
00031
00032 T operator[](size_type index) const {
00033 if (index >= container.size())
00034 return T();
00035 return container[index];
00036 }
00037
00038
00039
00040
00041
00042
00043
00044 T & operator[](size_type index) {
00045 if (index >= container.size())
00046 container.resize(index + 1);
00047 return container[index];
00048 }
00049
00050
00051
00052
00053
00054 growable_vector<T> & operator+=(growable_vector<T> const & rhs) {
00055 if (rhs.container.size() > container.size())
00056 container.resize(rhs.container.size());
00057
00058 size_type min_size = min(container.size(), rhs.container.size());
00059 for (size_type i = 0 ; i < min_size; ++i)
00060 container[i] += rhs.container[i];
00061
00062 return *this;
00063 }
00064
00065
00066
00067
00068
00069
00070 growable_vector<T> & operator-=(growable_vector<T> const & rhs) {
00071 if (rhs.container.size() > container.size())
00072 container.resize(rhs.container.size());
00073
00074 size_type min_size = min(container.size(), rhs.container.size());
00075 for (size_type i = 0 ; i < min_size; ++i)
00076 container[i] -= rhs.container[i];
00077
00078 return *this;
00079 }
00080
00081
00082
00083 size_type size() const {
00084 return container.size();
00085 }
00086
00087
00088
00089 void fill(size_type size, T const & value) {
00090 container.resize(size, value);
00091 }
00092
00093
00094
00095 bool zero() const {
00096 return std::find_if(container.begin(), container.end(),
00097 std::bind2nd(std::not_equal_to<T>(), T()))
00098 == container.end();
00099 }
00100
00101 private:
00102 container_type container;
00103 };
00104
00105 #endif // GROWABLE_VECTOR_H