Assembla home | Assembla project page
 

root/trunk/cbear.berlios.de/range/fill.hpp

Revision 2, 2.4 kB (checked in by sergey_shandar, 1 year ago)

restoring...

Line 
1 #ifndef CBEAR_BERLIOS_DE_RANGE_FILL_HPP_INCLUDED
2 #define CBEAR_BERLIOS_DE_RANGE_FILL_HPP_INCLUDED
3
4 #include <algorithm>
5
6 #include <cbear.berlios.de/range/begin.hpp>
7 #include <cbear.berlios.de/range/end.hpp>
8 #include <cbear.berlios.de/range/helper.hpp>
9
10 namespace cbear_berlios_de
11 {
12 namespace range
13 {
14
15 template<class Container, class T>
16 void fill(Container &X, T const &Value)
17 {
18         ::std::fill(range::begin(X), range::end(X), Value);
19 }
20
21 template<class Container, class T>
22 void fill(Container const &X, T const &Value)
23 {
24         ::std::fill(range::begin(X), range::end(X), Value);
25 }
26
27 template<class T>
28 class fill_iterator_t
29 {
30 public:
31         typedef ::std::random_access_iterator_tag iterator_category;
32         typedef T value_type;
33         typedef ::std::ptrdiff_t difference_type;
34         typedef T const *pointer;
35         typedef T const &reference;
36         fill_iterator_t(T const &V, difference_type I):
37                 V(V), I(I)
38         {
39         }
40         T const &operator*() const
41         {
42                 return this->V;
43         }
44         bool operator==(fill_iterator_t const &B) const
45         {
46                 return this->I == B.I;
47         }
48         bool operator!=(fill_iterator_t const &B) const
49         {
50                 return this->I != B.I;
51         }
52         fill_iterator_t &operator++()
53         {
54                 ++this->I;
55                 return *this;
56         }
57         bool operator<(fill_iterator_t const &B) const
58         {
59                 return this->I < B.I;
60         }
61         difference_type operator-(fill_iterator_t const &B) const
62         {
63                 return this->I - B.I;
64         }
65         fill_iterator_t &operator+=(difference_type d)
66         {
67                 this->I += d;
68                 return *this;
69         }
70         fill_iterator_t operator+(difference_type d) const
71         {
72                 fill_iterator_t t(*this);
73                 t += d;
74                 return t;
75         }
76 private:
77         T const &V;
78         difference_type I;
79         fill_iterator_t &operator=(fill_iterator_t const &);
80 };
81
82 namespace detail
83 {
84
85 template<class T>
86 class fill_base_t
87 {
88 public:
89         typedef fill_iterator_t<T> iterator;
90         typedef iterator const_iterator;
91 };
92
93 }
94
95 template<class T>
96 class fill_t:
97         public helper_t<fill_t<T>, detail::fill_base_t<T> >
98 {
99 public:
100
101         typedef fill_iterator_t<T> const_iterator;
102         typedef typename const_iterator::difference_type difference_type;
103
104         fill_t(T const &V, difference_type Size):
105                 End(V, Size)
106         {
107         }
108
109         const_iterator begin() const
110         {
111                 return const_iterator(*this->End, 0);
112         }
113
114         const_iterator end() const
115         {
116                 return this->End;
117         }
118
119 private:
120         const_iterator End;
121         fill_t &operator=(fill_t const &);
122 };
123
124 template<class T>
125 fill_t<T> make_fill(T const &V, std::size_t Size)
126 {
127         return fill_t<T>(V, Size);
128 }
129
130 }
131 }
132
133 #endif
134
Note: See TracBrowser for help on using the browser.