2008-12-21 22:17:09 +01:00
|
|
|
/*
|
2010-02-28 01:08:10 +01:00
|
|
|
* This file is a part of Winix
|
2014-10-04 20:04:03 +02:00
|
|
|
* and is distributed under the 2-Clause BSD licence.
|
|
|
|
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2021-06-27 23:31:50 +02:00
|
|
|
* Copyright (c) 2008-2021, Tomasz Sowa
|
2008-12-21 22:17:09 +01:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
2014-10-04 20:04:03 +02:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
|
|
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
2008-12-21 22:17:09 +01:00
|
|
|
*/
|
|
|
|
|
2011-01-23 15:15:30 +01:00
|
|
|
#ifndef headerfile_winix_core_ugcontainer
|
|
|
|
#define headerfile_winix_core_ugcontainer
|
2008-12-21 22:17:09 +01:00
|
|
|
|
2011-09-13 08:08:34 +02:00
|
|
|
#include <list>
|
2008-12-21 22:17:09 +01:00
|
|
|
#include <map>
|
2018-11-21 12:03:53 +01:00
|
|
|
#include "winixbase.h"
|
2008-12-21 22:17:09 +01:00
|
|
|
|
|
|
|
|
2014-02-12 17:30:49 +01:00
|
|
|
namespace Winix
|
|
|
|
{
|
|
|
|
|
|
|
|
|
2008-12-21 22:17:09 +01:00
|
|
|
|
|
|
|
template<class Type>
|
2018-11-21 12:03:53 +01:00
|
|
|
class UGContainer : public WinixBase
|
2008-12-21 22:17:09 +01:00
|
|
|
{
|
|
|
|
public:
|
2011-09-13 08:08:34 +02:00
|
|
|
|
2012-03-09 03:36:25 +01:00
|
|
|
UGContainer();
|
2011-09-13 08:08:34 +02:00
|
|
|
~UGContainer();
|
|
|
|
|
2012-03-09 03:36:25 +01:00
|
|
|
typedef typename std::list<Type> Table;
|
|
|
|
typedef typename Table::iterator Iterator;
|
|
|
|
typedef typename Table::size_type SizeType;
|
2008-12-21 22:17:09 +01:00
|
|
|
|
2012-03-09 03:36:25 +01:00
|
|
|
typedef typename std::map<long, Iterator> TableId;
|
|
|
|
typedef typename std::map<std::wstring, Iterator> TableName;
|
2008-12-21 22:17:09 +01:00
|
|
|
|
|
|
|
|
|
|
|
Iterator Begin();
|
|
|
|
Iterator End();
|
|
|
|
SizeType Size();
|
|
|
|
bool Empty();
|
2021-06-27 23:31:50 +02:00
|
|
|
Iterator PushBack(const Type & type, const std::wstring & name); // can return End() if the user already exists
|
2008-12-21 22:17:09 +01:00
|
|
|
void Clear();
|
|
|
|
|
2010-02-28 01:08:10 +01:00
|
|
|
bool Is(long id);
|
2010-11-21 01:19:17 +01:00
|
|
|
bool Is(const std::wstring & name);
|
2010-02-28 01:08:10 +01:00
|
|
|
|
2008-12-21 22:17:09 +01:00
|
|
|
Iterator FindId(long id);
|
2010-11-21 01:19:17 +01:00
|
|
|
Iterator FindName(const std::wstring & name);
|
2008-12-21 22:17:09 +01:00
|
|
|
|
2012-03-09 03:36:25 +01:00
|
|
|
bool Remove(long id);
|
|
|
|
|
2008-12-21 22:17:09 +01:00
|
|
|
|
|
|
|
|
2021-06-27 23:31:50 +02:00
|
|
|
|
|
|
|
// main table
|
|
|
|
Table table;
|
|
|
|
|
|
|
|
|
2008-12-21 22:17:09 +01:00
|
|
|
private:
|
|
|
|
|
2012-03-09 03:36:25 +01:00
|
|
|
// don't copy these objects
|
|
|
|
UGContainer(const UGContainer<Type> &);
|
|
|
|
UGContainer<Type> & operator=(const UGContainer<Type> &);
|
|
|
|
|
2021-06-27 23:31:50 +02:00
|
|
|
void AddIndexes(Iterator iter, const std::wstring & name);
|
2008-12-21 22:17:09 +01:00
|
|
|
void RebuildIndexes();
|
|
|
|
|
|
|
|
|
2012-03-09 03:36:25 +01:00
|
|
|
// table.size() has O(n) complexity
|
|
|
|
size_t table_size;
|
|
|
|
|
2008-12-21 22:17:09 +01:00
|
|
|
// indexes
|
|
|
|
TableId table_id;
|
|
|
|
TableName table_name;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template<class Type>
|
2011-09-13 08:08:34 +02:00
|
|
|
UGContainer<Type>::~UGContainer()
|
|
|
|
{
|
|
|
|
Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<class Type>
|
|
|
|
UGContainer<Type>::UGContainer()
|
2008-12-21 22:17:09 +01:00
|
|
|
{
|
2012-03-09 03:36:25 +01:00
|
|
|
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;
|
2008-12-21 22:17:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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()
|
|
|
|
{
|
2012-03-09 03:36:25 +01:00
|
|
|
return table_size;
|
2008-12-21 22:17:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template<class Type>
|
|
|
|
bool UGContainer<Type>::Empty()
|
|
|
|
{
|
|
|
|
return table.empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template<class Type>
|
2021-06-27 23:31:50 +02:00
|
|
|
typename UGContainer<Type>::Iterator UGContainer<Type>::PushBack(const Type & type, const std::wstring & name)
|
2008-12-21 22:17:09 +01:00
|
|
|
{
|
2021-06-27 23:31:50 +02:00
|
|
|
if( Is(type.id) || Is(name) )
|
2010-02-28 01:08:10 +01:00
|
|
|
return End();
|
|
|
|
|
2012-03-09 03:36:25 +01:00
|
|
|
table.push_back(type);
|
|
|
|
table_size += 1;
|
|
|
|
Iterator i = --table.end();
|
2021-06-27 23:31:50 +02:00
|
|
|
log << log3 << "UGCont: added, id: " << type.id << ", name: " << name << logend;
|
|
|
|
AddIndexes(i, name);
|
2008-12-21 22:17:09 +01:00
|
|
|
|
2012-03-09 03:36:25 +01:00
|
|
|
return i;
|
2008-12-21 22:17:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template<class Type>
|
|
|
|
void UGContainer<Type>::Clear()
|
|
|
|
{
|
2012-03-09 03:36:25 +01:00
|
|
|
table_size = 0;
|
2008-12-21 22:17:09 +01:00
|
|
|
table.clear();
|
|
|
|
table_id.clear();
|
|
|
|
table_name.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-02-28 01:08:10 +01:00
|
|
|
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>
|
2010-11-21 01:19:17 +01:00
|
|
|
bool UGContainer<Type>::Is(const std::wstring & name)
|
2010-02-28 01:08:10 +01:00
|
|
|
{
|
|
|
|
typename TableName::iterator i = table_name.find(name);
|
|
|
|
|
|
|
|
if( i == table_name.end() )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2008-12-21 22:17:09 +01:00
|
|
|
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();
|
|
|
|
|
2012-03-09 03:36:25 +01:00
|
|
|
return i->second;
|
2008-12-21 22:17:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template<class Type>
|
2010-11-21 01:19:17 +01:00
|
|
|
typename UGContainer<Type>::Iterator UGContainer<Type>::FindName(const std::wstring & name)
|
2008-12-21 22:17:09 +01:00
|
|
|
{
|
|
|
|
typename TableName::iterator i = table_name.find(name);
|
|
|
|
|
|
|
|
if( i == table_name.end() )
|
|
|
|
return table.end();
|
|
|
|
|
2012-03-09 03:36:25 +01:00
|
|
|
return i->second;
|
2008-12-21 22:17:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template<class Type>
|
2021-06-27 23:31:50 +02:00
|
|
|
void UGContainer<Type>::AddIndexes(UGContainer<Type>::Iterator iter, const std::wstring & name)
|
2008-12-21 22:17:09 +01:00
|
|
|
{
|
2012-03-09 03:36:25 +01:00
|
|
|
table_id.insert( std::make_pair(iter->id, iter) );
|
2021-06-27 23:31:50 +02:00
|
|
|
table_name.insert( std::make_pair(name, iter) );
|
2008-12-21 22:17:09 +01:00
|
|
|
|
2021-06-27 23:31:50 +02:00
|
|
|
log << log4 << "UGCont: added indexes to: id: " << iter->id << ", name: " << name << logend;
|
2008-12-21 22:17:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template<class Type>
|
|
|
|
void UGContainer<Type>::RebuildIndexes()
|
|
|
|
{
|
2012-03-09 03:36:25 +01:00
|
|
|
Iterator i;
|
|
|
|
|
2011-01-05 22:24:11 +01:00
|
|
|
log << log4 << "UGCont: rebuilding indexes" << logend;
|
2008-12-21 22:17:09 +01:00
|
|
|
|
|
|
|
table_id.clear();
|
|
|
|
table_name.clear();
|
2012-03-09 03:36:25 +01:00
|
|
|
|
|
|
|
for(i=table.begin() ; i!=table.end() ; ++i)
|
|
|
|
AddIndexes(i);
|
2008-12-21 22:17:09 +01:00
|
|
|
|
2012-03-09 03:36:25 +01:00
|
|
|
log << log4 << "UGCont: indexes rebuilt, table.size: " << table_size << ", table_id.size: "
|
2008-12-21 22:17:09 +01:00
|
|
|
<< table_id.size() << ", table_name.size: " << table_name.size() << logend;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-03-09 03:36:25 +01:00
|
|
|
|
|
|
|
|
2008-12-21 22:17:09 +01:00
|
|
|
template<class Type>
|
2012-03-09 03:36:25 +01:00
|
|
|
bool UGContainer<Type>::Remove(long id)
|
2008-12-21 22:17:09 +01:00
|
|
|
{
|
2012-03-09 03:36:25 +01:00
|
|
|
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 )
|
|
|
|
{
|
2021-06-27 23:31:50 +02:00
|
|
|
|
|
|
|
log << log4 << "UGCont: removed index_name to: id: " << i->second->id << ", name: " << n->first << logend;
|
2012-03-09 03:36:25 +01:00
|
|
|
table_name.erase(n);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-27 23:31:50 +02:00
|
|
|
log << log4 << "UGCont: removed index_id to: id: " << i->second->id << logend;
|
|
|
|
log << log3 << "UGCont: removed: id: " << i->second->id << logend;
|
2012-03-09 03:36:25 +01:00
|
|
|
table.erase(i->second);
|
|
|
|
table_id.erase(i);
|
|
|
|
result = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
2008-12-21 22:17:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-02-12 17:30:49 +01:00
|
|
|
|
|
|
|
} // namespace Winix
|
|
|
|
|
|
|
|
|
2008-12-21 22:17:09 +01:00
|
|
|
#endif
|