234 lines
4.3 KiB
C++
234 lines
4.3 KiB
C++
// License: Modified MIT (NON-AI)
|
|
// See the LICENSE file in the root directory for license information.
|
|
// Copyright 2025 Timothy Prepscius
|
|
|
|
#pragma once
|
|
|
|
namespace tjp {
|
|
namespace core {
|
|
namespace string_dictionary {
|
|
|
|
// ----
|
|
|
|
inline
|
|
Entry::Entry(const StrongPtr<Dictionary> &container_, std::string &&value_) :
|
|
container(container_),
|
|
value(std::move(value_))
|
|
{
|
|
}
|
|
|
|
inline
|
|
Entry::~Entry ()
|
|
{
|
|
if (container)
|
|
container->remove(this);
|
|
}
|
|
|
|
// ----
|
|
|
|
inline
|
|
Reference::operator const std::string &() const
|
|
{
|
|
return entry->value;
|
|
}
|
|
|
|
inline
|
|
Reference::operator const std::string_view () const
|
|
{
|
|
return entry->value;
|
|
}
|
|
|
|
inline
|
|
bool operator ==(const Reference &lhs, const Reference &rhs)
|
|
{
|
|
return (const std::string &)lhs == (const std::string &)rhs;
|
|
}
|
|
|
|
inline
|
|
bool operator ==(const Reference &lhs, const std::string &r)
|
|
{
|
|
return (const std::string &)lhs == r;
|
|
}
|
|
|
|
inline
|
|
bool operator ==(const Reference &lhs, const std::string_view &r)
|
|
{
|
|
return (const std::string &)lhs == r;
|
|
}
|
|
|
|
inline
|
|
bool operator ==(const Reference &lhs, const char *r)
|
|
{
|
|
return (const std::string &)lhs == r;
|
|
}
|
|
|
|
inline
|
|
bool operator ==(const std::string &l, const Reference &rhs)
|
|
{
|
|
return l == (const std::string &)rhs;
|
|
}
|
|
|
|
inline
|
|
bool operator ==(const std::string_view &l, const Reference &rhs)
|
|
{
|
|
return l == (const std::string &)rhs;
|
|
}
|
|
|
|
inline
|
|
bool operator ==(const char *l, const Reference &rhs)
|
|
{
|
|
return l == (const std::string &)rhs;
|
|
}
|
|
|
|
|
|
|
|
inline
|
|
bool operator !=(const Reference &lhs, const Reference &rhs)
|
|
{
|
|
return (const std::string &)lhs != (const std::string &)rhs;
|
|
}
|
|
|
|
inline
|
|
bool operator !=(const Reference &lhs, const std::string &r)
|
|
{
|
|
return (const std::string &)lhs != r;
|
|
}
|
|
|
|
inline
|
|
bool operator !=(const Reference &lhs, const std::string_view &r)
|
|
{
|
|
return (const std::string &)lhs != r;
|
|
}
|
|
|
|
inline
|
|
bool operator !=(const Reference &lhs, const char *r)
|
|
{
|
|
return (const std::string &)lhs != r;
|
|
}
|
|
|
|
inline
|
|
bool operator !=(const std::string &l, const Reference &rhs)
|
|
{
|
|
return l != (const std::string &)rhs;
|
|
}
|
|
|
|
inline
|
|
bool operator !=(const std::string_view &l, const Reference &rhs)
|
|
{
|
|
return l != (const std::string &)rhs;
|
|
}
|
|
|
|
inline
|
|
bool operator !=(const char *l, const Reference &rhs)
|
|
{
|
|
return l != (const std::string &)rhs;
|
|
}
|
|
|
|
|
|
|
|
inline
|
|
bool operator <(const Reference &lhs, const Reference &rhs)
|
|
{
|
|
return (const std::string &)lhs < (const std::string &)rhs;
|
|
}
|
|
|
|
inline
|
|
bool operator <(const Reference &lhs, const std::string &r)
|
|
{
|
|
return (const std::string &)lhs < r;
|
|
}
|
|
|
|
inline
|
|
bool operator <(const Reference &lhs, const std::string_view &r)
|
|
{
|
|
return (const std::string &)lhs < r;
|
|
}
|
|
|
|
inline
|
|
bool operator <(const Reference &lhs, const char *r)
|
|
{
|
|
return (const std::string &)lhs < r;
|
|
}
|
|
|
|
inline
|
|
bool operator <(const std::string &l, const Reference &rhs)
|
|
{
|
|
return l < (const std::string &)rhs;
|
|
}
|
|
|
|
inline
|
|
bool operator <(const std::string_view &l, const Reference &rhs)
|
|
{
|
|
return l < (const std::string &)rhs;
|
|
}
|
|
|
|
inline
|
|
bool operator <(const char *l, const Reference &rhs)
|
|
{
|
|
return l < (const std::string &)rhs;
|
|
}
|
|
|
|
|
|
// ----
|
|
|
|
inline
|
|
void Dictionary::remove(Entry *entry)
|
|
{
|
|
auto i = entries.find(entry->value);
|
|
if (i != entries.end())
|
|
entries.erase(i);
|
|
}
|
|
|
|
inline
|
|
Reference Dictionary::get(const std::string_view &value)
|
|
{
|
|
auto i = entries.find(value);
|
|
if (i != entries.end())
|
|
return { strong(i->second) };
|
|
|
|
auto entry = strong<Entry>(Entry { strong_this(this), std::string(value) });
|
|
entries.emplace(entry->value, entry);
|
|
return { std::move(entry) };
|
|
}
|
|
|
|
inline
|
|
Reference Dictionary::get(const std::string &value)
|
|
{
|
|
auto i = entries.find(value);
|
|
if (i != entries.end())
|
|
return { strong(i->second) };
|
|
|
|
auto entry = strong<Entry>(strong_this(this), std::string(value));
|
|
entries.emplace(entry->value, entry);
|
|
return { std::move(entry) };
|
|
}
|
|
|
|
inline
|
|
Reference Dictionary::get(std::string &&value)
|
|
{
|
|
auto i = entries.find(value);
|
|
if (i != entries.end())
|
|
return { strong(i->second) };
|
|
|
|
auto entry = strong<Entry>(strong_this(this), std::move(value));
|
|
entries.emplace(entry->value, entry);
|
|
return { std::move(entry) };
|
|
}
|
|
|
|
inline
|
|
Reference Dictionary::get(const char *value)
|
|
{
|
|
auto i = entries.find(value);
|
|
if (i != entries.end())
|
|
return { strong(i->second) };
|
|
|
|
auto entry = strong<Entry>(strong_this(this), std::string(value));
|
|
entries.emplace(entry->value, entry);
|
|
return { std::move(entry) };
|
|
}
|
|
|
|
} // namespace
|
|
} // namespace
|
|
} // namespace
|
|
|