1 #ifndef __COMPACT_LOOKUP_H__
2 #define __COMPACT_LOOKUP_H__
31 #include "CiftiAssert.h"
44 std::vector<T> elements;
46 std::vector<Chunk> m_chunks;
53 iterator(
CompactLookup<T>& container, std::size_t chunk, int64_t elem) : m_container(container), m_chunk(chunk), m_elem(elem) { }
55 bool operator==(
const iterator& rhs)
const
57 if (&m_container != &(rhs.m_container))
return false;
58 if (m_chunk != rhs.m_chunk)
return false;
59 if (m_elem != rhs.m_elem)
return false;
64 CiftiAssert(m_chunk < m_container.m_chunks.size());
65 CiftiAssert(m_elem >= 0 && m_elem < (int64_t)m_container.m_chunks[m_chunk].elements.size());
66 return m_container.m_chunks[m_chunk].elements[m_elem];
70 CiftiAssert(m_chunk < m_container.m_chunks.size());
71 CiftiAssert(m_elem >= 0 && m_elem < (int64_t)m_container.m_chunks[m_chunk].elements.size());
72 return &(m_container.m_chunks[m_chunk].elements[m_elem]);
85 if (&m_container != &(rhs.m_container))
return false;
86 if (m_chunk != rhs.m_chunk)
return false;
87 if (m_elem != rhs.m_elem)
return false;
92 CiftiAssert(m_chunk < m_container.m_chunks.size());
93 CiftiAssert(m_elem >= 0 && m_elem < (int64_t)m_container.m_chunks[m_chunk].elements.size());
94 return m_container.m_chunks[m_chunk].elements[m_elem];
98 CiftiAssert(m_chunk < m_container.m_chunks.size());
99 CiftiAssert(m_elem >= 0 && m_elem < (int64_t)m_container.m_chunks[m_chunk].elements.size());
100 return &(m_container.m_chunks[m_chunk].elements[m_elem]);
107 iterator
find(
const int64_t& index);
109 const_iterator
find(
const int64_t& index)
const;
111 const_iterator end()
const;
116 template <
typename T>
119 std::size_t numChunks = m_chunks.size();
120 std::size_t low = 0, high = numChunks, guess;
121 bool attach_low =
false, attach_high =
false;
124 guess = (low + high - 1) / 2;
125 CiftiAssert(guess < m_chunks.size());
126 if (m_chunks[guess].start > index)
133 if (high > 0 && m_chunks[high - 1].start + (int64_t)(m_chunks[high - 1].elements.size()) > index)
135 CiftiAssertVectorIndex(m_chunks[high -1].elements, index - m_chunks[high - 1].start);
136 return m_chunks[high - 1].elements[index - m_chunks[high - 1].start];
138 if (high > 0 && m_chunks[high - 1].start + (int64_t)(m_chunks[high - 1].elements.size()) == index) attach_low =
true;
139 if (high < numChunks && m_chunks[high].start == index + 1) attach_high =
true;
142 std::vector<T>& lowvec = m_chunks[high - 1].elements;
143 std::size_t retIndex = lowvec.size();
144 lowvec.push_back(T());
147 std::vector<T>& highvec = m_chunks[high].elements;
148 lowvec.insert(lowvec.end(), highvec.begin(), highvec.end());
149 m_chunks.erase(m_chunks.begin() + high);
151 return lowvec[retIndex];
155 std::vector<T>& highvec = m_chunks[high].elements;
156 highvec.insert(highvec.begin(), T());
157 m_chunks[high].start = index;
160 m_chunks.insert(m_chunks.begin() + high, Chunk());
161 m_chunks[high].start = index;
162 m_chunks[high].elements.push_back(T());
163 return m_chunks[high].elements[0];
168 template <
typename T>
171 std::size_t numChunks = m_chunks.size();
172 std::size_t low = 0, high = numChunks, guess;
175 guess = (low + high - 1) / 2;
176 CiftiAssert(guess < m_chunks.size());
177 if (m_chunks[guess].start > index)
184 if (high > 0 && m_chunks[high - 1].start + (int64_t)(m_chunks[high - 1].elements.size()) > index)
186 std::size_t outIndex = index - m_chunks[high - 1].start;
187 CiftiAssert(outIndex < m_chunks[high - 1].elements.size());
188 return iterator(*
this, high - 1, outIndex);
193 template <
typename T>
196 std::size_t numChunks = m_chunks.size();
197 std::size_t low = 0, high = numChunks, guess;
200 guess = (low + high - 1) / 2;
201 CiftiAssert(guess < m_chunks.size());
202 if (m_chunks[guess].start > index)
209 if (high > 0 && m_chunks[high - 1].start + (int64_t)(m_chunks[high - 1].elements.size()) > index)
211 std::size_t outIndex = index - m_chunks[high - 1].start;
212 CiftiAssert(outIndex < m_chunks[high - 1].elements.size());
218 template <
typename T>
221 return iterator(*
this, 0, -1);
224 template <
typename T>
225 typename CompactLookup<T>::const_iterator CompactLookup<T>::end()
const
227 return const_iterator(*
this, 0, -1);
230 template <
typename T>
Definition: CompactLookup.h:77
Definition: CompactLookup.h:49
Definition: CompactLookup.h:40
const_iterator find(const int64_t &index) const
returns a const_iterator pointing to the desired element, or one equal to end() if no such element is...
Definition: CompactLookup.h:194
void clear()
empties the lookup
Definition: CompactLookup.h:231
T & operator[](const int64_t &index)
creates the element if it didn't exist, and returns a reference to it
Definition: CompactLookup.h:117
iterator find(const int64_t &index)
returns an iterator pointing to the desired element, or one equal to end() if no such element is foun...
Definition: CompactLookup.h:169
namespace for all CiftiLib functionality
Definition: CiftiBrainModelsMap.h:42