Ejemplo n.º 1
0
static bool SlowRekey(IntMap* m) {
  IntMap tmp;

  for (auto iter = m->iter(); !iter.done(); iter.next()) {
    if (NewKeyFunction::shouldBeRemoved(iter.get().key())) {
      continue;
    }
    uint32_t hi = NewKeyFunction::rekey(iter.get().key());
    if (tmp.has(hi)) {
      return false;
    }
    if (!tmp.putNew(hi, iter.get().value())) {
      return false;
    }
  }

  m->clear();
  for (auto iter = tmp.iter(); !iter.done(); iter.next()) {
    if (!m->putNew(iter.get().key(), iter.get().value())) {
      return false;
    }
  }

  return true;
}
TEST(ThreadLocalStorage, ThreadLocalPtrCount)
{
	// Create multiple objects in this thread
	// Only 1 ID should be added to the list
	TLSObject* o1 = new TLSObject();
	TLSObject* o2 = new TLSObject();
	TLSObject* o3 = new TLSObject();

	// Spawn a thread to create a new TLS int
	// This should add another ID to the list
	Thread thread1(JustAThread);
	thread1.join();

	// Spawn another thread
	// This should add another ID to the list
	Thread thread2(JustAThread);
	thread2.join();

	// Now, only 3 IDs should be added to the list

	typedef map<string, int> IntMap;
	IntMap map = ObjectManager::sIntMap;
	for(auto&& t : map)
	{
		std::cout << t.first << " : " << t.second << "\n";
	}

	ASSERT_EQ(3, map.size());
}
Ejemplo n.º 3
0
void TeletextBrowser::ChannelSwitched(int ChannelNumber) {
   cChannel *chan=Channels.GetByNumber(ChannelNumber);
   
   if (!chan)
      return;
      
   tChannelID chid=chan->GetChannelID();
   if (chid==channel || chid==tChannelID::InvalidID)
      return;
      
   channel=chid;
   
   //store page number of current channel
   IntMap::iterator it;
   channelPageMap[currentChannelNumber] = currentPage;
   currentChannelNumber=ChannelNumber;
   
   currentPage=0x100;
   currentSubPage=0;
      
   //see if last page number on this channel was stored
   it=channelPageMap.find(ChannelNumber);
   if (it != channelPageMap.end()) { //found
      currentPage=(*it).second;
   }
   
   //on the one hand this must work in background mode, when the plugin is not active.
   //on the other hand, if active, the page should be shown.
   //so this self-Pointer.
   if (self) {
      self->ShowPage();
   }
}
Ejemplo n.º 4
0
// L speedboosters
// t time to boost
// N stars
// C repeat
LL solve(int L, LL t, int N, int C, IntVec &v) {
	LL result = 0;

	IntMap cnt;
	int i;
	for (i = 0; i < C; ++i) {
		cnt[v[i]] += (N / C) + ((N % C) >= (i+1));
	}

	for (i = 0; result < t && i < N; ++i) {
		int d = v[i % C];
		result += d;
		cnt[d] -= 1;
		if (result > t) {
			d = (int)(result - t);
			cnt[d] += 1;
			result = t;
		}
	}

	IntMap::reverse_iterator it;
	for (it = cnt.rbegin(); it != cnt.rend(); ++it) {
		int can = min(L, it->second);
		result += it->first * can / 2;
		it->second -= can;
		L -= can;
	}
	for (it = cnt.rbegin(); it != cnt.rend(); ++it) {
		result += it->first * it->second;
	}

	return result;
}
Ejemplo n.º 5
0
	int find(int a, int b, int jLen, int bLen) {
		int result = 0;
		IntMap Brus;
		int j;
		int jm = b - jLen + 1;
		for (j = a; j <= jm; ++j) {
			int BrusScore = 9999;
			int k;
			int km = j + jLen - bLen;
			for (k = j; k <= km; ++k) {
				IntMap::const_iterator bf = Brus.find(k);
				if (bf == Brus.end()) {
					// not calculated yet
					int bs = 0;
					int l;
					int lm = k + bLen - 1;
					for (l = k; l <= lm; ++l) {
						bs += isLucky(l);
					}
					Brus[k] = bs;
					BrusScore = min(BrusScore, bs);
				} else {
					BrusScore = min(BrusScore, bf->second);
				}
			}
			result = max(result, BrusScore);
		}
		return result;
	}
