25#ifndef __BITPIT_INDEX_GENERATOR_TPP__
26#define __BITPIT_INDEX_GENERATOR_TPP__
30template<
typename id_t>
31const typename IndexGenerator<id_t>::id_type IndexGenerator<id_t>::NULL_ID = std::numeric_limits<typename IndexGenerator<id_t>::id_type>
::min();
36template<
typename id_t>
38 : m_latest(NULL_ID), m_lowest(NULL_ID), m_highest(NULL_ID)
51template<
typename id_t>
56 if (m_trash.empty()) {
57 if (m_lowest == NULL_ID) {
61 }
else if (m_lowest > 0) {
65 assert(m_highest < std::numeric_limits<id_type>::max());
70 m_latest = *(m_trash.begin());
71 m_trash.erase(m_trash.begin());
82template<
typename id_t>
93template<
typename id_t>
104template<
typename id_t>
108 if (m_latest >= 0 &&
id == m_latest) {
113 if (
id > m_highest) {
123 if (m_trash.count(
id) > 0) {
135template<
typename id_t>
139 if (isAssigned(
id)) {
140 throw std::runtime_error(
"Requested id has already been assigned.");
148 if (m_highest == NULL_ID && m_lowest == NULL_ID) {
151 }
else if (
id > m_highest) {
152 typename std::set<id_type>::iterator trashIterator = m_trash.end();
153 for (id_type wasteId = m_highest + 1; wasteId < id; ++wasteId) {
154 trashIterator = m_trash.insert(trashIterator, wasteId);
158 }
else if (
id < m_lowest) {
159 typename std::set<id_type>::iterator trashIterator = m_trash.end();
160 for (id_type wasteId =
id + 1; wasteId < m_lowest; ++wasteId) {
161 trashIterator = m_trash.insert(trashIterator, wasteId);
166 assert(m_trash.count(
id) > 0);
181template<
typename id_t>
184 assert(
id != NULL_ID);
185 assert(m_trash.count(
id) == 0);
186 assert(
id >= m_lowest &&
id <= m_highest);
190 if (
id == m_highest &&
id == m_lowest) {
194 assert(m_trash.empty());
198 }
else if (
id == m_highest) {
200 auto trashIterator = m_trash.end();
201 while (!m_trash.empty()) {
203 if (*trashIterator != m_highest) {
207 trashIterator = m_trash.erase(trashIterator);
208 if (m_highest != 0) {
211 assert(m_trash.empty());
216 }
else if (
id == m_lowest) {
218 auto trashIterator = m_trash.begin();
219 while (!m_trash.empty()) {
220 if (*trashIterator != m_lowest) {
224 trashIterator = m_trash.erase(trashIterator);
233 if (
id == m_latest) {
241template<
typename id_t>
256template<
typename id_t>
268template<
typename id_t>
269int IndexGenerator<id_t>::getBinaryArchiveVersion()
const
279template<
typename id_t>
288 for (id_type
id : m_trash) {
298template<
typename id_t>
304 if (version != getBinaryArchiveVersion()) {
305 throw std::runtime_error (
"The version of the file does not match the required version");
316 std::set<id_type>().swap(m_trash);
318 for (
size_t i = 0; i < nTrashedIds; ++i) {
The IndexGenerator class allows to generate unique ids.
bool isAssigned(id_type id)
void setAssigned(id_type id)
void restore(std::istream &stream)
void dump(std::ostream &stream) const
std::array< T, d > min(const std::array< T, d > &x, const std::array< T, d > &y)
void write(std::ostream &stream, const std::vector< bool > &container)
void read(std::istream &stream, std::vector< bool > &container)