summary refs log tree commit diff stats
path: root/tools/sprite_dumper/identifier.h
blob: 4aac943f72ef2de65c943d5b5618a9d04d3022ff (plain) (blame)
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#ifndef IDENTIFIER_H_D7EE5679
#define IDENTIFIER_H_D7EE5679

#include <map>
#include <vector>

template <typename T>
class DefaultKeyExtract {
public:

  const T& operator()(const T& val) const {
    return val;
  }
};

template <typename T, typename KeyExtract = DefaultKeyExtract<T>>
class identifier {
public:

  using value_type = T;

private:

  using key_type = std::remove_reference_t<std::invoke_result_t<KeyExtract, const T&>>;
  using vector_type = std::vector<value_type>;

public:

  using id_type = typename vector_type::size_type;

  id_type add(const value_type& val)
  {
    auto it = ids_.find(KeyExtract()(val));

    if (it == std::end(ids_))
    {
      id_type ret = ids_.size();
      ids_[KeyExtract()(val)] = ret;

      uniq_.push_back(val);

      return ret;
    } else {
      return it->second;
    }
  }

  void compile()
  {
    ids_.clear();
  }

  inline const value_type& get(id_type i) const
  {
    return uniq_.at(i);
  }

  inline id_type size() const
  {
    return uniq_.size();
  }

private:

  std::map<key_type, id_type> ids_;
  vector_type uniq_;
};

#endif /* end of include guard: IDENTIFIER_H_D7EE5679 */