growable_vector.h

Go to the documentation of this file.
00001 
00012 #ifndef GROWABLE_VECTOR_H
00013 #define GROWABLE_VECTOR_H
00014 
00015 #include <vector>
00016 #include <algorithm>
00017 #include <functional>
00018 
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 
00032     T operator[](size_type index) const {
00033         if (index >= container.size())
00034             return T();
00035         return container[index];
00036     }
00037 
00038 
00044     T & operator[](size_type index) {
00045         if (index >= container.size())
00046             container.resize(index + 1);
00047         return container[index];
00048     }
00049 
00050 
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 
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 
00083     size_type size() const {
00084         return container.size();
00085     }
00086 
00087 
00089     void fill(size_type size, T const & value) {
00090         container.resize(size, value);
00091     }
00092 
00093 
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

Generated on 8 Nov 2012 for Oprofile by  doxygen 1.6.1