21 #ifndef MOAB_RANGE_MAP_HPP
22 #define MOAB_RANGE_MAP_HPP
37 template <
typename KeyType,
typename ValType, ValType NullVal = 0 >
54 typedef typename RangeList::const_iterator
iterator;
79 inline std::pair< iterator, bool >
insert( KeyType first_key, ValType first_val, KeyType count );
92 inline ValType
find( KeyType key )
const;
95 inline bool find( KeyType key, ValType& val_out )
const;
98 inline bool exists( KeyType key )
const;
101 inline bool intersects( KeyType start, KeyType count )
const;
121 Range b = { key, 1, NullVal };
122 return std::lower_bound(
begin(),
end(), b );
126 Range b = { key, 1, NullVal };
127 return std::lower_bound( s, e, b );
131 Range b = { key, 1, NullVal };
132 return std::upper_bound(
begin(),
end(), b );
136 Range b = { key, 1, NullVal };
137 return std::upper_bound( s, e, b );
139 inline std::pair< iterator, iterator >
equal_range( KeyType key )
const
141 Range b = { key, 1, NullVal };
142 return std::equal_range(
begin(),
end(), b );
154 template <
typename KeyType,
typename ValType, ValType NullVal >
155 inline std::pair< typename RangeMap< KeyType, ValType, NullVal >::iterator,
bool >
RangeMap<
158 NullVal >::insert( KeyType first_key, ValType first_val, KeyType count )
160 Range block = { first_key, count, first_val };
161 typename RangeList::iterator i = std::lower_bound( data.begin(), data.end(), block );
163 if( i == data.end() )
165 if( i != data.begin() )
168 if( i->begin + i->count == first_key && i->value + i->count == first_val )
171 return std::pair< iterator, bool >( i,
true );
174 data.push_back( block );
175 return std::pair< iterator, bool >( data.end() - 1,
true );
178 if( i->begin < first_key + count )
return std::pair< iterator, bool >( i,
false );
180 if( i->begin == first_key + count && i->value == first_val + count )
182 i->begin = first_key;
183 i->value = first_val;
185 if( i != data.begin() )
189 if( i->begin + i->count == first_key && i->value + i->count == first_val )
197 return std::pair< iterator, bool >( i,
true );
200 if( i != data.begin() )
203 if( i->begin + i->count == first_key && i->value + i->count == first_val )
206 return std::pair< iterator, bool >( i,
true );
211 return std::pair< iterator, bool >( data.insert( i, block ),
true );
214 template <
typename KeyType,
typename ValType, ValType NullVal >
219 new_data.reserve( other.
data.size() + data.size() );
222 typename RangeList::const_iterator i = other.
data.begin();
223 typename RangeList::const_iterator j = data.begin();
224 typename RangeList::const_iterator k;
225 while( i != other.
data.end() || j != data.end() )
227 if( j != data.end() && ( i == other.
data.end() || j->begin < i->begin ) )
232 else if( i != other.
data.end() )
239 if( new_data.empty() )
240 new_data.push_back( *k );
241 else if( new_data.back().begin + new_data.back().count > k->begin )
243 else if( new_data.back().begin + new_data.back().count == k->begin &&
244 new_data.back().value + new_data.back().count == k->value )
245 new_data.back().count += k->count;
247 new_data.push_back( *k );
250 data.swap( new_data );
254 template <
typename KeyType,
typename ValType, ValType NullVal >
257 Range search = { key, 1, NullVal };
258 typename RangeList::const_iterator i = std::lower_bound( data.begin(), data.end(), search );
259 if( i == data.end() || i->begin > key )
return NullVal;
261 return i->value + key - i->begin;
264 template <
typename KeyType,
typename ValType, ValType NullVal >
267 Range search = { key, 1, NullVal };
268 typename RangeList::const_iterator i = std::lower_bound( data.begin(), data.end(), search );
269 if( i == data.end() || i->begin > key )
275 val = i->value + key - i->begin;
279 template <
typename KeyType,
typename ValType, ValType NullVal >
282 Range search = { key, 1, NullVal };
283 typename RangeList::const_iterator i = std::lower_bound( data.begin(), data.end(), search );
284 return i != data.end() && key >= i->begin;
287 template <
typename KeyType,
typename ValType, ValType NullVal >
290 Range search = { start, count, NullVal };
291 typename RangeList::const_iterator i = std::lower_bound( data.begin(), data.end(), search );
292 return i != data.end() && start + count > i->begin && i->begin + i->count > start;
295 template <
typename KeyType,
typename ValType, ValType NullVal >
300 Range search = { key, 1, NullVal };
301 typename RangeList::iterator i, j;
302 i = std::lower_bound( data.begin(), data.end(), search );
304 if( i == data.end() )
return i;
308 KeyType offset = key - i->begin;
310 if( ( offset + count ) < i->count )
317 return data.insert( i, ins ) + 1;
325 for( j = i; j != data.end() && ( j->begin + j->count ) <= ( key + count ); ++j )
327 i = data.erase( i, j );
330 if( i != data.end() && ( key + count ) >= i->begin )
332 KeyType offset = key + count - i->begin;