Skip to content

packages/engine/scram-node/src/ext/linear_map.h

Implementation of a vector-based map for a small number of entries.

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 <cassert>

#include <algorithm>
#include <initializer_list>
#include <stdexcept>
#include <utility>
#include <vector>

namespace ext {

struct DefaultEraser {
  template <class T, class Iterator>
  static typename T::iterator erase(Iterator it, T* container) {
    return container->erase(it);
  }
};

struct MoveEraser {
  template <class T>
  static typename T::iterator erase(typename T::iterator it, T* container) {
    if (it != std::prev(container->end())) {  // Prevent move into itself.
      *it = std::move(container->back());
    }
    container->pop_back();
    return it;
  }
  template <class T>
  static typename T::iterator erase(typename T::const_iterator it,
                                    T* container) {
    return erase(
        std::next(container->begin(), std::distance(container->cbegin(), it)),
        container);
  }
};

template <typename Key, typename Value, class ErasePolicy = DefaultEraser,
          template <typename...> class Sequence = std::vector>
class linear_map {
  friend bool operator==(const linear_map& lhs, const linear_map& rhs) {
    if (lhs.size() != rhs.size())
      return false;
    for (const auto& entry : lhs) {
      if (std::find(rhs.begin(), rhs.end(), entry) == rhs.end())
        return false;
    }
    return true;
  }
  friend bool operator!=(const linear_map& lhs, const linear_map& rhs) {
    return !(lhs == rhs);
  }

  friend void swap(linear_map& lhs, linear_map& rhs) noexcept { lhs.swap(rhs); }

 public:
  using key_type = Key;
  using mapped_type = Value;
  using value_type = std::pair<key_type, mapped_type>;
  using container_type = Sequence<value_type>;

  using pointer = typename container_type::pointer;
  using const_pointer = typename container_type::const_pointer;
  using reference = typename container_type::reference;
  using const_reference = typename container_type::const_reference;
  using iterator = typename container_type::iterator;
  using const_iterator = typename container_type::const_iterator;
  using reverse_iterator = typename container_type::reverse_iterator;
  using const_reverse_iterator =
      typename container_type::const_reverse_iterator;
  using size_type = typename container_type::size_type;
  using difference_type = typename container_type::difference_type;

  linear_map() = default;
  linear_map(std::initializer_list<value_type> init_list) {
    linear_map::insert(init_list.begin(), init_list.end());
  }

  template <typename Iterator>
  linear_map(Iterator first1, Iterator last1) {
    linear_map::insert(first1, last1);
  }

  const_iterator find(const key_type& key) const {
    return std::find_if(map_.cbegin(), map_.cend(),
                        [&key](const value_type& p) { return p.first == key; });
  }

  iterator find(const key_type& key) {
    return std::find_if(map_.begin(), map_.end(),
                        [&key](const value_type& p) { return p.first == key; });
  }

  size_type count(const key_type& key) const {
    return linear_map::find(key) != map_.end();
  }

  mapped_type& operator[](const key_type& key) {
    auto it = linear_map::find(key);
    if (it != map_.end())
      return it->second;
    map_.emplace_back(key, mapped_type());
    return map_.back().second;
  }

  mapped_type& operator[](key_type&& key) {
    auto it = linear_map::find(key);
    if (it != map_.end())
      return it->second;
    map_.emplace_back(std::move(key), mapped_type());
    return map_.back().second;
  }

  const mapped_type& at(const key_type& key) const {
    auto it = linear_map::find(key);
    if (it == map_.end())
      throw std::out_of_range("Key is not found.");
    return it->second;
  }

  mapped_type& at(const key_type& key) {
    return const_cast<mapped_type&>(std::as_const(*this).at(key));
  }

  std::pair<iterator, bool> insert(const value_type& p) {
    auto it = linear_map::find(p.first);
    if (it != map_.end())
      return {it, false};
    map_.push_back(p);
    return {std::prev(map_.end()), true};
  }

  std::pair<iterator, bool> insert(value_type&& p) {
    auto it = linear_map::find(p.first);
    if (it != map_.end())
      return {it, false};
    map_.push_back(std::move(p));
    return {std::prev(map_.end()), true};
  }

  template <typename Iterator>
  void insert(Iterator first1, Iterator last1) {
    for (; first1 != last1; ++first1) {
      if (linear_map::find(first1->first) == map_.end())
        map_.push_back(*first1);
    }
  }

  template <typename... Ts>
  std::pair<iterator, bool> emplace(Ts&&... args) {
    return linear_map::insert(value_type(std::forward<Ts>(args)...));
  }

  iterator erase(const_iterator pos) { return ErasePolicy::erase(pos, &map_); }
  iterator erase(iterator pos) { return ErasePolicy::erase(pos, &map_); }

  size_type erase(const key_type& key) {
    iterator it = linear_map::find(key);
    if (it == map_.end())
      return 0;
    linear_map::erase(it);
    return 1;
  }

  void swap(linear_map& other) noexcept { map_.swap(other.map_); }

  size_type size() const { return map_.size(); }

  bool empty() const { return map_.empty(); }

  void clear() noexcept { map_.clear(); }

  void reserve(size_type n) { map_.reserve(n); }

  size_type capacity() const { return map_.capacity(); }

  container_type& data() { return map_; }
  const container_type& data() const { return map_; }

  iterator begin() { return map_.begin(); }
  iterator end() { return map_.end(); }

  const_iterator cbegin() const { return map_.cbegin(); }
  const_iterator begin() const { return map_.cbegin(); }

  const_iterator cend() const { return map_.cend(); }
  const_iterator end() const { return map_.cend(); }

  reverse_iterator rbegin() { return map_.rbegin(); }
  reverse_iterator rend() { return map_.rend(); }
  const_reverse_iterator crbegin() const { return map_.crbegin(); }
  const_reverse_iterator rbegin() const { return map_.crbegin(); }
  const_reverse_iterator crend() const { return map_.crend(); }
  const_reverse_iterator rend() const { return map_.crend(); }

 private:
  container_type map_;  
};

}  // namespace ext

Updated on 2025-11-11 at 16:51:08 +0000