TorchCraftAI
A bot for machine learning research on StarCraft: Brood War
language.h
1 /*
2  * Copyright (c) 2017-present, Facebook, Inc.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  */
7 
8 #pragma once
9 
10 #include <list>
11 #include <memory>
12 #include <type_traits>
13 #include <unordered_map>
14 #include <utility>
15 
16 namespace common {
17 
18 template <typename Enumeration>
19 auto enumAsInt(Enumeration const value) ->
20  typename std::underlying_type<Enumeration>::type {
21  return static_cast<typename std::underlying_type<Enumeration>::type>(value);
22 }
23 
24 // From https://gist.github.com/mrts/5890888, which is based on Alex Andrescu's
25 // implementation at http://bit.ly/2wfJnWn.
26 template <class Function>
27 class ScopeGuard {
28  public:
29  ScopeGuard(Function f) : guardFunction_(std::move(f)), active_(true) {}
30 
32  if (active_) {
33  guardFunction_();
34  }
35  }
36 
38  : guardFunction_(std::move(rhs.guardFunction_)), active_(rhs.active_) {
39  rhs.dismiss();
40  }
41 
42  void dismiss() {
43  active_ = false;
44  }
45 
46  private:
47  Function guardFunction_;
48  bool active_;
49 
50  ScopeGuard() = delete;
51  ScopeGuard(const ScopeGuard&) = delete;
52  ScopeGuard& operator=(const ScopeGuard&) = delete;
53 };
54 
55 template <class Function>
57  return ScopeGuard<Function>(std::move(f));
58 }
59 
60 template <typename K, typename V>
61 class LRUCache {
62  // store keys of cache
63  std::list<K> dq_;
64 
65  // store references of key in cache
66  std::unordered_map<
67  K,
68  std::pair<typename std::list<K>::iterator, std::unique_ptr<V>>>
69  map_;
70 
71  size_t csize_; // maximum capacity of cache
72 
73  public:
74  LRUCache(int n) : csize_(n) {}
75 
76  inline V* put(K k, std::unique_ptr<V>&& v) {
77  if (map_.find(k) == map_.end()) {
78  // Not in cache, cache size too big
79  if (dq_.size() == csize_) {
80  map_.erase(dq_.back());
81  dq_.pop_back();
82  }
83  } else {
84  dq_.erase(map_[k].first);
85  }
86 
87  dq_.push_front(k);
88  map_[k] = std::make_pair(dq_.begin(), std::move(v));
89  return map_[k].second.get();
90  }
91 
92  inline V* get(K const& k) {
93  if (map_.find(k) == map_.end()) {
94  return nullptr;
95  } else {
96  // Move list node to front
97  auto& it = map_[k].first;
98  dq_.splice(dq_.begin(), dq_, it);
99  return map_[k].second.get();
100  }
101  }
102 };
103 
104 } // namespace common
Definition: language.h:27
ScopeGuard< Function > makeGuard(Function f)
Definition: language.h:56
LRUCache(int n)
Definition: language.h:74
STL namespace.
~ScopeGuard()
Definition: language.h:31
auto enumAsInt(Enumeration const value) -> typename std::underlying_type< Enumeration >::type
Definition: language.h:19
Definition: language.h:61
General utilities.
Definition: assert.cpp:7
void dismiss()
Definition: language.h:42
ScopeGuard(Function f)
Definition: language.h:29
ScopeGuard(ScopeGuard &&rhs)
Definition: language.h:37
V * put(K k, std::unique_ptr< V > &&v)
Definition: language.h:76