Example #1
0
 void set(int key, int value) {
     auto iter = mm.find(key);
     if(iter != mm.end()){
         iter->second->second = value;
         ll.splice(ll.begin(), ll, iter->second); // move the record
     }
     else {
         if(mm.size() == cap){
             auto back = ll.back(); // NOTICE: back() returns a reference
             mm.erase(back.first);
             ll.pop_back();
         }
         ll.emplace_front(key, value);
         mm[key] = ll.begin();
     }
 }
Example #2
0
static void
get_chrom(const bool VERBOSE, const GenomicRegion &r, 
	  const unordered_map<string, string>& chrom_files,
      GenomicRegion &chrom_region,  string &chrom) {
  const unordered_map<string, string>::const_iterator
                              fn(chrom_files.find(r.get_chrom()));  
  if (fn == chrom_files.end())
    throw SMITHLABException("could not find chrom: " + r.get_chrom());
  chrom.clear();
  read_fasta_file(fn->second, r.get_chrom(), chrom);
  if (chrom.empty()) 
    throw SMITHLABException("could not find chrom: " + r.get_chrom());
  else {
    chrom_region.set_chrom(r.get_chrom());
  }
}
	void createIndices(vector<int>& nums, int which)
	{
		for (int i = 0; i < (int)nums.size(); i++)
		{
			unordered_map<int, IndexNode*>::iterator it = mIndices.find(nums[i]);

			if (it != mIndices.end())
			{
				it->second->append(which, i);
			}
			else
			{
				mIndices.insert(pair<int, IndexNode *>(nums[i], new IndexNode(nums[i], which, i)));
			}
		}
	}
Example #4
0
		list<int> search (string &str)
		{
			if (str.length() == 0)
				return indexes;
			else
			{
				char first = str[0];
				if (children.find (first) != children.end())
				{
					string remainder = str.substr (1);
					return (children[first].search(remainder));
				}
			}

			return list<int>();
		}
Example #5
0
 void put(int key, int value) {
     auto it = data.find(key);
     if (it != data.end()) {
         access_order.splice(access_order.begin(), access_order, it->second.second);
         data[key] = {value, access_order.begin()};
     } else {
         if (current_capacity == _capacity) {
             data.erase(*access_order.rbegin());
             access_order.pop_back();
         } else {
             current_capacity++;
         }
         access_order.push_front(key);
         data[key] = {value, access_order.begin()};
     }
 }
    int helper(long n, unordered_map<long, int> &numStepsMap) {
        if (n <= 1)
            return 0;
        if (numStepsMap.find(n) != numStepsMap.end())
            return numStepsMap[n];

        int res;
        if (n % 2 == 0) {
            res = 1 + helper(n / 2, numStepsMap);
        } else {
            res = 1 + min( helper(n - 1, numStepsMap), helper(n + 1, numStepsMap) );
        }

        numStepsMap[n] = res;
        return res;
    }
 int findRoot(unordered_map<int, int> &dj, int x) {
     if (dj.find(x) == dj.end()) {
         return x;
     }
     int r = x;
     while (r != dj[r]) {
         r = dj[r];
     }
     int k = x;
     while (x != r) {
         x = dj[x];
         dj[k] = r;
         k = x;
     }
     return r;
 }
Example #8
0
 int32_t operator()(df::coord p1, df::coord p2) {
     if ( p1 == p2 ) return 0;
     auto i1 = pointCost->find(p1);
     auto i2 = pointCost->find(p2);
     if ( i1 == pointCost->end() && i2 == pointCost->end() )
         return p1 < p2;
     if ( i1 == pointCost->end() )
         return true;
     if ( i2 == pointCost->end() )
         return false;
     cost_t c1 = (*i1).second;
     cost_t c2 = (*i2).second;
     if ( c1 != c2 )
         return c1 < c2;
     return p1 < p2;
 }
 void backTrace(string s) {
     if (bt.find(s) == bt.end()) {
         // End of back trac
         vector<string> r(res);
         r.push_back(s);
         reverse(r.begin(), r.end());
         ans.push_back(r);
         return;
     }
     unordered_set<string> &v = bt[s];
     res.push_back(s);
     for (auto it = v.begin(); it != v.end(); ++it) {
         backTrace(*it);
     }
     res.pop_back();
 }
