winix/core/ugcontainer.h

240 lines
4.2 KiB
C++
Executable File

/*
* This file is a part of Winix
* and is not publicly distributed
*
* Copyright (c) 2008-2010, Tomasz Sowa
* All rights reserved.
*
*/
#ifndef headerfile_winix_core_ugcontainer
#define headerfile_winix_core_ugcontainer
#include <vector>
#include <list>
#include <map>
#include <stdexcept>
#include "log.h"
template<class Type>
class UGContainer
{
public:
~UGContainer();
// we have to use a pointer (Type*) here
// because we are remembering some pointers to the items elsewhere
// so the pointers should not be invalidated
typedef typename std::vector<Type*> Table;
typedef typename Table::iterator Iterator;
typedef typename Table::size_type SizeType;
typedef typename std::map<long, SizeType> TableId;
typedef typename std::map<std::wstring, SizeType> TableName;
UGContainer();
Iterator Begin();
Iterator End();
SizeType Size();
bool Empty();
Iterator PushBack(const Type & type); // can return End() if the user already exists
void Clear();
bool Is(long id);
bool Is(const std::wstring & name);
Iterator FindId(long id);
Iterator FindName(const std::wstring & name);
Type & operator[](SizeType pos);
private:
void AddIndexes(SizeType pos);
void RebuildIndexes();
// main table
Table table;
// indexes
TableId table_id;
TableName table_name;
};
template<class Type>
UGContainer<Type>::~UGContainer()
{
Clear();
}
template<class Type>
UGContainer<Type>::UGContainer()
{
}
template<class Type>
typename UGContainer<Type>::Iterator UGContainer<Type>::Begin()
{
return table.begin();
}
template<class Type>
typename UGContainer<Type>::Iterator UGContainer<Type>::End()
{
return table.end();
}
template<class Type>
typename UGContainer<Type>::SizeType UGContainer<Type>::Size()
{
return table.size();
}
template<class Type>
bool UGContainer<Type>::Empty()
{
return table.empty();
}
template<class Type>
typename UGContainer<Type>::Iterator UGContainer<Type>::PushBack(const Type & type)
{
if( Is(type.id) || Is(type.name) )
return End();
table.push_back(new Type(type));
log << log2 << "UGCont: added, id: " << type.id << ", name: " << type.name << logend;
AddIndexes( table.size() - 1 );
return --table.end();
}
template<class Type>
void UGContainer<Type>::Clear()
{
for(size_t i=0 ; i<table.size() ; ++i)
delete table[i];
table.clear();
table_id.clear();
table_name.clear();
}
template<class Type>
bool UGContainer<Type>::Is(long id)
{
typename TableId::iterator i = table_id.find(id);
if( i == table_id.end() )
return false;
return true;
}
template<class Type>
bool UGContainer<Type>::Is(const std::wstring & name)
{
typename TableName::iterator i = table_name.find(name);
if( i == table_name.end() )
return false;
return true;
}
template<class Type>
typename UGContainer<Type>::Iterator UGContainer<Type>::FindId(long id)
{
typename TableId::iterator i = table_id.find(id);
if( i == table_id.end() )
return table.end();
return table.begin() + i->second;
}
template<class Type>
typename UGContainer<Type>::Iterator UGContainer<Type>::FindName(const std::wstring & name)
{
typename TableName::iterator i = table_name.find(name);
if( i == table_name.end() )
return table.end();
return table.begin() + i->second;
}
template<class Type>
void UGContainer<Type>::AddIndexes(UGContainer<Type>::SizeType pos)
{
table_id.insert( std::make_pair(table[pos]->id, pos) );
table_name.insert( std::make_pair(table[pos]->name, pos) );
log << log4 << "UGCont: added indexes to: id: " << table[pos]->id << ", name: " << table[pos]->name << logend;
}
template<class Type>
void UGContainer<Type>::RebuildIndexes()
{
log << log4 << "UGCont: rebuilding indexes" << logend;
table_id.clear();
table_name.clear();
SizeType i, len = table.size();
for(i=0 ; i!=len ; ++i)
AddIndexes( i );
log << log4 << "UGCont: indexes rebuilt, table.size: " << table.size() << ", table_id.size: "
<< table_id.size() << ", table_name.size: " << table_name.size() << logend;
}
template<class Type>
Type & UGContainer<Type>::operator[](UGContainer<Type>::SizeType pos)
{
if( pos >= table.size() )
throw std::out_of_range("UGContainer: operator[]: index is out of range");
return *table[pos];
}
#endif