23 #ifndef __CLRX_CONTAINERS_H__ 24 #define __CLRX_CONTAINERS_H__ 32 #include <unordered_map> 33 #include <initializer_list> 51 Array(): ptr(nullptr), ptrEnd(nullptr)
88 ptr = ptrEnd =
nullptr;
89 const size_t N = cp.
size();
93 std::copy(cp.ptr, cp.ptrEnd, ptr);
107 cp.ptr = cp.ptrEnd =
nullptr;
111 Array(std::initializer_list<T> list)
116 const size_t N = list.size();
120 std::copy(list.begin(), list.end(), ptr);
149 cp.ptr = cp.ptrEnd =
nullptr;
156 assign(list.begin(), list.end());
169 {
return ptrEnd==ptr; }
173 {
return ptrEnd-ptr; }
198 const size_t toMove = std::min(N,
size());
199 for (
size_t k = 0; k < toMove; k++)
200 newPtr[k] = std::move_if_noexcept(ptr[k]);
216 ptr = ptrEnd =
nullptr;
220 template<
typename It>
223 const size_t N = e-b;
230 { std::copy(b, e, newPtr); }
241 std::copy(b, e, ptr);
274 {
return ptrEnd[-1]; }
277 {
return ptrEnd[-1]; }
282 std::swap(ptr, array.ptr);
283 std::swap(ptrEnd, array.ptrEnd);
305 template<
typename It>
316 auto fit = std::find(std::vector<T>::begin(), std::vector<T>::end(), v);
317 if (fit == std::vector<T>::end())
318 std::vector<T>::push_back(v);
324 auto fit = std::find(std::vector<T>::begin(), std::vector<T>::end(), v);
325 if (fit != std::vector<T>::end())
326 std::vector<T>::erase(fit);
332 return std::find(std::vector<T>::begin(), std::vector<T>::end(), v) !=
333 std::vector<T>::end();
345 template<
typename Iter>
347 typename std::iterator_traits<Iter>::value_type& v);
357 template<
typename Iter,
typename Comp =
358 std::less<typename std::iterator_traits<Iter>::value_type> >
360 typename std::iterator_traits<Iter>::value_type& v, Comp comp);
369 template<
typename Iter>
371 typename std::iterator_traits<Iter>::value_type::first_type& k);
381 template<
typename Iter,
typename Comp =
382 std::less<typename std::iterator_traits<Iter>::value_type::first_type> >
384 typename std::iterator_traits<Iter>::value_type::first_type& k, Comp comp);
391 template<
typename Iter>
392 void mapSort(Iter begin, Iter end);
399 template<
typename Iter,
typename Comp =
400 std::less<typename std::iterator_traits<Iter>::value_type::first_type> >
401 void mapSort(Iter begin, Iter end, Comp comp);
410 template<
typename Iter>
412 const typename std::iterator_traits<Iter>::value_type& v)
414 auto it = std::lower_bound(begin, end, v);
415 if (it == end || v < *it)
428 template<
typename Iter,
typename Comp>
430 const typename std::iterator_traits<Iter>::value_type& v, Comp comp)
432 auto it = std::lower_bound(begin, end, v, comp);
433 if (it == end || comp(v, *it))
439 template<
typename Iter>
441 typename std::iterator_traits<Iter>::value_type::first_type& k)
443 typedef typename std::iterator_traits<Iter>::value_type::first_type K;
444 typedef typename std::iterator_traits<Iter>::value_type::second_type V;
445 auto it = std::lower_bound(begin, end, std::make_pair(k, V()),
446 [](
const std::pair<K,V>& e1,
const std::pair<K,V>& e2) {
447 return e1.first < e2.first; });
448 if (it == end || k < it->first)
454 template<
typename Iter,
typename Comp>
456 typename std::iterator_traits<Iter>::value_type::first_type& k, Comp comp)
458 typedef typename std::iterator_traits<Iter>::value_type::first_type K;
459 typedef typename std::iterator_traits<Iter>::value_type::second_type V;
460 auto it = std::lower_bound(begin, end, std::make_pair(k, V()),
461 [&comp](
const std::pair<K,V>& e1,
const std::pair<K,V>& e2) {
462 return comp(e1.first, e2.first); });
463 if (it == end || comp(k, it->first))
473 template<
typename Iter>
476 typedef typename std::iterator_traits<Iter>::value_type::first_type K;
477 typedef typename std::iterator_traits<Iter>::value_type::second_type V;
478 std::sort(begin, end, [](
const std::pair<K,V>& e1,
const std::pair<K,V>& e2) {
479 return e1.first < e2.first; });
488 template<
typename Iter,
typename Comp>
491 typedef typename std::iterator_traits<Iter>::value_type::first_type K;
492 typedef typename std::iterator_traits<Iter>::value_type::second_type V;
493 std::sort(begin, end, [&comp](
const std::pair<K,V>& e1,
const std::pair<K,V>& e2) {
494 return comp(e1.first, e2.first); });
499 template<
typename K,
typename V>
514 typedef typename std::unordered_map<K, Entry>::iterator EntryMapIt;
516 std::vector<EntryMapIt> sortedEntries;
517 std::unordered_map<K, Entry> entryMap;
519 void updateInSortedEntries(EntryMapIt it)
521 const size_t curPos = it->second.sortedPos;
524 if (sortedEntries[curPos-1]->second.usage < it->second.usage &&
525 (curPos==1 || sortedEntries[curPos-2]->second.usage >= it->second.usage))
528 std::swap(sortedEntries[curPos-1]->second.sortedPos, it->second.sortedPos);
529 std::swap(sortedEntries[curPos-1], sortedEntries[curPos]);
533 auto fit = std::upper_bound(sortedEntries.begin(),
534 sortedEntries.begin()+it->second.sortedPos, it,
535 [](EntryMapIt it1, EntryMapIt it2)
536 {
return it1->second.usage > it2->second.usage; });
537 if (fit != sortedEntries.begin()+it->second.sortedPos)
539 const size_t curPos = it->second.sortedPos;
540 std::swap((*fit)->second.sortedPos, it->second.sortedPos);
541 std::swap(*fit, sortedEntries[curPos]);
545 void insertToSortedEntries(EntryMapIt it)
547 it->second.sortedPos = sortedEntries.size();
548 sortedEntries.push_back(it);
551 void removeFromSortedEntries(
size_t pos)
554 for (
size_t i = pos+1; i < sortedEntries.size(); i++)
555 (sortedEntries[i]->second.sortedPos)--;
556 sortedEntries.erase(sortedEntries.begin() + pos);
561 explicit SimpleCache(
size_t _maxWeight) : totalWeight(0), maxWeight(_maxWeight)
567 auto it = entryMap.find(key);
568 if (it != entryMap.end())
571 updateInSortedEntries(it);
572 return &(it->second.value);
579 {
return entryMap.find(key) != entryMap.end(); }
582 void put(
const K& key,
const V& value)
584 auto res = entryMap.insert({ key, Entry{ 0, 0, value } });
587 removeFromSortedEntries(res.first->second.sortedPos);
589 totalWeight -= res.first->second.value.weight();
590 res.first->second = Entry{ 0, 0, value };
592 const size_t elemWeight = value.weight();
595 if (elemWeight > maxWeight)
596 maxWeight = elemWeight<<1;
598 while (totalWeight+elemWeight > maxWeight)
601 auto minUsageIt = sortedEntries.back();
602 sortedEntries.pop_back();
603 totalWeight -= minUsageIt->second.value.weight();
604 entryMap.erase(minUsageIt);
607 insertToSortedEntries(res.first);
609 totalWeight += elemWeight;
const T * begin() const
get iterator to first element
Definition: Containers.h:253
T element_type
element type
Definition: Containers.h:46
Iter binaryMapFind(Iter begin, Iter end, const typename std::iterator_traits< Iter >::value_type::first_type &k)
binary find helper for array-map
Definition: Containers.h:440
void swap(Array &array)
swap two arrays
Definition: Containers.h:280
void allocate(size_t N)
only allocating space without keeping previous content
Definition: Containers.h:176
bool hasKey(const K &key)
return true if key exists
Definition: Containers.h:578
const T & operator[](size_t i) const
operator of indexing
Definition: Containers.h:161
VectorSet(size_t n, const T &v)
constructor
Definition: Containers.h:301
const T & front() const
get first element
Definition: Containers.h:267
an array class
Definition: Containers.h:41
VectorSet.
Definition: Containers.h:289
void resize(size_t N)
resize space with keeping old content
Definition: Containers.h:188
const T * const_iterator
type of constant iterator
Definition: Containers.h:45
Array()
empty constructor
Definition: Containers.h:51
Array(size_t N)
construct array of N elements
Definition: Containers.h:55
Array & operator=(Array &&cp) noexcept
move assignment
Definition: Containers.h:142
T & back()
get last element
Definition: Containers.h:276
VectorSet(It first, It last)
constructor
Definition: Containers.h:306
Array & operator=(std::initializer_list< T > list)
assignment from initializer list
Definition: Containers.h:154
Array(std::initializer_list< T > list)
constructor with initializer list
Definition: Containers.h:111
T * end()
get iterator to after last element
Definition: Containers.h:263
Array & operator=(const Array &cp)
copy assignment
Definition: Containers.h:134
void insertValue(const T &v)
insert new value if doesn't exists
Definition: Containers.h:314
T * data()
get data
Definition: Containers.h:249
T & front()
get first element
Definition: Containers.h:270
void put(const K &key, const V &value)
put value
Definition: Containers.h:582
void mapSort(Iter begin, Iter end)
map range of iterators
Definition: Containers.h:474
main namespace
Definition: AsmDefs.h:38
Array & assign(It b, It e)
assign from range of iterators
Definition: Containers.h:221
T * iterator
type of iterator
Definition: Containers.h:44
~Array()
destructor
Definition: Containers.h:130
Array(It b, It e)
construct array of elements in begin and end
Definition: Containers.h:65
T * begin()
get iterator to first element
Definition: Containers.h:256
SimpleCache(size_t _maxWeight)
constructor
Definition: Containers.h:561
Array(const Array &cp)
copy constructor
Definition: Containers.h:84
void eraseValue(const T &v)
erase value if exists
Definition: Containers.h:322
VectorSet(std::initializer_list< T > l)
constructor
Definition: Containers.h:310
V * use(const K &key)
use key - get value
Definition: Containers.h:565
Iter binaryFind(Iter begin, Iter end, const typename std::iterator_traits< Iter >::value_type &v)
binary find helper
Definition: Containers.h:411
bool hasValue(const T &v) const
return true if value present in vector set
Definition: Containers.h:330
size_t size() const
returns number of elements
Definition: Containers.h:172
const T * data() const
get data
Definition: Containers.h:246
void clear()
clear array
Definition: Containers.h:213
bool empty() const
returns true if empty
Definition: Containers.h:168
Array(Array &&cp) noexcept
move constructor
Definition: Containers.h:103
const T & back() const
get last element
Definition: Containers.h:273
const T * end() const
get iterator to after last element
Definition: Containers.h:260
VectorSet(size_t n)
constructor
Definition: Containers.h:297
Simple cache for object. object class should have a weight method.
Definition: Containers.h:501
VectorSet()
constructor
Definition: Containers.h:293