Home | History | Annotate | Download | only in libutil++
      1 /**
      2  * @file growable_vector.h
      3  * Auto-expanding vector type
      4  *
      5  * @remark Copyright 2002 OProfile authors
      6  * @remark Read the file COPYING
      7  *
      8  * @author John Levon
      9  * @author Philippe Elie
     10  */
     11 
     12 #ifndef GROWABLE_VECTOR_H
     13 #define GROWABLE_VECTOR_H
     14 
     15 #include <vector>
     16 #include <algorithm>
     17 #include <functional>
     18 
     19 /**
     20  * A simple growable vector template.
     21  */
     22 template <typename T> class growable_vector {
     23 public:
     24 	typedef std::vector<T> container_type;
     25 	typedef typename container_type::size_type size_type;
     26 
     27 
     28 	/**
     29 	 * Index into the vector for a value. An out of
     30 	 * bounds index will return a default-constructed value.
     31 	 */
     32 	T operator[](size_type index) const {
     33 		if (index >= container.size())
     34 			return T();
     35 		return container[index];
     36 	}
     37 
     38 
     39 	/**
     40 	 * Index into the vector for a value. If the index is larger than
     41 	 * the current max index, the array is expanded, default-filling
     42 	 * any intermediary gaps.
     43 	 */
     44 	T & operator[](size_type index) {
     45 		if (index >= container.size())
     46 			container.resize(index + 1);
     47 		return container[index];
     48 	}
     49 
     50 
     51 	/**
     52 	 * vectorized += operator
     53 	 */
     54 	growable_vector<T> & operator+=(growable_vector<T> const & rhs) {
     55 		if (rhs.container.size() > container.size())
     56 			container.resize(rhs.container.size());
     57 
     58 		size_type min_size = min(container.size(), rhs.container.size());
     59 		for (size_type i = 0 ; i < min_size; ++i)
     60 			container[i] += rhs.container[i];
     61 
     62 		return *this;
     63 	}
     64 
     65 
     66 	/**
     67 	 * vectorized -= operator, overflow shouldn't occur during substraction
     68 	 * (iow: for each components lhs[i] >= rhs[i]
     69 	 */
     70 	growable_vector<T> & operator-=(growable_vector<T> const & rhs) {
     71 		if (rhs.container.size() > container.size())
     72 			container.resize(rhs.container.size());
     73 
     74 		size_type min_size = min(container.size(), rhs.container.size());
     75 		for (size_type i = 0 ; i < min_size; ++i)
     76 			container[i] -= rhs.container[i];
     77 
     78 		return *this;
     79 	}
     80 
     81 
     82 	/// return current size of vector
     83 	size_type size() const {
     84 		return container.size();
     85 	}
     86 
     87 
     88 	/// fill container with given value
     89 	void fill(size_type size, T const & value) {
     90 		container.resize(size, value);
     91 	}
     92 
     93 
     94 	/// return true if all elements have the default constructed value
     95 	bool zero() const {
     96 		return std::find_if(container.begin(), container.end(),
     97 	                            std::bind2nd(std::not_equal_to<T>(), T()))
     98 					== container.end();
     99 	}
    100 
    101 private:
    102 	container_type container;
    103 };
    104 
    105 #endif // GROWABLE_VECTOR_H
    106