Example #10
0
        vector<int> findSubstring(string S, vector<string> &L)
        {
            vector<int> result;
            if(L.size() == 0) 
                return result;

            size_t wordLen = L[0].size();
            size_t wordNum = L.size();
            size_t wordsLen = wordLen * wordNum;

            if(S.size() < wordsLen)
                return result;

            //cout << "wordLen \t" << wordLen << endl;
            //cout << "wordNum \t" << wordNum<< endl;
            //cout << "wordsLen\t" << wordsLen << endl;

            initMap(L);
            //printMap();
            
            for(size_t i = 0; i <= S.size() - wordsLen; i++)
            {
                size_t j = i;
                for( /**/; j < (i + wordsLen); j += wordLen)
                {
                    //cout << "j\t" << j << endl;
                    //printMap();
                    string tmp = S.substr(j, wordLen);
                    if(m_map.find(tmp) != m_map.end() && m_map[tmp] > 0)  
                    {
                        m_map[tmp]--;
                    }
                    else
                    {
                        break;
                    }
                }
                //cout << "==j\t" << j << endl;
                if(j >= (i+wordsLen))
                {
                    result.push_back(i);
                }
                initMap(L);
            }
            return result;

        }
Example #11
0
df::building *Buildings::findAtTile(df::coord pos)
{
    auto occ = Maps::getTileOccupancy(pos);
    if (!occ || !occ->bits.building)
        return NULL;

    // Try cache lookup in case it works:
    auto cached = locationToBuilding.find(pos);
    if (cached != locationToBuilding.end())
    {
        auto building = df::building::find(cached->second);

        if (building && building->z == pos.z &&
            building->isSettingOccupancy() &&
            containsTile(building, pos, false))
        {
            return building;
        }
    }

    // The authentic method, i.e. how the game generally does this:
    auto &vec = df::building::get_vector();
    for (size_t i = 0; i < vec.size(); i++)
    {
        auto bld = vec[i];

        if (pos.z != bld->z ||
            pos.x < bld->x1 || pos.x > bld->x2 ||
            pos.y < bld->y1 || pos.y > bld->y2)
            continue;

        if (!bld->isSettingOccupancy())
            continue;

        if (bld->room.extents && bld->isExtentShaped())
        {
            auto etile = getExtentTile(bld->room, pos);
            if (!etile || !*etile)
                continue;
        }

        return bld;
    }

    return NULL;
}
Example #12
0
static void
get_chrom(const MappedRead &mr,
          const vector<string> &all_chroms,
          const unordered_map<string, size_t> &chrom_lookup,
          GenomicRegion &chrom_region, string &chrom) {

  const unordered_map<string, size_t>::const_iterator
    the_chrom(chrom_lookup.find(mr.r.get_chrom()));
  if (the_chrom == chrom_lookup.end())
    throw runtime_error("could not find chrom: " + mr.r.get_chrom());

  chrom = all_chroms[the_chrom->second];
  if (chrom.empty())
    throw runtime_error("could not find chrom: " + mr.r.get_chrom());

  chrom_region.set_chrom(mr.r.get_chrom());
}
Example #13
0
// Reduce an event name to a short string.
string LocalState::short_event_name (string longname)
{
  string shortname;
  auto siter = short_evname.find(longname);
  if (siter == short_evname.end()) {
    // Unknown name: Make up something for now, but we really ought to fix the
    // code to match the run-time library.
    static size_t id = 1;
    char sname[25];
    sprintf(sname, "E%lu", id++);
    shortname = string(sname);
    short_evname[longname] = shortname;
  }
  else
    shortname = siter->second;
  return shortname;
}
Example #14
0
 void set(int key, int value) {
     if (dict.find(key) != dict.end())
     {
         data.splice(data.begin(), data, dict[key]);
         dict[key]->val = value;
     }    
     else 
     {
         if (data.size() == capacity)
         {
             dict.erase(data.back().key);
             data.pop_back();
         }
         data.push_front(Node(key, value));
     }
     dict[key] = data.begin();
 }
Example #15
0
    bool extract(const string &str, vector<pair<string, double> > &keywords, size_t topN) const
    {
        vector<string> words;
        if (!_segment.cut(str, words))
        {
            LogError("segment cut(%s) failed.", str.c_str());
            return false;
        }

        map<string, double> wordmap;
        for (vector<string>::iterator iter = words.begin(); iter != words.end(); iter++)
        {
            if (_isSingleWord(*iter))
            {
                continue;
            }
            wordmap[*iter] += 1.0;
        }

        for (map<string, double>::iterator itr = wordmap.begin(); itr != wordmap.end(); )
        {
            if (_stopWords.end() != _stopWords.find(itr->first))
            {
                wordmap.erase(itr++);
                continue;
            }

            unordered_map<string, double>::const_iterator cit = _idfMap.find(itr->first);
            if (cit != _idfMap.end())
            {
                itr->second *= cit->second;
            }
            else
            {
                itr->second *= _idfAverage;
            }
            itr ++;
        }

        keywords.clear();
        std::copy(wordmap.begin(), wordmap.end(), std::inserter(keywords, keywords.begin()));
        topN = min(topN, keywords.size());
        partial_sort(keywords.begin(), keywords.begin() + topN, keywords.end(), _cmp);
        keywords.resize(topN);
        return true;
    }