Ejemplo n.º 6
0
	int rec(int mask) {
		if (m.find(mask) != m.end()) {
			return m[mask];
		}
		int &res = m[mask];

		res = 0;

		int i, prev, next;
		prev = 0;
		for (i = 1; i < (len-1); ++i) {
			int b = (1<<i);
			if (mask & b) {
				int a = mask ^ b;
				for (next = i+1; ; ++next) {
					if (mask & (1<<next)) {
						break;
					}
				}

				res = max(res, x[prev]*x[next]+rec(a));

				prev = i;
			}
		}
		return res;
	}
Ejemplo n.º 7
0
inline void AudioDataOutput::convertAndEmit(const QVector<qint16> &leftBuffer, const QVector<qint16> &rightBuffer)
{
    //TODO: Floats
    IntMap map;
    map.insert(Phonon::AudioDataOutput::LeftChannel, leftBuffer);
    map.insert(Phonon::AudioDataOutput::RightChannel, rightBuffer);
    emit dataReady(map);
}
Ejemplo n.º 8
0
 int getRating(const std::string &game)
 {
   int rate = -1;
   if(ratings.find(game) != ratings.end())
     rate = ratings[game];
   if(rate < -1 || rate > 5) rate = -1;
   return rate;
 }
	static void addInt(const string& name, int i)
	{
		ScopedLock<Mutex> l(mutex);
		IntMap::iterator it = sIntMap.find(name);
		if(it == sIntMap.end())
		{
			sIntMap.insert(IntMap::value_type(name, i));
		}
	}
Ejemplo n.º 10
0
        IntMap multiplyMap(const IntMap& m, int f)
        {
            IntMap ret;
            for (IntMap::const_iterator it = m.begin(); it != m.end(); ++it)
            {
                ret[it->first] = it->second * f;
            }

            return ret;
        }
Ejemplo n.º 11
0
void test_erase_map() {
    IntMap map;
    for (int i = 0; i < 100; ++i) {
        map[i] = i + 10000;
    }

    std::cout << "map_size: " << map.size() << std::endl;

    for (IntMap::iterator it = map.begin(); it != map.end(); ++it) {
        std::cout << it->first << ": "  << it->second << std::endl;

        if (it->first % 2 == 0) {
            map.erase(it->first);
        }
    }

    std::cout << "clean finish" << std::endl;
    std::cout << map.size() << std::endl;

    for (IntMap::iterator it = map.begin(); it != map.end(); ++it) {
        std::cout << it->first << ": "  << it->second << std::endl;
    }

    std::cout << map.size() << std::endl;
}
Ejemplo n.º 12
0
bool utl::renumber (int& num, int& runner, IntMap& old2new)
{
  IntMap::iterator it = old2new.find(num);
  if (it == old2new.end())
    it = old2new.insert(std::make_pair(num,++runner)).first;

  if (num == it->second) return false;

  num = it->second;
  return true;
}
Ejemplo n.º 13
0
  void write()
  {
    Json::Value v;

    for(IntMap::iterator it = ratings.begin();
        it != ratings.end(); it++)
      {
        v[it->first] = it->second;
      }

    std::string filename = get.getPath("ratings.json");
    writeJson(filename, v);
  }
