added: winix function: rmuser
changed: UGContainer<> now uses std::list as a storage (previously it was using std::vector with pointers) removed: now we don't have the operator[] for UGContainer<> git-svn-id: svn://ttmath.org/publicrep/winix/trunk@816 e52654a7-88a9-db11-a3e9-0013d4bc506e
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
* This file is a part of Winix
|
||||
* and is not publicly distributed
|
||||
*
|
||||
* Copyright (c) 2008-2010, Tomasz Sowa
|
||||
* Copyright (c) 2008-2012, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
*/
|
||||
@@ -10,11 +10,8 @@
|
||||
#ifndef headerfile_winix_core_ugcontainer
|
||||
#define headerfile_winix_core_ugcontainer
|
||||
|
||||
#include <vector>
|
||||
#include <list>
|
||||
#include <map>
|
||||
#include <stdexcept>
|
||||
|
||||
#include "log.h"
|
||||
|
||||
|
||||
@@ -24,21 +21,17 @@ class UGContainer
|
||||
{
|
||||
public:
|
||||
|
||||
UGContainer();
|
||||
~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::list<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;
|
||||
typedef typename std::map<long, Iterator> TableId;
|
||||
typedef typename std::map<std::wstring, Iterator> TableName;
|
||||
|
||||
|
||||
UGContainer();
|
||||
|
||||
Iterator Begin();
|
||||
Iterator End();
|
||||
SizeType Size();
|
||||
@@ -52,17 +45,25 @@ public:
|
||||
Iterator FindId(long id);
|
||||
Iterator FindName(const std::wstring & name);
|
||||
|
||||
Type & operator[](SizeType pos);
|
||||
bool Remove(long id);
|
||||
|
||||
|
||||
|
||||
private:
|
||||
|
||||
void AddIndexes(SizeType pos);
|
||||
// don't copy these objects
|
||||
UGContainer(const UGContainer<Type> &);
|
||||
UGContainer<Type> & operator=(const UGContainer<Type> &);
|
||||
|
||||
void AddIndexes(Iterator iter);
|
||||
void RebuildIndexes();
|
||||
|
||||
// main table
|
||||
Table table;
|
||||
|
||||
// table.size() has O(n) complexity
|
||||
size_t table_size;
|
||||
|
||||
// indexes
|
||||
TableId table_id;
|
||||
TableName table_name;
|
||||
@@ -80,6 +81,23 @@ UGContainer<Type>::~UGContainer()
|
||||
template<class Type>
|
||||
UGContainer<Type>::UGContainer()
|
||||
{
|
||||
Clear();
|
||||
}
|
||||
|
||||
|
||||
// is private
|
||||
template<class Type>
|
||||
UGContainer<Type>::UGContainer(const UGContainer<Type> &)
|
||||
{
|
||||
Clear();
|
||||
}
|
||||
|
||||
// is private
|
||||
template<class Type>
|
||||
UGContainer<Type> & UGContainer<Type>::operator=(const UGContainer<Type> &)
|
||||
{
|
||||
Clear();
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
@@ -102,7 +120,7 @@ typename UGContainer<Type>::Iterator UGContainer<Type>::End()
|
||||
template<class Type>
|
||||
typename UGContainer<Type>::SizeType UGContainer<Type>::Size()
|
||||
{
|
||||
return table.size();
|
||||
return table_size;
|
||||
}
|
||||
|
||||
|
||||
@@ -121,12 +139,13 @@ typename UGContainer<Type>::Iterator UGContainer<Type>::PushBack(const Type & ty
|
||||
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 );
|
||||
table.push_back(type);
|
||||
table_size += 1;
|
||||
Iterator i = --table.end();
|
||||
log << log3 << "UGCont: added, id: " << type.id << ", name: " << type.name << logend;
|
||||
AddIndexes(i);
|
||||
|
||||
return --table.end();
|
||||
return i;
|
||||
}
|
||||
|
||||
|
||||
@@ -134,9 +153,7 @@ return --table.end();
|
||||
template<class Type>
|
||||
void UGContainer<Type>::Clear()
|
||||
{
|
||||
for(size_t i=0 ; i<table.size() ; ++i)
|
||||
delete table[i];
|
||||
|
||||
table_size = 0;
|
||||
table.clear();
|
||||
table_id.clear();
|
||||
table_name.clear();
|
||||
@@ -177,7 +194,7 @@ typename UGContainer<Type>::Iterator UGContainer<Type>::FindId(long id)
|
||||
if( i == table_id.end() )
|
||||
return table.end();
|
||||
|
||||
return table.begin() + i->second;
|
||||
return i->second;
|
||||
}
|
||||
|
||||
|
||||
@@ -190,18 +207,18 @@ typename UGContainer<Type>::Iterator UGContainer<Type>::FindName(const std::wstr
|
||||
if( i == table_name.end() )
|
||||
return table.end();
|
||||
|
||||
return table.begin() + i->second;
|
||||
return i->second;
|
||||
}
|
||||
|
||||
|
||||
|
||||
template<class Type>
|
||||
void UGContainer<Type>::AddIndexes(UGContainer<Type>::SizeType pos)
|
||||
void UGContainer<Type>::AddIndexes(UGContainer<Type>::Iterator iter)
|
||||
{
|
||||
table_id.insert( std::make_pair(table[pos]->id, pos) );
|
||||
table_name.insert( std::make_pair(table[pos]->name, pos) );
|
||||
table_id.insert( std::make_pair(iter->id, iter) );
|
||||
table_name.insert( std::make_pair(iter->name, iter) );
|
||||
|
||||
log << log4 << "UGCont: added indexes to: id: " << table[pos]->id << ", name: " << table[pos]->name << logend;
|
||||
log << log4 << "UGCont: added indexes to: id: " << iter->id << ", name: " << iter->name << logend;
|
||||
}
|
||||
|
||||
|
||||
@@ -209,30 +226,51 @@ void UGContainer<Type>::AddIndexes(UGContainer<Type>::SizeType pos)
|
||||
template<class Type>
|
||||
void UGContainer<Type>::RebuildIndexes()
|
||||
{
|
||||
Iterator i;
|
||||
|
||||
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 );
|
||||
|
||||
for(i=table.begin() ; i!=table.end() ; ++i)
|
||||
AddIndexes(i);
|
||||
|
||||
log << log4 << "UGCont: indexes rebuilt, table.size: " << table.size() << ", table_id.size: "
|
||||
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];
|
||||
|
||||
template<class Type>
|
||||
bool UGContainer<Type>::Remove(long id)
|
||||
{
|
||||
typename TableId::iterator i = table_id.find(id);
|
||||
typename TableName::iterator n;
|
||||
bool result = false;
|
||||
|
||||
if( i != table_id.end() )
|
||||
{
|
||||
for(n=table_name.begin() ; n != table_name.end() ; ++n)
|
||||
{
|
||||
if( n->second == i->second )
|
||||
{
|
||||
table_name.erase(n);
|
||||
log << log4 << "UGCont: removed index_id to: id: " << i->second->id << ", name: " << i->second->name << logend;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
log << log4 << "UGCont: removed index_name to: id: " << i->second->id << ", name: " << i->second->name << logend;
|
||||
log << log3 << "UGCont: removed: id: " << i->second->id << ", name: " << i->second->name << logend;
|
||||
table.erase(i->second);
|
||||
table_id.erase(i);
|
||||
result = true;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user