Example #16
0
 int romanToInt(string s) {
     int res = 0;
     if (a2i.size() == 0) build_tlb();
     
     while (!s.empty()) {
         int len = s.length();
         for (int i=min(4, len); i>0; i--) {
             unordered_map<string, int>::iterator iter = a2i.find(s.substr(len-i));
             if (iter == a2i.end()) continue;
             res += iter->second;
             s.resize(len - i);
             break;
         }
     }
     
     return res;
 }
 vector<string> findRepeatedDnaSequences(string s) {
     std::hash<std::string> strHash;
     if (s.length() < 10) return vector<string>();
     for (int ind=0; ind <= s.length() - 10; ind++) {
         string cand = s.substr(ind, 10);
         auto it = seqCount.find(strHash(cand));
         if (it == seqCount.end()) {
             seqCount[strHash(cand)] = 1;
         } else {
             seqCount[strHash(cand)] += 1;
             if (seqCount[strHash(cand)] == 2) {
                 strMap.push_back(cand);
             }
         }
     }
     return strMap;
 }
Example #18
0
i64 get_mem_value(int xc, int yc, int aindex, int peri)
{
    xc += peri;
    yc += peri;
    i64 result = xc;
    result <<= 8;
    result += yc;
    result <<=8;
    result += aindex;
    result <<= 8;
    result += peri;
    auto iter =pmap.find(result);
    if(iter != pmap.end())
        return iter->second;
    else
        return -1;
}
Example #19
0
int RouteTableImpl::update_service_map(const ServiceItem& item,
    unordered_map<INSTANCE_ID_TYPE, ServiceItem>& new_map)
{
    unordered_map<INSTANCE_ID_TYPE, ServiceItem>::iterator it;
    it = new_map.find(item.instance_id);
    if (it != new_map.end())
    {
        LOG_FATAL("Duplicated service item, instance_name:" 
            << item.instance_name << ", instance_id:"
            << item.instance_id);
        return -1;
    }
    
    new_map[item.instance_id] = item;
    
    return 0;
}
Example #20
0
//1 if stan 0 if ollie
bool winner(unsigned long n, unsigned long p, bool current)
{
	for(int i = 9; i >= 2; i--)
	{
		//if current play has multiplication greater than n, then he wins
		if(p * i >= n) return current;
		//if not, search map see if can wins or not based on current state
		if(win.find(p * i) != win.end())
		{
			if(win[p * i]) return current;
			continue;
		}
		win[p * i] = winner(n, p * i, (current + 1) % 2) == current;
		if(win[p * i]) return current;
	}
	return (current + 1) % 2;
}
    bool isMatched_(vector<string> s, unordered_map<string,int> LL) {
        for(int i=0; i<s.size(); ++i) {
            if(LL.find(s[i])!=LL.end()) {
                if(--LL[s[i]]<0) {
                    return false;
                }
            } else {
                return false;
            }
        }

        for(unordered_map<string,int>::iterator its=LL.begin(); its!=LL.end(); its++) {
            if(its->second!=0)
                return false;
        }
        return true;
    }