Ejemplo n.º 14
0
bool utl::renumber (int& num, const IntMap& old2new, bool msg)
{
  IntMap::const_iterator it = old2new.find(num);
  if (it == old2new.end())
  {
    if (msg)
      std::cerr <<" *** utl::renumber: Old value "<< num
                <<" does not exist in old2new mapping"<< std::endl;
    return false;
  }

  num = it->second;
  return true;
}
Ejemplo n.º 15
0
        vector<int> topKFrequent(vector<int>& nums, int k) {
            typedef std::map<int, int> IntMap;
            typedef IntMap::iterator IMIter;

            IntMap countMap;
            for(size_t i = 0, sz = nums.size(); i < sz; ++i)
            {
                if(countMap.find(nums[i]) != countMap.end())
                {
                    countMap[nums[i]] = countMap[nums[i]] + 1;
                }
                else
                {
                    countMap[nums[i]] = 1;
                }
            }

            typedef std::map<int, std::vector<int>* > OrderMap;
            typedef OrderMap::iterator OMIter;
            typedef OrderMap::reverse_iterator OMRter;
            OrderMap orderMap;
            for(IMIter itr = countMap.begin(), end = countMap.end(); itr != end; ++itr)
            {
                if(orderMap.find(itr->second) != orderMap.end())
                {
                    orderMap[itr->second]->push_back(itr->first);
                }
                else
                {
                    std::vector<int>* vec = new std::vector<int>();
                    vec->push_back(itr->first);
                    orderMap[itr->second] = vec;
                }
            }

            std::vector<int> result;
            int count = 0;
            for(OMRter itr = orderMap.rbegin(), end = orderMap.rend(); itr != end && count != k; ++itr)
            {
                std::vector<int>* vec = itr->second;
                for(size_t i = 0, sz = vec->size(); i < sz && count != k; ++i, ++count)
                {
                    result.push_back((*vec)[i]);
                }
            }

            return result;
        }
