2012-11-15 07:16:31 +01:00
|
|
|
//
|
|
|
|
// ListMap.h
|
|
|
|
//
|
|
|
|
// Library: Foundation
|
2020-01-09 10:08:09 +01:00
|
|
|
// Package: Core
|
2012-11-15 07:16:31 +01:00
|
|
|
// Module: ListMap
|
|
|
|
//
|
|
|
|
// Definition of the ListMap class.
|
|
|
|
//
|
|
|
|
// Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
|
|
|
|
// and Contributors.
|
|
|
|
//
|
2014-05-04 21:02:42 +02:00
|
|
|
// SPDX-License-Identifier: BSL-1.0
|
2012-11-15 07:16:31 +01:00
|
|
|
//
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef Foundation_ListMap_INCLUDED
|
|
|
|
#define Foundation_ListMap_INCLUDED
|
|
|
|
|
|
|
|
|
|
|
|
#include "Poco/Foundation.h"
|
2013-03-10 05:31:49 +01:00
|
|
|
#include "Poco/String.h"
|
2012-11-15 07:16:31 +01:00
|
|
|
#include "Poco/Exception.h"
|
2020-01-09 10:08:09 +01:00
|
|
|
#include <vector>
|
2012-11-15 07:16:31 +01:00
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
|
|
|
|
namespace Poco {
|
|
|
|
|
|
|
|
|
2020-01-09 10:08:09 +01:00
|
|
|
template <class Key, class Mapped, class Container = std::vector<std::pair<Key, Mapped>>, bool CaseSensitive = false>
|
2012-11-15 07:16:31 +01:00
|
|
|
class ListMap
|
2013-03-10 05:31:49 +01:00
|
|
|
/// This class implements a multimap in terms of a sequential container.
|
2012-11-15 07:16:31 +01:00
|
|
|
/// The use for this type of associative container is wherever automatic
|
|
|
|
/// ordering of elements is not desirable. Naturally, this container will
|
2013-03-10 05:31:49 +01:00
|
|
|
/// have inferior data retrieval performance and it is not recommended for
|
2012-11-15 07:16:31 +01:00
|
|
|
/// use with large datasets. The main purpose within POCO is for Internet
|
2016-04-21 09:36:53 +02:00
|
|
|
/// messages (email message, http headers etc), to prevent automatic
|
2013-03-10 05:31:49 +01:00
|
|
|
/// header entry reordering.
|
2012-11-15 07:16:31 +01:00
|
|
|
{
|
|
|
|
public:
|
2020-01-09 10:08:09 +01:00
|
|
|
using KeyType = Key;
|
|
|
|
using MappedType = Mapped;
|
|
|
|
using Reference = Mapped&;
|
|
|
|
using ConstReference = const Mapped&;
|
|
|
|
using Pointer = Mapped*;
|
|
|
|
using ConstPointer = const Mapped*;
|
2012-11-15 07:16:31 +01:00
|
|
|
|
2020-01-09 10:08:09 +01:00
|
|
|
using ValueType = typename Container::value_type;
|
|
|
|
using SizeType = typename Container::size_type;
|
|
|
|
using Iterator = typename Container::iterator;
|
|
|
|
using ConstIterator = typename Container::const_iterator;
|
2012-11-15 07:16:31 +01:00
|
|
|
|
|
|
|
ListMap()
|
|
|
|
/// Creates an empty ListMap.
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-01-09 10:08:09 +01:00
|
|
|
explicit ListMap(std::size_t initialReserve):
|
|
|
|
_container(initialReserve)
|
2012-11-15 07:16:31 +01:00
|
|
|
/// Creates the ListMap with room for initialReserve entries.
|
|
|
|
{
|
|
|
|
}
|
2020-01-09 10:08:09 +01:00
|
|
|
|
|
|
|
ListMap(const ListMap& other):
|
|
|
|
_container(other._container)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ListMap(ListMap&& other) noexcept:
|
|
|
|
_container(std::move(other._container))
|
|
|
|
{
|
|
|
|
}
|
2012-11-15 07:16:31 +01:00
|
|
|
|
|
|
|
ListMap& operator = (const ListMap& map)
|
|
|
|
/// Assigns another ListMap.
|
|
|
|
{
|
|
|
|
ListMap tmp(map);
|
|
|
|
swap(tmp);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2020-01-09 10:08:09 +01:00
|
|
|
ListMap& operator = (ListMap&& map) noexcept
|
|
|
|
/// Assigns another ListMap.
|
|
|
|
{
|
|
|
|
_container = std::move(map._container);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2012-11-15 07:16:31 +01:00
|
|
|
void swap(ListMap& map)
|
|
|
|
/// Swaps the ListMap with another one.
|
|
|
|
{
|
2020-01-09 10:08:09 +01:00
|
|
|
_container.swap(map._container);
|
2012-11-15 07:16:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ConstIterator begin() const
|
2013-03-10 05:31:49 +01:00
|
|
|
/// Returns the beginning of the map.
|
2012-11-15 07:16:31 +01:00
|
|
|
{
|
2020-01-09 10:08:09 +01:00
|
|
|
return _container.begin();
|
2012-11-15 07:16:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ConstIterator end() const
|
2013-03-10 05:31:49 +01:00
|
|
|
/// Returns the end of the map.
|
2012-11-15 07:16:31 +01:00
|
|
|
{
|
2020-01-09 10:08:09 +01:00
|
|
|
return _container.end();
|
2012-11-15 07:16:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Iterator begin()
|
2013-03-10 05:31:49 +01:00
|
|
|
/// Returns the beginning of the map.
|
2012-11-15 07:16:31 +01:00
|
|
|
{
|
2020-01-09 10:08:09 +01:00
|
|
|
return _container.begin();
|
2012-11-15 07:16:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Iterator end()
|
2013-03-10 05:31:49 +01:00
|
|
|
/// Returns the end of the map.
|
2012-11-15 07:16:31 +01:00
|
|
|
{
|
2020-01-09 10:08:09 +01:00
|
|
|
return _container.end();
|
2012-11-15 07:16:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ConstIterator find(const KeyType& key) const
|
2016-04-21 09:36:53 +02:00
|
|
|
/// Finds the first occurrence of the key and
|
2013-03-10 05:31:49 +01:00
|
|
|
/// returns iterator pointing to the found entry
|
|
|
|
/// or iterator pointing to the end if entry is
|
|
|
|
/// not found.
|
2012-11-15 07:16:31 +01:00
|
|
|
{
|
2020-01-09 10:08:09 +01:00
|
|
|
typename Container::const_iterator it = _container.begin();
|
|
|
|
typename Container::const_iterator itEnd = _container.end();
|
2016-04-21 09:36:53 +02:00
|
|
|
for(; it != itEnd; ++it)
|
2012-11-15 07:16:31 +01:00
|
|
|
{
|
|
|
|
if (isEqual(it->first, key)) return it;
|
|
|
|
}
|
2016-04-21 09:36:53 +02:00
|
|
|
return itEnd;
|
2012-11-15 07:16:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Iterator find(const KeyType& key)
|
2016-04-21 09:36:53 +02:00
|
|
|
/// Finds the first occurrence of the key and
|
2013-03-10 05:31:49 +01:00
|
|
|
/// returns iterator pointing to the found entry
|
|
|
|
/// or iterator pointing to the end if entry is
|
|
|
|
/// not found.
|
2012-11-15 07:16:31 +01:00
|
|
|
{
|
2020-01-09 10:08:09 +01:00
|
|
|
typename Container::iterator it = _container.begin();
|
|
|
|
typename Container::iterator itEnd = _container.end();
|
2016-04-21 09:36:53 +02:00
|
|
|
for(; it != itEnd; ++it)
|
2012-11-15 07:16:31 +01:00
|
|
|
{
|
|
|
|
if (isEqual(it->first, key)) return it;
|
|
|
|
}
|
2016-04-21 09:36:53 +02:00
|
|
|
return itEnd;
|
2012-11-15 07:16:31 +01:00
|
|
|
}
|
|
|
|
|
2013-03-10 05:31:49 +01:00
|
|
|
Iterator insert(const ValueType& val)
|
|
|
|
/// Inserts the value into the map. If one or more values
|
|
|
|
/// already exist, new value is inserted at the end of the
|
|
|
|
/// block. Thus, all the equal value entries are located
|
|
|
|
/// sequentially at all times.
|
|
|
|
/// Returns iterator pointing to the newly inserted value
|
2012-11-15 07:16:31 +01:00
|
|
|
{
|
2013-03-10 05:31:49 +01:00
|
|
|
Iterator it = find(val.first);
|
2020-01-09 10:08:09 +01:00
|
|
|
while (it != _container.end() && isEqual(it->first, val.first)) ++it;
|
|
|
|
return _container.insert(it, val);
|
2012-11-15 07:16:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void erase(Iterator it)
|
|
|
|
{
|
2020-01-09 10:08:09 +01:00
|
|
|
_container.erase(it);
|
2012-11-15 07:16:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
SizeType erase(const KeyType& key)
|
|
|
|
{
|
|
|
|
SizeType count = 0;
|
|
|
|
Iterator it = find(key);
|
2013-03-10 05:31:49 +01:00
|
|
|
bool removed = false;
|
2020-01-09 10:08:09 +01:00
|
|
|
while (it != _container.end())
|
2012-11-15 07:16:31 +01:00
|
|
|
{
|
2013-03-10 05:31:49 +01:00
|
|
|
if (isEqual(it->first, key))
|
|
|
|
{
|
|
|
|
++count;
|
2020-01-09 10:08:09 +01:00
|
|
|
it = _container.erase(it);
|
2013-03-10 05:31:49 +01:00
|
|
|
removed = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (removed) return count;
|
|
|
|
++it;
|
|
|
|
}
|
2012-11-15 07:16:31 +01:00
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
void clear()
|
|
|
|
{
|
2020-01-09 10:08:09 +01:00
|
|
|
_container.clear();
|
2012-11-15 07:16:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
std::size_t size() const
|
|
|
|
{
|
2020-01-09 10:08:09 +01:00
|
|
|
return _container.size();
|
2012-11-15 07:16:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool empty() const
|
|
|
|
{
|
2020-01-09 10:08:09 +01:00
|
|
|
return _container.empty();
|
2012-11-15 07:16:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ConstReference operator [] (const KeyType& key) const
|
|
|
|
{
|
|
|
|
ConstIterator it = find(key);
|
2020-01-09 10:08:09 +01:00
|
|
|
if (it != _container.end())
|
2012-11-15 07:16:31 +01:00
|
|
|
return it->second;
|
|
|
|
else
|
|
|
|
throw NotFoundException();
|
|
|
|
}
|
|
|
|
|
|
|
|
Reference operator [] (const KeyType& key)
|
|
|
|
{
|
|
|
|
Iterator it = find(key);
|
2020-01-09 10:08:09 +01:00
|
|
|
if (it != _container.end())
|
|
|
|
{
|
2012-11-15 07:16:31 +01:00
|
|
|
return it->second;
|
2020-01-09 10:08:09 +01:00
|
|
|
}
|
2012-11-15 07:16:31 +01:00
|
|
|
else
|
|
|
|
{
|
2013-03-10 05:31:49 +01:00
|
|
|
ValueType value(key, Mapped());
|
2016-04-21 09:36:53 +02:00
|
|
|
Iterator itInsert = insert(value);
|
|
|
|
return itInsert->second;
|
2012-11-15 07:16:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
template <typename T1, typename T2>
|
|
|
|
bool isEqual(T1 val1, T2 val2) const
|
|
|
|
{
|
|
|
|
return val1 == val2;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isEqual(const std::string& s1, const std::string& s2) const
|
|
|
|
{
|
2013-03-10 05:31:49 +01:00
|
|
|
if (!CaseSensitive)
|
|
|
|
return Poco::icompare(s1, s2) == 0;
|
|
|
|
else
|
|
|
|
return s1 == s2;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isEqual(const std::string& s1, const char* s2) const
|
|
|
|
{
|
|
|
|
return isEqual(s1, std::string(s2));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isEqual(const char* s1, const std::string& s2) const
|
|
|
|
{
|
|
|
|
return isEqual(std::string(s1), s2);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isEqual(const char* s1, const char* s2) const
|
|
|
|
{
|
|
|
|
return isEqual(std::string(s1), std::string(s2));
|
2012-11-15 07:16:31 +01:00
|
|
|
}
|
|
|
|
|
2020-01-09 10:08:09 +01:00
|
|
|
Container _container;
|
2012-11-15 07:16:31 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace Poco
|
|
|
|
|
|
|
|
|
|
|
|
#endif // Foundation_ListMap_INCLUDED
|