| 1 |
// |
| 2 |
// SPDX-License-Identifier: BSD-3-Clause |
| 3 |
// Copyright Contributors to the OpenEXR Project. |
| 4 |
// |
| 5 |
|
| 6 |
// clang-format off |
| 7 |
|
| 8 |
#include <limits> |
| 9 |
#include <stdexcept> |
| 10 |
#include "PyImathExport.h" |
| 11 |
#include "PyImathStringTable.h" |
| 12 |
|
| 13 |
namespace PyImath { |
| 14 |
|
| 15 |
template<class T> |
| 16 |
StringTableIndex |
| 17 |
StringTableT<T>::lookup(const T &s) const |
| 18 |
{ |
| 19 |
typedef typename Table::template nth_index<1>::type StringSet; |
| 20 |
const StringSet &strings = _table.template get<1>(); |
| 21 |
|
| 22 |
typename StringSet::const_iterator it = strings.find(s); |
| 23 |
if (it == strings.end()) { |
| 24 |
throw std::domain_error ("String table access out of bounds"); |
| 25 |
} |
| 26 |
|
| 27 |
return it->i; |
| 28 |
} |
| 29 |
|
| 30 |
template<class T> |
| 31 |
const T & |
| 32 |
StringTableT<T>::lookup(StringTableIndex index) const |
| 33 |
{ |
| 34 |
typedef typename Table::template nth_index<0>::type IndexSet; |
| 35 |
const IndexSet &indices = _table.template get<0>(); |
| 36 |
|
| 37 |
typename IndexSet::const_iterator it = indices.find(index); |
| 38 |
if (it == indices.end()) { |
| 39 |
throw std::domain_error ("String table access out of bounds"); |
| 40 |
} |
| 41 |
|
| 42 |
return it->s; |
| 43 |
} |
| 44 |
|
| 45 |
template<class T> |
| 46 |
StringTableIndex |
| 47 |
StringTableT<T>::intern(const T &s) |
| 48 |
{ |
| 49 |
typedef typename Table::template nth_index<1>::type StringSet; |
| 50 |
const StringSet &strings = _table.template get<1>(); |
| 51 |
|
| 52 |
typename StringSet::const_iterator it = strings.find(s); |
| 53 |
if (it == strings.end()) { |
| 54 |
size_t next_index = _table.size(); |
| 55 |
if (next_index > std::numeric_limits<StringTableIndex::index_type>::max()) { |
| 56 |
throw std::domain_error ("Unable to intern string - string table would exceed maximum size"); |
| 57 |
} |
| 58 |
StringTableIndex index = StringTableIndex(StringTableIndex::index_type(next_index)); |
| 59 |
_table.insert(StringTableEntry<T>(index,s)); |
| 60 |
return index; |
| 61 |
} |
| 62 |
|
| 63 |
return it->i; |
| 64 |
} |
| 65 |
|
| 66 |
template<class T> |
| 67 |
size_t |
| 68 |
StringTableT<T>::size() const |
| 69 |
{ |
| 70 |
return _table.size(); |
| 71 |
} |
| 72 |
|
| 73 |
template<class T> |
| 74 |
bool |
| 75 |
StringTableT<T>::hasString(const T &s) const |
| 76 |
{ |
| 77 |
typedef typename Table::template nth_index<1>::type StringSet; |
| 78 |
const StringSet &strings = _table.template get<1>(); |
| 79 |
return strings.find(s) != strings.end(); |
| 80 |
} |
| 81 |
|
| 82 |
template<class T> |
| 83 |
bool |
| 84 |
StringTableT<T>::hasStringIndex(const StringTableIndex &s) const |
| 85 |
{ |
| 86 |
typedef typename Table::template nth_index<0>::type IndexSet; |
| 87 |
const IndexSet &indices = _table.template get<0>(); |
| 88 |
return indices.find(s) != indices.end(); |
| 89 |
} |
| 90 |
|
| 91 |
namespace { |
| 92 |
template class StringTableDetailT<std::string>; |
| 93 |
template class StringTableDetailT<std::wstring>; |
| 94 |
} |
| 95 |
|
| 96 |
template class PYIMATH_EXPORT StringTableT<std::string>; |
| 97 |
template class PYIMATH_EXPORT StringTableT<std::wstring>; |
| 98 |
|
| 99 |
} // namespace PyImath |