Ejemplo n.º 16
0
/// Read comma separated list of integers representing a map. There must be an
/// an even number of integers - even numbers are keys, odd numbers are values.
/// For example the string "1,5,8,1" specifies a map where 1 -> 5 and 8 -> 1.
static IntMap read_map(std::istream &in)
{
  IntMap map;
  std::string contents;
  while (1) {
    auto c = in.get();
    if (!in.good())
      break;
    if (std::isspace(c))
      continue;
    contents.push_back(c);
  }
  // Parse into a list of integers.
  std::vector<long> values;
  const char *p = contents.c_str();
  while (*p != '\0') {
    char *endp;
    auto value = std::strtol(p, &endp, 10);
    if (endp == p) {
      std::cerr << "error: unexpected format\n";
      std::exit(1);
    }
    values.push_back(value);
    p = endp;
    if (*p == ',') {
      ++p;
    } else if (*p != '\0') {
      std::cerr << "error: unexpected format\n";
      std::exit(1);
    }
  }
  if ((values.size() % 2) != 0) {
    std::cerr << "error: odd number of values\n";
    std::exit(1);
  }
  // Add values to map.
  for (unsigned i = 0, e = values.size(); i != e; i += 2) {
    map.insert(std::make_pair(values[i], values[i + 1]));
  }
  return map;
}
Ejemplo n.º 17
0
static bool
SlowRekey(IntMap *m) {
    IntMap tmp;
    tmp.init();

    for (IntMap::Range r = m->all(); !r.empty(); r.popFront()) {
        if (NewKeyFunction::shouldBeRemoved(r.front().key))
            continue;
        uint32_t hi = NewKeyFunction::rekey(r.front().key);
        if (tmp.has(hi))
            return false;
        tmp.putNew(hi, r.front().value);
    }

    m->clear();
    for (IntMap::Range r = tmp.all(); !r.empty(); r.popFront()) {
        m->putNew(r.front().key, r.front().value);
    }

    return true;
}
Ejemplo n.º 18
0
void HashMapTest::testInsert()
{
	const int N = 1000;

	typedef HashMap<int, int> IntMap;
	IntMap hm;
	
	assert (hm.empty());
	
	for (int i = 0; i < N; ++i)
	{
		std::pair<IntMap::Iterator, bool> res = hm.insert(IntMap::ValueType(i, i*2));
		assert (res.first->first == i);
		assert (res.first->second == i*2);
		assert (res.second);
		IntMap::Iterator it = hm.find(i);
		assert (it != hm.end());
		assert (it->first == i);
		assert (it->second == i*2);
		assert (hm.size() == i + 1);
	}		
	
	assert (!hm.empty());
	
	for (int i = 0; i < N; ++i)
	{
		IntMap::Iterator it = hm.find(i);
		assert (it != hm.end());
		assert (it->first == i);
		assert (it->second == i*2);
	}
	
	for (int i = 0; i < N; ++i)
	{
		std::pair<IntMap::Iterator, bool> res = hm.insert(IntMap::ValueType(i, 0));
		assert (res.first->first == i);
		assert (res.first->second == i*2);
		assert (!res.second);
	}		
}
Ejemplo n.º 19
0
void HashMapTest::testConstIterator()
{
	const int N = 1000;

	typedef HashMap<int, int> IntMap;
	IntMap hm;

	for (int i = 0; i < N; ++i)
	{
		hm.insert(IntMap::ValueType(i, i*2));
	}
	
	std::map<int, int> values;
	IntMap::ConstIterator it = hm.begin();
	while (it != hm.end())
	{
		assert (values.find(it->first) == values.end());
		values[it->first] = it->second;
		++it;
	}
	
	assert (values.size() == N);
}
Ejemplo n.º 20
0
// Add entries to a HashMap until either we get an OOM, or the table has been
// resized a few times.
static bool GrowUntilResize() {
  IntMap m;

  // Add entries until we've resized the table four times.
  size_t lastCapacity = m.capacity();
  size_t resizes = 0;
  uint32_t key = 0;
  while (resizes < 4) {
    auto p = m.lookupForAdd(key);
    if (!p && !m.add(p, key, 0)) {
      return false;  // OOM'd in lookupForAdd() or add()
    }

    size_t capacity = m.capacity();
    if (capacity != lastCapacity) {
      resizes++;
      lastCapacity = capacity;
    }
    key++;
  }

  return true;
}
Ejemplo n.º 21
0
static void
reduce_using_and(IntMap &map, std::vector<std::unique_ptr<Step>> &steps)
{
    IntPairs map_entries(begin(map), end(map));
    IntMap tmp;
    tmp.reserve(map_entries.size());
    std::bitset<32> mask;
    for (int bit = 31; bit >= 0; bit--) {
        auto candidate_mask = mask;
        candidate_mask.set(bit);
        auto bitmask = ~static_cast<uint32_t>(candidate_mask.to_ulong());
        auto hash = [=](uint32_t x) {
            return x & bitmask;
        };
        if (apply_hash_to_map(map_entries, hash, tmp) &&
                tmp.size() < map_entries.size()) {
            std::swap(map, tmp);
            mask = candidate_mask;
        }
    }
    if (mask.any()) {
        steps.push_back(std::unique_ptr<Step>(new AndNotStep(mask.to_ulong())));
    }
}
Ejemplo n.º 22
0
void HashMapTest::testIndex()
{
	typedef HashMap<int, int> IntMap;
	IntMap hm;

	hm[1] = 2;
	hm[2] = 4;
	hm[3] = 6;
	
	assert (hm.size() == 3);
	assert (hm[1] == 2);
	assert (hm[2] == 4);
	assert (hm[3] == 6);
	
	try
	{
		const IntMap& im = hm;
		int x = im[4];
		fail("no such key - must throw");
	}
	catch (Poco::NotFoundException&)
	{
	}
}
Ejemplo n.º 23
0
void HashMapTest::testIterator()
{
	const int N = 1000;

	typedef HashMap<int, int> IntMap;
	IntMap hm;

	for (int i = 0; i < N; ++i)
	{
		hm.insert(IntMap::ValueType(i, i*2));
	}
	
	std::map<int, int> values;
	IntMap::Iterator it; // do not initialize here to test proper behavior of uninitialized iterators
	it = hm.begin();
	while (it != hm.end())
	{
		assert (values.find(it->first) == values.end());
		values[it->first] = it->second;
		++it;
	}
	
	assert (values.size() == N);
}
Ejemplo n.º 24
0
// Add entries to a HashMap using lookupWithDefault until either we get an OOM,
// or the table has been resized a few times.
static bool
LookupWithDefaultUntilResize() {
    IntMap m;

    if (!m.init())
        return false;

    // Add entries until we've resized the table four times.
    size_t lastCapacity = m.capacity();
    size_t resizes = 0;
    uint32_t key = 0;
    while (resizes < 4) {
        if (!m.lookupWithDefault(key++, 0))
            return false;

        size_t capacity = m.capacity();
        if (capacity != lastCapacity) {
            resizes++;
            lastCapacity = capacity;
        }
    }

    return true;
}
Ejemplo n.º 25
0
bool Init()
{
	int nRandom = 0;
	for (int i = 0; i < g_nMax; ++i)
	{
		nRandom = i;
		g_nSummary += nRandom;
		g_dInts.push_back(nRandom);
		g_hmInts.insert(std::make_pair(nRandom, nRandom));
		g_hsInts.insert(nRandom);
		g_lInts.push_back(nRandom);
		g_mInts.insert(std::make_pair(nRandom, nRandom));
		g_mmInts.insert(std::make_pair(nRandom, nRandom));
		g_msInts.insert(nRandom);
		g_sInts.insert(nRandom);
		g_vInts.push_back(nRandom);
	}
	return true;
}
Ejemplo n.º 26
0
static bool MapsAreEqual(IntMap& am, IntMap& bm) {
  bool equal = true;
  if (am.count() != bm.count()) {
    equal = false;
    fprintf(stderr, "A.count() == %u and B.count() == %u\n", am.count(),
            bm.count());
  }
  for (auto iter = am.iter(); !iter.done(); iter.next()) {
    if (!bm.has(iter.get().key())) {
      equal = false;
      fprintf(stderr, "B does not have %x which is in A\n", iter.get().key());
    }
  }
  for (auto iter = bm.iter(); !iter.done(); iter.next()) {
    if (!am.has(iter.get().key())) {
      equal = false;
      fprintf(stderr, "A does not have %x which is in B\n", iter.get().key());
    }
  }
  return equal;
}
Ejemplo n.º 27
0
static bool
MapsAreEqual(IntMap& am, IntMap& bm)
{
    bool equal = true;
    if (am.count() != bm.count()) {
        equal = false;
        fprintf(stderr, "A.count() == %u and B.count() == %u\n", am.count(), bm.count());
    }
    for (IntMap::Range r = am.all(); !r.empty(); r.popFront()) {
        if (!bm.has(r.front().key())) {
            equal = false;
            fprintf(stderr, "B does not have %x which is in A\n", r.front().key());
        }
    }
    for (IntMap::Range r = bm.all(); !r.empty(); r.popFront()) {
        if (!am.has(r.front().key())) {
            equal = false;
            fprintf(stderr, "A does not have %x which is in B\n", r.front().key());
        }
    }
    return equal;
}
Ejemplo n.º 28
0
	int maxEnergy(vector <int> weight) {
		m.clear();
		memset(x, 0, sizeof(x));
		len = weight.size();
		if (len < 3) {
			return 0;
		}
		if (len == 3) {
			return weight[0]*weight[2];
		}
		int i;
		for (i = 0; i < len; ++i) {
			x[i] = weight[i];
		}
		int all_mask = (1<<len)-1;
		int mid_mask = ((1<<(len-2))-1)*2;
		int end_mask = all_mask ^ mid_mask;
		m[end_mask] = 0;
		return rec(all_mask);
	}
