490 lines
9.4 KiB
C++
490 lines
9.4 KiB
C++
// Copyright Onix Solutions Limited [OnixS]. All rights reserved.
|
|
//
|
|
// This software owned by Onix Solutions Limited [OnixS] and is
|
|
// protected by copyright law and international copyright treaties.
|
|
//
|
|
// Access to and use of the software is governed by the terms of the applicable
|
|
// OnixS Software Services Agreement (the Agreement) and Customer end user license
|
|
// agreements granting a non-assignable, non-transferable and non-exclusive license
|
|
// to use the software for it's own data processing purposes under the terms defined
|
|
// in the Agreement.
|
|
//
|
|
// Except as otherwise granted within the terms of the Agreement, copying or
|
|
// reproduction of any part of this source code or associated reference material
|
|
// to any other location for further reproduction or redistribution, and any
|
|
// amendments to this copyright notice, are expressly prohibited.
|
|
//
|
|
// Any reproduction or redistribution for sale or hiring of the Software not in
|
|
// accordance with the terms of the Agreement is a violation of copyright law.
|
|
//
|
|
|
|
#pragma once
|
|
|
|
#include <vector>
|
|
#include <iterator>
|
|
#include <algorithm>
|
|
|
|
#include <OnixS/CME/MDH/String.h>
|
|
|
|
ONIXS_CMEMDH_NAMESPACE_BEGIN
|
|
|
|
/// Class implementing set optimized
|
|
/// for storing small number of items.
|
|
template
|
|
<
|
|
class Key
|
|
>
|
|
class TinySet
|
|
{
|
|
public:
|
|
/// Aliases set item type.
|
|
typedef Key Item;
|
|
|
|
/// Aliases internal representation.
|
|
typedef
|
|
std::vector<Item>
|
|
Items;
|
|
|
|
/// Aliases iterator over set items.
|
|
typedef
|
|
typename
|
|
Items::const_iterator
|
|
ConstIterator;
|
|
|
|
/// Initializes empty set.
|
|
TinySet()
|
|
{
|
|
}
|
|
|
|
/// Initializes empty set and configures
|
|
/// internal storage to be enough capacious
|
|
/// to store given number of items.
|
|
explicit
|
|
TinySet(
|
|
size_t capacity)
|
|
{
|
|
reserve(capacity);
|
|
}
|
|
|
|
/// Initializes set as copy of other one.
|
|
TinySet(
|
|
const TinySet& other)
|
|
: items_(other.items_)
|
|
{
|
|
}
|
|
|
|
/// Cleans everything up.
|
|
~TinySet()
|
|
{
|
|
}
|
|
|
|
/// Indicates whether set is empty.
|
|
bool empty() const
|
|
{
|
|
return items_.empty();
|
|
}
|
|
|
|
/// Number of items in the set.
|
|
size_t size() const
|
|
{
|
|
return items_.size();
|
|
}
|
|
|
|
/// Makes internal storage capacious
|
|
/// enough to store given number of items.
|
|
void
|
|
reserve(
|
|
size_t capacity)
|
|
{
|
|
items_.reserve(capacity);
|
|
}
|
|
|
|
/// Provides iterating facilities.
|
|
ConstIterator
|
|
begin() const
|
|
{
|
|
return items_.begin();
|
|
}
|
|
|
|
/// Provides iterating facilities.
|
|
ConstIterator
|
|
end() const
|
|
{
|
|
return items_.end();
|
|
}
|
|
|
|
/// Tells whether set contains given item.
|
|
ConstIterator
|
|
find(
|
|
const Item& item) const
|
|
{
|
|
return
|
|
std::find(
|
|
items_.begin(),
|
|
items_.end(),
|
|
item);
|
|
}
|
|
|
|
/// Inserts items into the set.
|
|
/// Returned value indicates whether item
|
|
/// was inserted (true) or set already
|
|
/// contained given item (false).
|
|
bool
|
|
insert(
|
|
const Item& item)
|
|
{
|
|
if (end() == find(item))
|
|
{
|
|
items_.push_back(item);
|
|
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
/// Removes given item from the set.
|
|
/// Returned value indicates whether
|
|
/// item was actually removed.
|
|
bool
|
|
erase(
|
|
const Item& item)
|
|
{
|
|
typename
|
|
Items::iterator
|
|
const
|
|
found =
|
|
std::find(
|
|
items_.begin(),
|
|
items_.end(),
|
|
item);
|
|
|
|
if (items_.end() == found)
|
|
return false;
|
|
|
|
items_.erase(found);
|
|
|
|
return true;
|
|
}
|
|
|
|
/// Brings set to blank state.
|
|
void
|
|
clear()
|
|
{
|
|
items_.clear();
|
|
}
|
|
|
|
/// Swaps content with the other instance.
|
|
void
|
|
swap(
|
|
TinySet& other)
|
|
{
|
|
std::swap(
|
|
items_,
|
|
other.items_);
|
|
}
|
|
|
|
/// Re-initializes instance as copy of other instance.
|
|
TinySet&
|
|
operator =(
|
|
const TinySet& other)
|
|
{
|
|
if (&other != this)
|
|
{
|
|
TinySet copy(other);
|
|
|
|
swap(copy);
|
|
}
|
|
|
|
return *this;
|
|
}
|
|
|
|
private:
|
|
Items items_;
|
|
};
|
|
|
|
/// Implements iterator for TinyStrSet class.
|
|
ONIXS_CMEMDH_LTWT_CLASS
|
|
TinyStrSetIterator
|
|
{
|
|
public:
|
|
/// Aliases internal representation.
|
|
typedef
|
|
std::vector<std::string>
|
|
Items;
|
|
|
|
/// Aliases iterator over internal store.
|
|
typedef
|
|
Items::const_iterator
|
|
ItemIterator;
|
|
|
|
/// Initializes iterator pointing to nowhere.
|
|
TinyStrSetIterator()
|
|
{
|
|
}
|
|
|
|
/// Initializes instance as copy of the other one.
|
|
TinyStrSetIterator(
|
|
const TinyStrSetIterator& other)
|
|
: iterator_(other.iterator_)
|
|
{
|
|
}
|
|
|
|
/// Initializes from iterator over internal store.
|
|
explicit
|
|
TinyStrSetIterator(
|
|
const ItemIterator& iterator)
|
|
: iterator_(iterator)
|
|
{
|
|
}
|
|
|
|
/// Cleans everything up.
|
|
~TinyStrSetIterator()
|
|
{
|
|
}
|
|
|
|
/// Provides access to underlying object.
|
|
StrRef
|
|
operator *() const
|
|
{
|
|
return toStrRef(*iterator_);
|
|
}
|
|
|
|
/// Compares with the other instance.
|
|
bool
|
|
operator ==(
|
|
const
|
|
TinyStrSetIterator& other) const
|
|
{
|
|
return iterator_ == other.iterator_;
|
|
}
|
|
|
|
/// Compares with the other instance.
|
|
bool
|
|
operator !=(
|
|
const
|
|
TinyStrSetIterator& other) const
|
|
{
|
|
return iterator_ != other.iterator_;
|
|
}
|
|
|
|
/// Re-initializes as copy of the other instance.
|
|
TinyStrSetIterator&
|
|
operator =(
|
|
const
|
|
TinyStrSetIterator& other)
|
|
{
|
|
iterator_ = other.iterator_;
|
|
|
|
return *this;
|
|
}
|
|
|
|
/// Advances instance to the next item.
|
|
TinyStrSetIterator&
|
|
operator ++()
|
|
{
|
|
++iterator_;
|
|
|
|
return *this;
|
|
}
|
|
|
|
private:
|
|
ItemIterator iterator_;
|
|
};
|
|
|
|
/// Implements TinySet for StrRef class.
|
|
///
|
|
/// In contrast to TinySet<StrRef> instantiation,
|
|
/// given class allocates space for content being
|
|
/// referenced by StrRef instances inserted into
|
|
/// given set.
|
|
ONIXS_CMEMDH_LTWT_CLASS
|
|
TinyStrSet
|
|
{
|
|
public:
|
|
/// Items of collection.
|
|
typedef StrRef Item;
|
|
|
|
/// Aliases iterator type.
|
|
typedef
|
|
TinyStrSetIterator
|
|
ConstIterator;
|
|
|
|
/// Initializes empty set.
|
|
TinyStrSet()
|
|
{
|
|
}
|
|
|
|
/// Initializes empty set capacious
|
|
/// enough to store given number of items.
|
|
explicit
|
|
TinyStrSet(
|
|
size_t capacity)
|
|
{
|
|
reserve(capacity);
|
|
}
|
|
|
|
/// Initializes as copy of other instance.
|
|
TinyStrSet(
|
|
const TinyStrSet& other)
|
|
: items_(other.items_)
|
|
{
|
|
}
|
|
|
|
/// Performs clean-up.
|
|
~TinyStrSet()
|
|
{
|
|
}
|
|
|
|
/// Indicates whether set is empty.
|
|
bool empty() const
|
|
{
|
|
return items_.empty();
|
|
}
|
|
|
|
/// Returns number of items in the set.
|
|
size_t size() const
|
|
{
|
|
return items_.size();
|
|
}
|
|
|
|
/// Makes internal storage capacious
|
|
/// enough to store given number of items.
|
|
void
|
|
reserve(
|
|
size_t capacity)
|
|
{
|
|
items_.reserve(capacity);
|
|
}
|
|
|
|
/// Provides iterating facilities.
|
|
ConstIterator
|
|
begin() const
|
|
{
|
|
return
|
|
ConstIterator(
|
|
items_.begin());
|
|
}
|
|
|
|
/// Provides iterating facilities.
|
|
ConstIterator
|
|
end() const
|
|
{
|
|
return
|
|
ConstIterator(
|
|
items_.end());
|
|
}
|
|
|
|
/// Tells whether set contains given item.
|
|
ConstIterator
|
|
find(
|
|
const Item& item) const
|
|
{
|
|
return
|
|
ConstIterator(
|
|
std::find_if(
|
|
items_.begin(),
|
|
items_.end(),
|
|
ItemLocator(item)));
|
|
}
|
|
|
|
/// Inserts item into set.
|
|
///
|
|
/// Content referenced by StrRef instance
|
|
/// is copied into internal storage.
|
|
///
|
|
/// Returned value indicates whether
|
|
/// item was actually inserted.
|
|
bool
|
|
insert(
|
|
const Item& item)
|
|
{
|
|
if (end() == find(item))
|
|
{
|
|
items_.push_back(toStr(item));
|
|
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
/// Removes given item from the set.
|
|
/// Returned value indicates whether
|
|
/// item was actually erased.
|
|
bool
|
|
erase(
|
|
const Item& item)
|
|
{
|
|
ItemIterator
|
|
const found =
|
|
std::find_if(
|
|
items_.begin(),
|
|
items_.end(),
|
|
ItemLocator(item));
|
|
|
|
if (items_.end() == found)
|
|
return false;
|
|
|
|
items_.erase(found);
|
|
|
|
return true;
|
|
}
|
|
|
|
/// Brings set to blank/empty state.
|
|
void
|
|
clear()
|
|
{
|
|
items_.clear();
|
|
}
|
|
|
|
/// Exchanges content with the other instance.
|
|
void
|
|
swap(
|
|
TinyStrSet& other)
|
|
{
|
|
std::swap
|
|
(
|
|
items_,
|
|
other.items_
|
|
);
|
|
}
|
|
|
|
private:
|
|
typedef
|
|
std::vector<std::string>
|
|
Items;
|
|
|
|
typedef
|
|
Items::const_iterator
|
|
ItemConstIterator;
|
|
|
|
typedef
|
|
Items::iterator
|
|
ItemIterator;
|
|
|
|
class ItemLocator
|
|
{
|
|
StrRef sought_;
|
|
|
|
public:
|
|
ItemLocator(
|
|
const StrRef& sought)
|
|
: sought_(sought)
|
|
{
|
|
}
|
|
|
|
bool
|
|
operator ()(
|
|
const
|
|
std::string& other) const
|
|
{
|
|
return sought_ == other;
|
|
}
|
|
};
|
|
|
|
Items items_;
|
|
};
|
|
|
|
ONIXS_CMEMDH_NAMESPACE_END
|