void get( const LLUUID& key, callback_slot_t slot ) { if(key.isNull()) return; cache_t::const_iterator it = sCache.find(key); if (it != sCache.end()) { // ...name already exists in cache, fire callback now callback_signal_t signal; signal.connect(slot); signal(it->second); return; } fetch(key); // always store additional callback, even if request is pending signal_map_t::iterator sig_it = sSignalMap.find(key); if (sig_it == sSignalMap.end()) { // ...new callback for this id callback_signal_t* signal = new callback_signal_t(); signal->connect(slot); sSignalMap[key] = signal; } else { // ...existing callback, bind additional slot callback_signal_t* signal = sig_it->second; signal->connect(slot); } }
typename std::enable_if<(sizeof...(Args), std::is_same<cache_t, cache_vector_t>::value) && tp == timeout_policy_t::NEVER, std::remove_const_t<std::decay_t<R>>>::type const& call(Args const&... args) { decayed_key_t new_keys(getKey(args)...); auto&& it(cache.find(new_keys)); if (it == cache.end()) // not cached { if (cache.size() == LRU_MAX) // cache is full, time to overwrite old objects { if (internal_it == internal_cache.end()) { assert(cache_it == cache.end()); internal_it = internal_cache.begin(); cache_it = cache.begin(); } *internal_it = fptr(args...); cache.erase(cache_it); cache.insert(cache_it, std::pair<key_t, internal_cache_it_t>(new_keys, internal_it)); ++cache_it; return *internal_it++; } else { // Maybe emplace_back internal_cache.push_back(fptr(args...)); cache[new_keys] = internal_cache.end(); return internal_cache.back(); } } else // was cached return *it->second; };
void erase( const LLUUID& key ) { cache_t::iterator it = sCache.find(key); if(it != sCache.end()) { sCache.erase(it); } }
bool fetch( const LLUUID& key, bool refresh/* = true*/ ) { if(!key.isNull() && !isRequestPending(key) && (refresh || sCache.find(key)==sCache.end())) { LL_DEBUGS("ExperienceCache") << " queue request for " << EXPERIENCE_ID << " " << key << LL_ENDL ; sAskQueue[key]=EXPERIENCE_ID; return true; } return false; }
const LLSD& get(const LLUUID& key) { if(key.isNull()) return empty; cache_t::const_iterator it = sCache.find(key); if (it != sCache.end()) { return it->second; } fetch(key); return empty; }
typename std::enable_if<(sizeof...(Args), std::is_same<cache_t, cache_map_t>::value) && tp == timeout_policy_t::NEVER, std::remove_const_t<std::decay_t<R>>>::type const& call(Args const&... args) { decayed_key_t new_keys(getKey(args)...); auto&& it(cache.find(new_keys)); if (it == cache.end()) //not cached { auto&& pos(cache.insert(std::pair<key_t, R>(std::move(new_keys), fptr(args...)))); return pos.first->second; } else //was cached return it->second; };
int cycleLengthRecursive(unsigned int n) { // base case is handled by cache cache_t::iterator it = cache.find(n); if (it != cache.end()) { return it->second; } int out; if (n % 2 == 1) out = 1 + cycleLengthRecursive((n*3) + 1); // if n is odd then n <- 3n + 1 else out = 1 + cycleLengthRecursive(n/2); // else n <- n/2 cache[n] = out; return out; }
// static bool Dbase::CacheGet(Cddb::Matches& res, const Cddb::discid_t discID) { bool ret = false; for (cache_t::const_iterator it = s_cache.find(discID); it != s_cache.end(); ++it) { if (it->discID == discID) { ret = true; res.discID = discID; LOG(VB_MEDIA, LOG_DEBUG, QString("Cddb CacheGet %1 found %2 ") .arg(discID,0,16).arg(it->discID,0,16) + it->discGenre + " " + it->artist + " / " + it->title); // If it's marker for 'no match' then genre is empty if (!it->discGenre.isEmpty()) res.matches.push_back(Cddb::Match(*it)); } } return ret; }
//start = startIndex int mx(int start, int maxw) { if(start >= tt->size() || maxw <= 0) return 0; cache_t::iterator it = cache.find(PI(start, maxw)); if(it != cache.end()){ return it->second; } int without = mx(start+1, maxw); int res = without; int st_w = (*tt)[start].w; int st_st = (*tt)[start].st; int with = -1; if(st_w <= maxw){ int new_maxw = min(maxw - st_w, st_st - st_w); with = mx(start+1, new_maxw); if(1+with > res){ res = 1+with; } } cache[PI(start,maxw)] = res; return res; }
expr_ref bind_variables::abstract(expr* term, cache_t& cache, unsigned scope) { unsigned sz = m_todo.size(); m_todo.push_back(term); m_args.reset(); expr* b, *arg; while (m_todo.size() > sz) { expr* e = m_todo.back(); if (cache.contains(e)) { m_todo.pop_back(); continue; } switch(e->get_kind()) { case AST_VAR: { SASSERT(to_var(e)->get_idx() < scope); // mixing bound variables and free is possible for the caller, // but not proper use. // So we assert here even though we don't check for it. cache.insert(e, e); m_todo.pop_back(); break; } case AST_APP: { app* a = to_app(e); var2bound::obj_map_entry* w = m_var2bound.find_core(a); if (w) { var* v = w->get_data().m_value; if (!v) { // allocate a bound index. v = m.mk_var(m_names.size(), m.get_sort(a)); m_names.push_back(a->get_decl()->get_name()); m_bound.push_back(m.get_sort(a)); w->get_data().m_value = v; m_pinned.push_back(v); } if (scope == 0) { cache.insert(e, v); } else { var* v1 = m.mk_var(scope + v->get_idx(), m.get_sort(v)); m_pinned.push_back(v1); cache.insert(e, v1); } m_todo.pop_back(); break; } bool all_visited = true; bool some_diff = false; m_args.reset(); for (unsigned i = 0; i < a->get_num_args(); ++i) { arg = a->get_arg(i); if (!cache.find(arg, b)) { m_todo.push_back(arg); all_visited = false; } else if (all_visited) { m_args.push_back(b); if (b != arg) { some_diff = true; } } } if (all_visited) { if (some_diff) { b = m.mk_app(a->get_decl(), m_args.size(), m_args.c_ptr()); m_pinned.push_back(b); } else { b = a; } cache.insert(e, b); m_todo.pop_back(); } break; } case AST_QUANTIFIER: { quantifier* q = to_quantifier(e); expr_ref_buffer patterns(m); expr_ref result1(m); unsigned new_scope = scope + q->get_num_decls(); cache_t new_cache; for (unsigned i = 0; i < q->get_num_patterns(); ++i) { patterns.push_back(abstract(q->get_pattern(i), new_cache, new_scope)); } result1 = abstract(q->get_expr(), new_cache, new_scope); b = m.update_quantifier(q, patterns.size(), patterns.c_ptr(), result1.get()); m_pinned.push_back(b); cache.insert(e, b); m_todo.pop_back(); break; } default: UNREACHABLE(); } } return expr_ref(cache.find(term), m); }