summary refs log tree commit diff stats
path: root/tools/sprite_dumper/identifier.h
diff options
context:
space:
mode:
Diffstat (limited to 'tools/sprite_dumper/identifier.h')
-rw-r--r--tools/sprite_dumper/identifier.h26
1 files changed, 18 insertions, 8 deletions
diff --git a/tools/sprite_dumper/identifier.h b/tools/sprite_dumper/identifier.h index 74d83ce..4aac943 100644 --- a/tools/sprite_dumper/identifier.h +++ b/tools/sprite_dumper/identifier.h
@@ -5,6 +5,15 @@
5#include <vector> 5#include <vector>
6 6
7template <typename T> 7template <typename T>
8class DefaultKeyExtract {
9public:
10
11 const T& operator()(const T& val) const {
12 return val;
13 }
14};
15
16template <typename T, typename KeyExtract = DefaultKeyExtract<T>>
8class identifier { 17class identifier {
9public: 18public:
10 19
@@ -12,20 +21,21 @@ public:
12 21
13private: 22private:
14 23
24 using key_type = std::remove_reference_t<std::invoke_result_t<KeyExtract, const T&>>;
15 using vector_type = std::vector<value_type>; 25 using vector_type = std::vector<value_type>;
16 26
17public: 27public:
18 28
19 using key_type = typename vector_type::size_type; 29 using id_type = typename vector_type::size_type;
20 30
21 key_type add(const value_type& val) 31 id_type add(const value_type& val)
22 { 32 {
23 auto it = ids_.find(val); 33 auto it = ids_.find(KeyExtract()(val));
24 34
25 if (it == std::end(ids_)) 35 if (it == std::end(ids_))
26 { 36 {
27 key_type ret = ids_.size(); 37 id_type ret = ids_.size();
28 ids_[val] = ret; 38 ids_[KeyExtract()(val)] = ret;
29 39
30 uniq_.push_back(val); 40 uniq_.push_back(val);
31 41
@@ -40,19 +50,19 @@ public:
40 ids_.clear(); 50 ids_.clear();
41 } 51 }
42 52
43 inline const value_type& get(key_type i) const 53 inline const value_type& get(id_type i) const
44 { 54 {
45 return uniq_.at(i); 55 return uniq_.at(i);
46 } 56 }
47 57
48 inline key_type size() const 58 inline id_type size() const
49 { 59 {
50 return uniq_.size(); 60 return uniq_.size();
51 } 61 }
52 62
53private: 63private:
54 64
55 std::map<value_type, key_type> ids_; 65 std::map<key_type, id_type> ids_;
56 vector_type uniq_; 66 vector_type uniq_;
57}; 67};
58 68