summaryrefslogtreecommitdiff
path: root/tiger-compiler/lib/misc/endomap.hxx
blob: 58c1912beb3258dd60c8f31c936a2c45eb7110e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/**                                                     -*- C++ -*-
 ** \file misc/endomap.hxx
 ** \brief Implementation of misc::endomap.
 */

#pragma once

#include <misc/endomap.hh>

namespace misc
{
  template <class T>
  endomap<T>::endomap()
    : map<T, T>()
    , strictness_(strictness_type::nonstrict)
  {}

  template <class T> endomap<T>* endomap<T>::clone() const
  {
    return new endomap<T>(*this);
  }

  template <class T> T endomap<T>::operator()(const T& t) const
  {
    if (const auto&& ires = this->find(t); ires != this->map_.end())
      return ires->second;
    else if (this->strictness_ == strictness_type::nonstrict)
      return t;
    std::ostringstream err;
    err << "map: no mapping for " << t;
    throw std::range_error(err.str());
  }

  template <class T> T& endomap<T>::operator[](const T& t)
  {
    // Inspired by ``Efficient STL'' on efficient insert_or_update
    // for maps.  See also misc::put.
    auto i = this->map_.lower_bound(t);
    if (i == this->map_.end() || this->map_.key_comp()(t, i->first))
      i = this->map_.emplace(t, t).first;
    return i->second;
  }

} // namespace misc