void ExchangeAnalyzer::populateTraderFills( unordered_map<uint64_t, string>& traders,
											unordered_map<uint32_t, uint64_t>& clients,
											unordered_map<string, size_t>& traderFills
											) {
	for (auto msg : _ds->getOrderFills()) {
		shared_ptr<OrderFillMessage> fill_msg = static_pointer_cast<OrderFillMessage>(msg);  // downcast for accessing message specific info
		
		uint64_t t = clients[fill_msg->order_id];
		string trader_name = traders[t];				// lookup in clients and traders to get tradername.
		size_t total_qty = getFillQty(fill_msg);
		
		if (traderFills.find(trader_name) == traderFills.end())
			traderFills[trader_name] = total_qty;
		else
			traderFills[trader_name] += total_qty;
	}
}
Example #23
0
long long int MaxValue(long long int n)
{
	unordered_map<long long int,long long int>::const_iterator it = hashtable.find(n);

	if(it==hashtable.end())
	{
		long long int t = MaxValue(n/2)+MaxValue(n/3)+MaxValue(n/4);
		long long int val = maximum(n,t);
		hashtable[n] = val;
		return val;
	}
	else
	{
		return it->second;
	}

}
Example #24
0
	int get(int key)
	{
		if (to_index.find(key) == to_index.end())
		{
			return -1;
		}
		int index = to_index[key];
		++std::get<2>(huan_cun[index]);
		int result = std::get<1>(huan_cun[index]);
		if (index>0 && std::get<2>(huan_cun[index - 1]) <= std::get<2>(huan_cun[index]))
		{
			swap(huan_cun[index], huan_cun[index - 1]);
			to_index[std::get<0>(huan_cun[index])] = index;
			to_index[std::get<0>(huan_cun[index - 1])] = index - 1;
		}
		return result;
	}
Example #25
0
	vector<T*> findAll(const Str& name) const
	{
		auto it = data.find(name);

		Binding* binding = (it != data.end()) ? it->second : nullptr;

		vector<T*> result;

		while (binding)
		{
			result.push_back(binding->value);

			binding = binding->shadow;
		}

		return result;
	}
	void calNeighbor(vector<unordered_set<int> > & neighbor, int node, const vector<string> & vs, const unordered_map<string, int> & m) {
		string str = vs[node];
		for(size_t i=0; i < vs[node].length(); ++i) {
			char t = str[i];
			for(char ch='a'; ch <= 'z'; ++ch) {
				if ( ch != t) {
					str[i] = ch;
					auto iter = m.find(str);
					if ( iter != m.end()) {
						neighbor[node].insert(iter->second);
						neighbor[iter->second].insert(node); 
					}					
				}
			}
			str[i] = t;
		}
	}
Example #27
0
 void set(int key, int value) {
     if(hash.find(key) != hash.end()){
         iterator it = hash[key];
         q.erase(it);
         q.push_front(make_pair(key,value));
         hash[key] = q.begin();
     }
     else{
         if(q.size() == mCapacity){
             pii last = q.back();
             q.pop_back();
             hash.erase(last.first);
         }
         q.push_front(make_pair(key,value));
         hash[key] = q.begin();
     }
 }
Example #28
0
 void set(int key, int value) {
     if (m_map.find(key) == m_map.end()) {
         CacheEntry newItem(key, value);
         if (m_LRU_cache.size() >= m_capacity)
         {
             m_map.erase(m_LRU_cache.back().key);
             m_LRU_cache.pop_back();                
         }
         
         m_LRU_cache.push_front(newItem);
         m_map[key] = m_LRU_cache.begin();
         return;
     }
     
     m_map[key]->value = value;
     MoveToHead(key);
 }
 vector<vector<int>> getFactors(int n) {
     if(record.find(n)!=record.end()) return record[n];
     vector<vector<int>> result;
     for(int i=2; i*i<=n; i++) {
         if(n%i!=0) continue;
         result.push_back({i, n/i});
         vector<vector<int>> remnants = getFactors(n/i);
         for(auto r:remnants) {
             if(r[0]<i) continue;
             vector<int> sol(r);
             sol.insert(sol.begin(), i);
             result.push_back(sol);
         }
     }
     record[n] = result;
     return result;
 }
 bool isScr(string s1, int start1, int end1, string s2, int start2, int end2) {
     bool result = false;
     string s = s1.substr(start1, end1 - start1 + 1) + "#" + s2.substr(start2, end2 - start2 + 1);
     if (m.find(str_hash(s)) != m.end()) return m[str_hash(s)];
     
     if ((end1 - start1) != (end2 - start2)) result = false;
     if ((end1 < start1) || (end2 < start2)) result = false;
     if (s1.substr(start1, (end1 - start1 + 1)) == s2.substr(start2, (end2 - start2 + 1))) result = true;
     if (start1 == end1) result = (s1[start1]==s2[start2]);
     
     for (int i = 0; i < (end1 - start1); ++i) {
         if (isScr(s1, start1, start1 + i, s2, start2, start2 + i) && isScr(s1, start1 + i + 1, end1, s2, start2 + i + 1, end2)) result = true;
         if (isScr(s1, start1, start1 + i, s2, end2 - i, end2) && isScr(s1, start1 + i + 1, end1, s2, start2, end2 - i - 1)) result = true;
     }
     m[str_hash(s)] = result;
     return result;
 }