winix/core/ugcontainer.h

192 lines
3.3 KiB
C++
Executable File

/*
* This file is a part of CMSLU -- Content Management System like Unix
* and is not publicly distributed
*
* Copyright (c) 2008, Tomasz Sowa
* All rights reserved.
*
*/
#ifndef headerfileugcontainer
#define headerfileugcontainer
#include <vector>
#include <map>
#include <stdexcept>
#include "log.h"
template<class Type>
class UGContainer
{
public:
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::string, SizeType> TableName;
UGContainer();
Iterator Begin();
Iterator End();
SizeType Size();
bool Empty();
Iterator PushBack(const Type & type);
void Clear();
Iterator FindId(long id);
Iterator FindName(const std::string & 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() // : table(100)
{
}
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)
{
table.push_back(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()
{
table.clear();
table_id.clear();
table_name.clear();
}
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::string & 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 << log2 << "UGCont: added indexes to: id: " << table[pos].id << ", name: " << table[pos].name << logend;
}
template<class Type>
void UGContainer<Type>::RebuildIndexes()
{
log << log2 << "UGCont: rebuilding indexes" << logend;
table_id.clear();
table_name.clear();
SizeType i, len = table.size();
for(i=0 ; i!=len ; ++i)
AddIndexes( i );
log << log2 << "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