|
| 1 | +#pragma once |
| 2 | +#include <QPair> |
| 3 | +#include <QList> |
| 4 | +#include <QHash> |
| 5 | +#include <exception> |
| 6 | + |
| 7 | +typedef long int RowIndex; |
| 8 | + |
| 9 | +class CacheRange : public QPair<RowIndex, RowIndex> |
| 10 | +{ |
| 11 | +public: |
| 12 | + CacheRange(const RowIndex& f = -1, const RowIndex& s = -1) |
| 13 | + : QPair<RowIndex, RowIndex>(f, s) |
| 14 | + { |
| 15 | + } |
| 16 | + |
| 17 | + bool isEmpty() const |
| 18 | + { |
| 19 | + return first == -1 && second == -1; |
| 20 | + } |
| 21 | +}; |
| 22 | + |
| 23 | + |
| 24 | +template < typename T > class MappedCache |
| 25 | +{ |
| 26 | +public: |
| 27 | + |
| 28 | + MappedCache() |
| 29 | + : m_lastCacheRange(), |
| 30 | + m_size(0), |
| 31 | + m_valid(false) |
| 32 | + { |
| 33 | + } |
| 34 | + |
| 35 | + bool isValid() const |
| 36 | + { |
| 37 | + return m_valid; |
| 38 | + } |
| 39 | + |
| 40 | + void addLoadedRange(const CacheRange& range, const QList<T>& dataForRange) |
| 41 | + { |
| 42 | + if (!isValid()) |
| 43 | + clear(); |
| 44 | + |
| 45 | + m_mapping[range] = dataForRange; |
| 46 | + m_size += dataForRange.size(); |
| 47 | + |
| 48 | + if (range.second > m_lastCacheRange.second) |
| 49 | + m_lastCacheRange = range; |
| 50 | + } |
| 51 | + |
| 52 | + bool isRowLoaded(RowIndex index) |
| 53 | + { |
| 54 | + CacheRange i = findTargetRange(index); |
| 55 | + return !i.isEmpty(); |
| 56 | + } |
| 57 | + |
| 58 | + T getRow(RowIndex index) |
| 59 | + { |
| 60 | + if (!isRowLoaded(index)) |
| 61 | + return T(); |
| 62 | + |
| 63 | + CacheRange i = findTargetRange(index); |
| 64 | + qDebug() << "get row with index" << (index - i.first); |
| 65 | + return m_mapping[i].at(index - i.first); |
| 66 | + } |
| 67 | + |
| 68 | + T operator [](RowIndex index) { |
| 69 | + return getRow(index); |
| 70 | + } |
| 71 | + |
| 72 | + void replace(RowIndex index, T row) { |
| 73 | + if (!isRowLoaded(index)) { |
| 74 | + throw std::out_of_range("Invalid row"); |
| 75 | + } |
| 76 | + CacheRange i = findTargetRange(index); |
| 77 | + return m_mapping[i].replace(index - i.first, row); |
| 78 | + } |
| 79 | + |
| 80 | + void removeAt(RowIndex index) { |
| 81 | + if (!isRowLoaded(index)) { |
| 82 | + throw std::out_of_range("Invalid row"); |
| 83 | + } |
| 84 | + CacheRange i = findTargetRange(index); |
| 85 | + |
| 86 | + m_mapping[i].removeAt(index - i.first); |
| 87 | + CacheRange newKey{i.first, i.second - 1}; |
| 88 | + replaceRangeInMapping(newKey, i); |
| 89 | + m_valid = false; |
| 90 | + m_size -= 1; |
| 91 | + } |
| 92 | + |
| 93 | + void push_back(const T& row) { |
| 94 | + CacheRange newKey{0, 1}; |
| 95 | + |
| 96 | + if (!m_lastCacheRange.isEmpty()) { |
| 97 | + newKey.first += m_lastCacheRange.first; |
| 98 | + newKey.second += m_lastCacheRange.second; |
| 99 | + } |
| 100 | + |
| 101 | + m_mapping[m_lastCacheRange].push_back(row); |
| 102 | + replaceRangeInMapping(newKey); |
| 103 | + m_size += 1; |
| 104 | + } |
| 105 | + |
| 106 | + unsigned long long size() const |
| 107 | + { |
| 108 | + return m_size; |
| 109 | + } |
| 110 | + |
| 111 | + void clear() |
| 112 | + { |
| 113 | + m_mapping.clear(); |
| 114 | + m_size = 0; |
| 115 | + m_lastCacheRange = CacheRange(); |
| 116 | + m_valid = true; |
| 117 | + } |
| 118 | + |
| 119 | +private: |
| 120 | + CacheRange findTargetRange(RowIndex index) |
| 121 | + { |
| 122 | + for (auto i = m_mapping.constBegin(); |
| 123 | + i != m_mapping.constEnd(); ++i) |
| 124 | + { |
| 125 | + if (i.key().first <= index && index <= i.key().second) { |
| 126 | + return i.key(); |
| 127 | + } |
| 128 | + } |
| 129 | + return CacheRange(); |
| 130 | + } |
| 131 | + |
| 132 | + void replaceRangeInMapping(const CacheRange& newRange, const CacheRange& current = CacheRange()) |
| 133 | + { |
| 134 | + CacheRange replaceKey = current.isEmpty()? m_lastCacheRange : current; |
| 135 | + |
| 136 | + m_mapping[newRange] = m_mapping[replaceKey]; |
| 137 | + m_mapping.remove(current); |
| 138 | + |
| 139 | + if (current.isEmpty()) |
| 140 | + m_lastCacheRange = newRange; |
| 141 | + } |
| 142 | + |
| 143 | +private: |
| 144 | + CacheRange m_lastCacheRange; |
| 145 | + QHash<CacheRange, QList<T>> m_mapping; |
| 146 | + unsigned long long m_size; |
| 147 | + bool m_valid; |
| 148 | +}; |
0 commit comments