Пример #1
0
void test1()
{
	typedef int value_type;
	libmaus2::util::SplayTree<value_type> ST;

	std::set<value_type> S;

	addElement(ST,S,5);
	addElement(ST,S,3);
	addElement(ST,S,7);
	addElement(ST,S,2);
	addElement(ST,S,1);
	addElement(ST,S,6);

	bool ok = true;

	for ( value_type i = 0; ok && i < 2048; ++i )
	{
		ok = ok && addElement(ST,S,i);
		ok = ok && addElement(ST,S,4096-i);
	}

	assert ( ok );

	eraseElement(ST,S,7);
	eraseElement(ST,S,5);
	eraseElement(ST,S,1);
	eraseElement(ST,S,6);
	eraseElement(ST,S,3);
	eraseElement(ST,S,2);

	for ( value_type i = 0; ok && i < 2048; ++i )
	{
		ok = ok && eraseElement(ST,S,i);
		ok = ok && eraseElement(ST,S,4096-i);
	}
}
void referencePage(unsigned long long pageId, unsigned long long *numReferences, unsigned long long *numFaults) {
    /* --------------------------------
     * LEAST RECENTLY USED ALGORITHM:
     * -------------------------------
     * If we find the requested page in our cache, then set its counter to our global counter value. Finished.
     * Else, if our cache is full, then erase the element with the lowest counter.
     * Then, insert the requested page and set its counter to our global counter value. Finished.
     */
    (*numReferences)++;
    if(findElement(lru->pages, pageId) >= 0) {
        setCounterOnElement(lru->pages, pageId, *numReferences); 
    }
    else {
        if(getSetSize(lru->pages) == lru->numPages)
            eraseElement(lru->pages, findMinElement(lru->pages));
        
        insertElement(lru->pages, pageId);
        setCounterOnElement(lru->pages, pageId, *numReferences);
        
        (*numFaults)++;
        printPageFault(pageId);
    }
}
Пример #3
0
void test2()
{
	typedef uint64_t value_type;
	libmaus2::util::SplayTree<value_type> ST;

	libmaus2::random::Random::setup();

	std::vector<value_type> V;
	for ( uint64_t i = 0; i < 4096; ++i )
		V.push_back(libmaus2::random::Random::rand64());

	std::set<value_type> S;

	bool ok = true;
	for ( uint64_t i = 0; ok && i < V.size(); ++i )
	{
		bool lok = addElement(ST,S,V[i]);

		if ( ! lok )
		{
			std::vector<uint64_t> prefix;
			for ( uint64_t j = 0; j <= i; ++j )
				prefix.push_back(V[j]);
			std::sort(prefix.begin(),prefix.end());

			std::cerr << "inserted sequence: ";
			for ( uint64_t j = 0; j <= i; ++j )
				std::cerr << std::lower_bound(prefix.begin(),prefix.end(),V[j])-prefix.begin() << ";";
			std::cerr << std::endl;

			assert ( lok );
		}

		ok = ok && lok;
	}

	for ( std::set<value_type>::const_iterator ita = S.begin(); ita != S.end(); ++ita )
	{
		value_type const v = *ita;
		assert ( ST.find(v) != -1 );
		assert ( ST.getKey(ST.find(v)) == v );

		std::set<value_type>::const_iterator itn = ita;
		itn++;

		if ( itn != S.end() )
		{
			assert ( ST.getNext(ST.find(v)) != -1 );
			assert ( ST.getKey(ST.getNext(ST.find(v))) == *itn );
		}
		else
		{
			assert ( ST.getNext(ST.find(v)) == -1 );
		}

		if ( ita == S.begin() )
			assert ( ST.getPrev(ST.find(v)) == -1 );
		else
		{
			std::set<value_type>::const_iterator itp = ita;
			--itp;
			assert ( ST.getPrev(ST.find(v)) != -1 );
			assert ( ST.getKey(ST.getPrev(ST.find(v))) == *itp );
		}
	}

	for ( uint64_t i = 0; ok && i < V.size(); ++i )
	{
		bool const lok = eraseElement(ST,S,V[i]);
		assert ( lok );
		ok = ok && lok;
	}
}