Ejemplo n.º 29
0
int utl::findKey (const IntMap& iMap, int iVal)
{
  IntMap::const_iterator it = utl::findValue(iMap,iVal);
  return it == iMap.end() ? iVal : it->first;
}
Ejemplo n.º 30
0
void HashMapTest::testErase()
{
	const int N = 1000;

	typedef HashMap<int, int> IntMap;
	IntMap hm;

	for (int i = 0; i < N; ++i)
	{
		hm.insert(IntMap::ValueType(i, i*2));
	}
	assert (hm.size() == N);
	
	for (int i = 0; i < N; i += 2)
	{
		hm.erase(i);
		IntMap::Iterator it = hm.find(i);
		assert (it == hm.end());
	}
	assert (hm.size() == N/2);
	
	for (int i = 0; i < N; i += 2)
	{
		IntMap::Iterator it = hm.find(i);
		assert (it == hm.end());
	}
	
	for (int i = 1; i < N; i += 2)
	{
		IntMap::Iterator it = hm.find(i);
		assert (it != hm.end());
		assert (*it == i);
	}

	for (int i = 0; i < N; i += 2)
	{
		hm.insert(IntMap::ValueType(i, i*2));
	}
	
	for (int i = 0; i < N; ++i)
	{
		IntMap::Iterator it = hm.find(i);
		assert (it != hm.end());
		assert (it->first == i);
		assert (it->second == i*2);		
	}
}