33 #ifndef __UTIL__SECTION_CONTAINER__IMPL__
34 #define __UTIL__SECTION_CONTAINER__IMPL__
54 template <
class TValue,
class TContainer>
60 template <
class TValue,
class TContainer>
68 for(
typename SectionVec::iterator iter = m_vSections.begin();
69 iter != m_vSections.end(); ++iter)
72 sec.m_elemsBegin = sec.m_elemsEnd = m_container.end();
74 sec.m_numElements = 0;
78 template <
class TValue,
class TContainer>
83 assert((sectionIndex >= 0) && (sectionIndex < num_sections()) &&
84 "ERROR in SectionContainer::clear_section(): bad sectionIndex");
86 iterator iterStart = section_begin(sectionIndex);
88 if(iterStart != iterEnd)
90 m_container.erase(iterStart, iterEnd);
91 m_vSections[sectionIndex].m_elemsBegin = m_vSections[sectionIndex].m_elemsEnd;
93 m_numElements -= m_vSections[sectionIndex].m_numElements;
94 m_vSections[sectionIndex].m_numElements = 0;
97 for(
int i = sectionIndex - 1; i >= 0; --i){
98 if(m_vSections[i].m_numElements > 0){
99 m_vSections[i].m_elemsEnd = m_vSections[sectionIndex].m_elemsBegin;
114 template <
class TValue,
class TContainer>
120 return m_container.begin();
121 else if(sectionIndex >= num_sections())
122 return m_container.end();
124 return m_vSections[sectionIndex].m_elemsBegin;
127 template <
class TValue,
class TContainer>
133 return m_container.begin();
134 else if(sectionIndex >= num_sections())
135 return m_container.end();
137 return m_vSections[sectionIndex].m_elemsBegin;
140 template <
class TValue,
class TContainer>
145 if(sectionIndex >= num_sections() || sectionIndex < 0)
146 return m_container.end();
148 return m_vSections[sectionIndex].m_elemsEnd;
151 template <
class TValue,
class TContainer>
156 if(sectionIndex >= num_sections() || sectionIndex < 0)
157 return m_container.end();
159 return m_vSections[sectionIndex].m_elemsEnd;
162 template <
class TValue,
class TContainer>
167 assert((secIndex >= -1) && (secIndex < num_sections()) &&
"Bad section index");
169 return m_container.front();
170 return *m_vSections[secIndex].m_elemsBegin;
173 template <
class TValue,
class TContainer>
178 assert((secIndex >= -1) && (secIndex < num_sections()) &&
"Bad section index");
180 return m_container.back();
185 if(m_vSections[secIndex].m_elemsEnd == m_container.end())
186 return m_container.back();
192 iterator titer(m_vSections[secIndex].m_elemsEnd);
197 template <
class TValue,
class TContainer>
202 for(
int i = 0; i < num; ++i)
203 m_vSections.push_back(
Section(m_container.end(), m_container.end(),
208 template <
class TValue,
class TContainer>
213 assert((sectionIndex >= 0) &&
214 "ERROR in SectionContainer::num_elements(): bad sectionIndex");
216 if(sectionIndex >= num_sections())
218 return m_vSections[sectionIndex].m_numElements;
221 template <
class TValue,
class TContainer>
224 insert(
const TValue& val,
int sectionIndex)
226 assert((sectionIndex >= 0) &&
227 "ERROR in SectionContainer::insert(): bad sectionIndex");
232 if(sectionIndex >= num_sections())
233 add_sections(sectionIndex - num_sections() + 1);
236 if(num_elements(sectionIndex) == 0)
240 int nextValidSection = -1;
241 for(
int i = sectionIndex + 1; i < num_sections(); ++i){
242 if(num_elements(i) > 0){
243 nextValidSection = i;
249 int prevValidSection = -1;
250 for(
int i = sectionIndex - 1; i >= 0; --i){
251 if(num_elements(i) > 0){
252 prevValidSection = i;
258 if(nextValidSection != -1){
259 m_vSections[sectionIndex].m_elemsEnd = m_vSections[nextValidSection].m_elemsBegin;
262 m_vSections[sectionIndex].m_elemsEnd = m_container.end();
265 nHandle = m_vSections[sectionIndex].m_elemsBegin = m_container.insert(m_vSections[sectionIndex].m_elemsEnd, val);
266 m_vSections[sectionIndex].m_numElements++;
274 if(prevValidSection != -1){
275 m_vSections[prevValidSection].m_elemsEnd = m_vSections[sectionIndex].m_elemsBegin;
284 nHandle = m_container.insert(m_vSections[sectionIndex].m_elemsEnd, val);
285 m_vSections[sectionIndex].m_numElements++;
293 template <
class TValue,
class TContainer>
298 assert((sectionIndex >= 0) && (sectionIndex < num_sections()) &&
299 "ERROR in SectionContainer::erase(): bad sectionIndex");
305 if(elemHandle == m_vSections[sectionIndex].m_elemsBegin)
308 hNext = m_container.erase(elemHandle);
309 m_vSections[sectionIndex].m_numElements--;
312 m_vSections[sectionIndex].m_elemsBegin = hNext;
316 for (
int i = sectionIndex - 1; i >= 0; --i)
318 if (num_elements(i) <= 0)
320 m_vSections[i].m_elemsBegin = m_vSections[i].m_elemsEnd = hNext;
323 m_vSections[i].m_elemsEnd = hNext;
330 hNext = m_container.erase(elemHandle);
331 m_vSections[sectionIndex].m_numElements--;
340 if(hNext == m_container.end())
341 m_vSections[sectionIndex].m_elemsEnd = m_container.end();
344 template <
class TValue,
class TContainer>
351 const TValue& val = *iter;
358 template <
class TValue,
class TContainer>
365 const TValue& val = *iter;
A special iterator which allows to iterate over elements in a AttachedElementList.
Definition: attached_list.h:52
A container that is divided into different sections.
Definition: section_container.h:59
Container::const_iterator const_iterator
Definition: section_container.h:64
void erase(const iterator &iter, int sectionIndex)
Definition: section_container.hpp:296
iterator section_begin(int sectionIndex)
Definition: section_container.hpp:117
value_type & front(int secIndex=-1)
returns the first entry in the given section.
Definition: section_container.hpp:165
int num_sections() const
Definition: section_container.h:109
void add_sections(int num)
Definition: section_container.hpp:200
void clear_section(int sectionIndex)
Definition: section_container.hpp:81
uint num_elements() const
Definition: section_container.h:108
iterator section_end(int sectionIndex)
Definition: section_container.hpp:143
TValue value_type
Definition: section_container.h:61
value_type & back(int secIndex=-1)
returns the last entry in the given section.
Definition: section_container.hpp:176
void transfer_elements(SectionContainer &c)
takes all elements from the given section container and transfers them to this one.
Definition: section_container.hpp:361
Container::iterator iterator
Definition: section_container.h:63
void append(const SectionContainer &c)
appends the elements of the given container to the current one
Definition: section_container.hpp:347
void clear()
Definition: section_container.hpp:63
iterator insert(const TValue &val, int sectionIndex)
Definition: section_container.hpp:224
SectionContainer()
Definition: section_container.hpp:56
unsigned int uint
Definition: types.h:114
Definition: section_container.h:133