/**************************************************************************/1/* inflection_map.h */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#pragma once3132#include "core/templates/hash_map.h"33#include "core/templates/local_vector.h"3435#include <iterator>3637/// An unordered map that splits elements between a fast-access vector of LinearCount consecutively38/// indexed elements, and a slower-access map holding sparse indexes larger than LinearCount.39///40/// \tparam KeyType is used to lookup values, and must be a type that is convertible to an unsigned integer.41/// \tparam ValueType must have an empty constructor (default or otherwise).42/// \tparam LinearCount43/// \tparam IndexType must be a type that is convertible to an unsigned integer (eg. uint8_t...uint64_t), and which is large enough to represent the number of values in this map.44template <typename KeyType, typename ValueType, size_t LinearCount, typename IndexType = uint16_t>45class InflectionMap {46public:47using value_type = ValueType;48class Iterator {49InflectionMap *map;50IndexType index;5152public:53using iterator_category = std::forward_iterator_tag;54using value_type = ValueType;55using pointer = value_type *;56using reference = value_type &;5758Iterator() :59map(nullptr), index(0) {}60Iterator(InflectionMap &p_m, const IndexType p_i) :61map(&p_m), index(p_i) {}6263Iterator &operator=(const Iterator &p_it) {64map = p_it.map;65index = p_it.index;66return *this;67}6869ValueType *operator->() { return &map->_values[index]; }70ValueType &operator*() { return map->_values[index]; }71operator ValueType *() { return &map->_values[index]; }7273bool operator==(const Iterator &p_it) const { return map == p_it.map && index == p_it.index; }74bool operator!=(const Iterator &p_it) const { return map != p_it.map || index != p_it.index; }7576Iterator &operator++() {77index++;78return *this;79}80Iterator operator++(int) {81Iterator t = *this;82index++;83return t;84}8586bool is_valid() const { return index < map->_values.size(); }87};8889const ValueType &operator[](const KeyType p_idx) const { return get_value(p_idx); }90ValueType &operator[](const KeyType p_idx) { return get_value(p_idx); }9192Iterator begin() { return Iterator(*this, 0); }93Iterator end() { return Iterator(*this, _values.size()); }9495bool is_empty() { return _values.is_empty(); }96size_t size() { return _values.size(); }97void reserve(size_t p_new_cap) { _values.reserve(p_new_cap); }9899protected:100static constexpr IndexType INVALID = std::numeric_limits<IndexType>::max();101typedef struct IndexValue {102IndexType value = INVALID;103} IndexValue;104105// Returns a reference to the value at the index.106// If the index has not been initialized, add an empty element at107// the end of the values array, and set the index to its position.108ValueType &get_value(KeyType p_idx) {109IndexValue *val_idx = p_idx < LinearCount ? &_linear_indexes[p_idx] : _inflection_indexes.getptr(p_idx);110if (val_idx == nullptr || val_idx->value == INVALID) {111_values.push_back({});112if (val_idx == nullptr) {113val_idx = &_inflection_indexes.insert(p_idx, {})->value;114}115val_idx->value = _values.size() - 1;116}117return _values[val_idx->value];118}119120TightLocalVector<ValueType> _values;121HashMap<KeyType, IndexValue> _inflection_indexes;122IndexValue _linear_indexes[LinearCount];123};124125126