/** Inserts a new key <Key> with value 1. Or increments an existing key by 1. */ void inc (string key) { if (strmap.find(key) == strmap.end()) { if (matrix.empty() || matrix.back().val != 1) { //adding new key with val(1) at the bottom auto newrow = matrix.emplace(matrix.end(), key, 1); strmap[key] = make_pair(newrow, newrow->strs.begin()); } else { //adding new key with val(1) into the exsiting row auto newrow = --matrix.end(); newrow->strs.push_front(key); strmap[key] = make_pair(newrow, newrow->strs.begin()); } } else { auto row = strmap[key].first; auto col = strmap[key].second; auto lastrow = row; --lastrow; //if (lastrow == matrix.end() || lastrow->val != row->val + 1) { if (row == matrix.begin() || lastrow->val != row->val + 1) { //sorted by values from bigger to smaller auto newrow = matrix.emplace(row, key, row->val + 1); //insert different val with new string list strmap[key] = make_pair(newrow, newrow->strs.begin()); } else { //update existing val string list auto newrow = lastrow; newrow->strs.push_front(key); strmap[key] = make_pair(newrow, newrow->strs.begin()); } row->strs.erase(col); //remove the key from old string list if (row->strs.empty()) matrix.erase(row); } }
/** Decrements an existing key by 1. If Key's value is 1, remove it from the data structure. */ void dec(string key) { if (strmap.find(key) == strmap.end()) { return; } else { auto row = strmap[key].first; auto col = strmap[key].second; if (row->val == 1) { row->strs.erase(col); if (row->strs.empty()) { matrix.erase(row); strmap.erase(key); } return; } auto nextrow = row; ++nextrow; if (nextrow == matrix.end() || nextrow->val != row->val - 1) { auto newrow = matrix.emplace(nextrow, key, row->val - 1); strmap[key] = make_pair(newrow, newrow->strs.begin()); } else { auto newrow = nextrow; newrow->strs.push_front(key); strmap[key] = make_pair(newrow, newrow->strs.begin()); } row->strs.erase(col); if (row->strs.empty()) matrix.erase(row); } }
void set(int key, int value) { if(!cap) return ; if(dict.find(key) == dict.end()){ list<Row>::iterator row, newrow, nxtrow; list<pair<int,int>>::iterator col; if(curcap >= cap){ row = cache.end(); -- row; col = row->cols.end(); -- col; row->cols.pop_back(); if(row->cols.empty()) cache.erase(row); dict.erase(col->first); -- curcap; } if(cache.empty() || cache.back().freq != 1){ newrow = cache.emplace(cache.end(), key, value, 1); }else{ newrow = --cache.end(); newrow->cols.push_front(make_pair(key, value)); } dict[key] = make_pair(newrow, newrow->cols.begin()); ++ curcap; }else{ updateCache(key, value); } }
void set(int key, int value) { if (cacheMap.find(key) != cacheMap.end()) { *cacheMap[key] = make_pair(key, value); movetoFront(cacheMap[key]); } else { if (cacheMap.size() == size) { cacheMap.erase(cacheList.back().first); cacheList.back() = make_pair(key, value); movetoFront(--cacheList.end()); } else { cacheList.emplace(cacheList.begin(), key, value); } cacheMap[key] = cacheList.begin(); } }
void updateCache(int key, int value){ list<Row>::iterator row = dict[key].first, nxtrow, newrow; list<pair<int,int>>::iterator col = dict[key].second; nxtrow = row; -- nxtrow; if(nxtrow == cache.end() || nxtrow->freq != row->freq + 1){ newrow = cache.emplace(row, key, value, row->freq + 1); }else{ newrow = nxtrow; newrow->cols.push_front(make_pair(key, value)); } dict[key] = make_pair(newrow, newrow->cols.begin()); row->cols.erase(col); if(row->cols.empty()) cache.erase(row); }