packages/engine/scram/src/ext/index_map.h
Non-zero based Index->Value map adapter on sequential containers.
Namespaces
| Name |
|---|
| ext |
Classes
| Name | |
|---|---|
| class | ext::index_map <br>An adaptor map to shift zero-based containers to a different base. |
| class | ext::owned_index_map <br>An [index_map](Classes/classext_1_1index__map.md) variant that owns its storage and exposes convenience methods to. |
Source code
cpp
/*
* Copyright (C) 2014-2018 Olzhas Rakhimov
* Copyright (C) 2023 OpenPRA ORG Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <vector>
#include <algorithm>
#include <cassert>
namespace ext {
template <std::size_t BaseIndex, typename T,
template <typename...> class Sequence = std::vector>
class index_map : public Sequence<T> {
public:
using Sequence<T>::Sequence;
typename Sequence<T>::reference operator[](const std::size_t index) {
return Sequence<T>::operator[](index - BaseIndex);
}
typename Sequence<T>::const_reference operator[](const std::size_t index) const {
return Sequence<T>::operator[](index - BaseIndex);
}
};
// -----------------------------------------------------------------------------
// Owned Index Map – utility wrapper around `index_map` that provides explicit
// lifecycle helpers for bulk (re)initialization and logical clearing without
// affecting the underlying memory allocation.
// -----------------------------------------------------------------------------
template <std::size_t BaseIndex, typename T,
template <typename...> class Sequence = std::vector>
class owned_index_map : public index_map<BaseIndex, T, Sequence> {
public:
using base_type = index_map<BaseIndex, T, Sequence>;
using base_type::base_type; // Inherit constructors from `index_map`.
void init(std::size_t n, const T &value = T()) {
this->assign(n, value);
}
void init_no_alloc(const T &value = T()) {
assert(!this->empty() && "owned_index_map::init_no_alloc called on empty container — use init(n, value) instead.");
std::fill(this->begin(), this->end(), value);
}
void clear_values() {
std::fill(this->begin(), this->end(), T{});
}
void reset_storage() {
base_type tmp; // RVO-friendly swap trick to deallocate in O(1).
this->swap(tmp);
}
};
} // namespace extUpdated on 2026-01-09 at 21:59:12 +0000
