Line data Source code
1 : /*
2 : * Copyright (c) 2009-2015: G-CSC, Goethe University Frankfurt
3 : * Author: Sebastian Reiter
4 : *
5 : * This file is part of UG4.
6 : *
7 : * UG4 is free software: you can redistribute it and/or modify it under the
8 : * terms of the GNU Lesser General Public License version 3 (as published by the
9 : * Free Software Foundation) with the following additional attribution
10 : * requirements (according to LGPL/GPL v3 §7):
11 : *
12 : * (1) The following notice must be displayed in the Appropriate Legal Notices
13 : * of covered and combined works: "Based on UG4 (www.ug4.org/license)".
14 : *
15 : * (2) The following notice must be displayed at a prominent place in the
16 : * terminal output of covered works: "Based on UG4 (www.ug4.org/license)".
17 : *
18 : * (3) The following bibliography is recommended for citation and must be
19 : * preserved in all covered files:
20 : * "Reiter, S., Vogel, A., Heppner, I., Rupp, M., and Wittum, G. A massively
21 : * parallel geometric multigrid solver on hierarchically distributed grids.
22 : * Computing and visualization in science 16, 4 (2013), 151-164"
23 : * "Vogel, A., Reiter, S., Rupp, M., Nägel, A., and Wittum, G. UG4 -- a novel
24 : * flexible software system for simulating pde based models on high performance
25 : * computers. Computing and visualization in science 16, 4 (2013), 165-179"
26 : *
27 : * This program is distributed in the hope that it will be useful,
28 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 : * GNU Lesser General Public License for more details.
31 : */
32 :
33 : #ifndef __UTIL__SECTION_CONTAINER__
34 : #define __UTIL__SECTION_CONTAINER__
35 :
36 : #include <vector>
37 : #include "../types.h"
38 :
39 : namespace ug
40 : {
41 :
42 : /// \addtogroup ugbase_common_util
43 : /// \{
44 :
45 : ////////////////////////////////////////////////////////////////////////
46 : // SectionContainer
47 : /// A container that is divided into different sections.
48 : /**
49 : * This container can be used to store values that have a common base,
50 : * but can be sorted in different sections. The SectionContainer supplies
51 : * you with interfaces to iterate over a certain section or to iterate
52 : * over the complete container.
53 : * The SectionContainer works on containers of the standard template library.
54 : * Be sure, that TContainer holds values of type TValue.
55 : * TContainer has to support the container operations of a std::list.
56 : */
57 : template <class TValue, class TContainer>
58 4 : class SectionContainer
59 : {
60 : public:
61 : typedef TValue value_type;
62 : typedef TContainer Container;
63 : typedef typename Container::iterator iterator;
64 : typedef typename Container::const_iterator const_iterator;
65 : //typedef typename Container::reverse_iterator reverse_iterator;
66 : //typedef typename Container::const_reverse_iterator const_reverse_iterator;
67 :
68 : public:
69 : SectionContainer();
70 :
71 : void clear();
72 : void clear_section(int sectionIndex);
73 :
74 : iterator insert(const TValue& val, int sectionIndex);
75 : void erase(const iterator& iter, int sectionIndex);
76 :
77 : inline iterator begin() {return m_container.begin();}
78 : inline iterator end() {return m_container.end();}
79 :
80 : inline const_iterator begin() const {return m_container.begin();}
81 : inline const_iterator end() const {return m_container.end();}
82 :
83 : /**if the section is empty section_begin and section_end return the same iterators.
84 : * However, no assumptions on the positions of these iterators should be made.*/
85 : iterator section_begin(int sectionIndex);
86 :
87 : const_iterator section_begin(int sectionIndex) const;
88 :
89 : /**if the section is empty section_begin and section_end return the same iterators.
90 : However, no assumptions on the positions of these iterators should be made.*/
91 : iterator section_end(int sectionIndex);
92 :
93 : const_iterator section_end(int sectionIndex) const;
94 :
95 : /// returns the first entry in the given section.
96 : /** use index -1 to get the first entry of the complete chain.
97 : * Make sure to only call this method if there are elements in the
98 : * given section at all.*/
99 : value_type& front(int secIndex = -1);
100 :
101 : /// returns the last entry in the given section.
102 : /** use index -1 to get the last entry of the complete chain.
103 : * Make sure to only call this method if there are elements in the
104 : * given section at all.*/
105 : value_type& back(int secIndex = -1);
106 :
107 : uint num_elements(int sectionIndex) const;
108 15 : inline uint num_elements() const {return m_numElements;}
109 10 : inline int num_sections() const {return (int)m_vSections.size();}
110 :
111 : /// returns the container for raw access.
112 : /** Use this method with extreme care. Changes to the elements
113 : * and to the layout of the container may most likely result in a
114 : * corruption of the SectionContainer.
115 : */
116 8 : inline Container& get_container() {return m_container;}
117 :
118 : /// appends the elements of the given container to the current one
119 : /** Note that the append operation is performed for each section separately.
120 : * \warning This method should not be used if the underlying container and
121 : * the given one operate on the same AttachedElemList. Otherwise
122 : * severe side effect will occur! Use transfer_elements instead!*/
123 : void append(const SectionContainer& c);
124 :
125 : /// takes all elements from the given section container and transfers them to this one.
126 : void transfer_elements(SectionContainer& c);
127 :
128 : protected:
129 : void add_sections(int num);
130 :
131 : protected:
132 3 : struct Section
133 : {
134 : Section() {}
135 3 : Section(const iterator& elemsBegin, const iterator& elemsEnd,
136 : //const reverse_iterator& elemsRBegin,
137 : //const reverse_iterator& elemsREnd,
138 : int numElems) :
139 : m_elemsBegin(elemsBegin), m_elemsEnd(elemsEnd),
140 : //m_elemsRBegin(elemsRBegin), m_elemsREnd(elemsREnd),
141 3 : m_numElements(numElems)
142 : {}
143 :
144 : iterator m_elemsBegin;
145 : iterator m_elemsEnd;
146 : //reverse_iterator m_elemsRBegin;
147 : //reverse_iterator m_elemsREnd;
148 : uint m_numElements;
149 : };
150 :
151 : typedef std::vector<Section> SectionVec;
152 :
153 : protected:
154 : Container m_container;
155 : SectionVec m_vSections;
156 : uint m_numElements;
157 : };
158 :
159 : // end group ugbase_common_util
160 : /// \}
161 :
162 : }// end of namespace
163 :
164 : // include implementation
165 : #include "section_container.hpp"
166 :
167 : #endif
|