5 //---------------------------------------------------------------------------
7 template < typename T, typename TCount >
10 operator[](std::size_t i) const
12 assert(i <= this->size());
13 std::size_t current_i = 0;
14 typename Data::const_iterator iData = m_data.begin();
15 while (i >= iData->length())
20 return iData->value();
23 //---------------------------------------------------------------------------
25 template < typename T, typename TCount >
26 typename rle_array<T,TCount>::ValueProxy
28 operator[](std::size_t i)
30 assert(i <= this->size());
31 typename Data::iterator iData = m_data.begin();
32 while (i >= iData->length())
37 return ValueProxy(std::make_pair(iData, i), *this);
40 //---------------------------------------------------------------------------
43 template < typename T, typename TCount >
48 std::pair<typename Data::iterator, TCount> & index
52 // Do nothing if nothing to do
53 if (value == index.first->value()) return;
55 // Is the current segment made of only one element?
56 if (index.first->length() == 1)
58 assert(index.second == 0);
60 typename Data::iterator iPreviousSegment = index.first;
61 bool atBeginning = (index.first == m_data.begin());
62 if (!atBeginning) --iPreviousSegment;
63 typename Data::iterator iNextSegment = index.first;
64 bool atEnd = (++iNextSegment == m_data.end());
66 if (!atBeginning && iPreviousSegment->value() == value)
68 index.second = (iPreviousSegment->length())++;
69 if (!atEnd && iNextSegment->value() == value)
71 iPreviousSegment->length() += iNextSegment->length();
72 m_data.erase(iNextSegment);
74 m_data.erase(index.first);
75 index.first = iPreviousSegment;
77 else if (!atEnd && iNextSegment->value() == value)
79 ++(iNextSegment->length());
80 index.first = m_data.erase(index.first);
82 // Then we remain a single point : simply update the value.
85 // Simply overwrite the value
86 index.first->value() = value;
87 //index.first->setValue(value);
90 // Are we at the very first of current repeated value?
91 else if (index.second == 0)
93 typename Data::iterator iPreviousSegment = index.first;
94 bool atBeginning = (index.first == m_data.begin());
95 if (!atBeginning) --iPreviousSegment;
97 // If so, decrease current length number by one
98 --(index.first->length());
100 // Is the previous value the same?
101 if (!atBeginning && iPreviousSegment->value() == value)
103 // Then just increase its length number
104 index.second = (iPreviousSegment->length())++;
105 index.first = iPreviousSegment;
109 // else insert a new length value
110 index.first = m_data.insert(index.first, Segment(1, value));
114 // Are we exactly at the end of current segment?
115 else if (index.second == index.first->length()-1)
117 typename Data::iterator iNextSegment = index.first;
118 bool atEnd = (++iNextSegment == m_data.end());
120 // Then, decrease current length number by one
121 --(index.first->length());
124 if (!atEnd && iNextSegment->value() == value)
126 // If the next sequence has the same value, just increase
127 // its length number by one
128 ++(iNextSegment->length());
129 index.first = iNextSegment;
133 // Insert a new length value
134 index.first = m_data.insert(iNextSegment, Segment(1, value));
137 // we are in the middle of a repeated value
140 assert(index.second > 0);
142 m_data.insert(index.first, Segment(index.second, index.first->value()));
144 m_data.insert(index.first, Segment(1, value));
146 index.first->length() -= index.second+1;
153 template < typename T, typename TCount >
154 class rle_array<T,TCount>::ValueProxy : public value_proxy<rle_array<T,TCount>, std::pair<typename Data::iterator, TCount> >
157 typedef value_proxy<rle_array<T,TCount>, std::pair<typename Data::iterator, TCount> > Base;
158 typedef typename Base::index_type index_type;
159 typedef typename Base::container container;
160 public: // constructors & destructor
161 ValueProxy(index_type i, container * p) : Base(i, p) {}
163 // unfortunately we have to redefine this function by hand :(
164 void operator=(typename boost::call_traits<T>::param_type value) { this->Base::operator=(value); }
168 //---------------------------------------------------------------------------