Browse Source

Add consts to LRU and use std::optional

c++17 (which our compiler supports) has std::optional<T> that can
be used for thigns that return a value or nothing (like this) Use
that.

add in some consts and pass parameters by const reference.

use std::uint32_t instead of uint. Initialize it with the constructor
initializer list instead of in the body of the constructor, and use
uniform initialization syntax to do so.

Initialize the cache instantiation using uniform initialization.

Use a few more autos so that we don't have to type const as much.
changes/91/636791/1
Monty Taylor 3 months ago
parent
commit
c9245f61ec
2 changed files with 17 additions and 23 deletions
  1. 1
    1
      m4/opendev_canonical.m4
  2. 16
    22
      zuul-preview/main.cc

+ 1
- 1
m4/opendev_canonical.m4 View File

@@ -20,7 +20,7 @@ AC_DEFUN([OPENDEV_CANONICAL_TARGET],[
20 20
   AX_IS_RELEASE(always)
21 21
   AM_SILENT_RULES([yes])
22 22
 
23
-  AX_CXX_COMPILE_STDCXX([14],[],[mandatory])
23
+  AX_CXX_COMPILE_STDCXX([17],[],[mandatory])
24 24
   AM_PROG_CC_C_O
25 25
   AC_PROG_CC_STDC
26 26
   gl_VISIBILITY

+ 16
- 22
zuul-preview/main.cc View File

@@ -38,35 +38,29 @@ vector<string> split(const string &in)
38 38
 class Cache {
39 39
   // A queue of hostname, URL pairs.  The head of the queue is always
40 40
   // the most recently accessed entry, the tail is the least.
41
-  list<pair<string, string>> queue;
41
+  list<pair<const string, const string>> queue;
42 42
 
43 43
   // A map of hostname -> iterator that points into the queue, for
44 44
   // quick lookup.
45
-  unordered_map<string, list<pair<string, string>>::iterator> map;
45
+  unordered_map<string, list<pair<const string, const string>>::iterator> map;
46 46
 
47 47
   // The maximum size of the cache.
48
-  uint size;
48
+  const uint32_t size;
49 49
 
50 50
 public:
51
-  // A constant returned by get if the entry is not found.
52
-  string notfound = "";
53
-
54 51
   Cache(uint s)
55
-    : queue {}, map {}, size(0)
56
-  {
57
-    size = s;
58
-  }
52
+    : queue {}, map {}, size{s}
53
+  { }
59 54
 
60
-  // Lookup the hostname in the cache and return the URL if present,
61
-  // notfound otherwise.  If the entry is present, it is moved to the
62
-  // head of the queue.
63
-  string get(string key)
55
+  // Lookup the hostname in the cache and return the URL if present.
56
+  // If the entry is present, it is moved to the head of the queue.
57
+  optional<const string> get(const string &key)
64 58
   {
65 59
     auto location = map.find(key);
66 60
     if (location == map.end())
67
-      return notfound;
61
+      return {};
68 62
 
69
-    pair<string, string> val = *(location->second);
63
+    auto val = *(location->second);
70 64
     queue.erase(location->second);
71 65
     queue.push_front(val);
72 66
     cout << "get push " << val.second << endl;
@@ -75,14 +69,14 @@ public:
75 69
 
76 70
   // Add an entry to the cache.  If the cache is full, drop the least
77 71
   // recently used entry.
78
-  void put(string key, string value)
72
+  void put(const string &key, const string &value)
79 73
   {
80 74
     auto location = map.find(key);
81 75
     if (location != map.end())
82 76
       return;
83 77
 
84 78
     if (queue.size() == size) {
85
-      pair<string, string> last = queue.back();
79
+      auto last = queue.back();
86 80
       cout << "put pop " << last.second << endl;
87 81
       queue.pop_back();
88 82
       map.erase(last.first);
@@ -100,7 +94,7 @@ int main(int, char**)
100 94
   web::http::client::http_client client("https://zuul.opendev.org");
101 95
 
102 96
   string hostname;
103
-  Cache cache = Cache(2);
97
+  Cache cache{2};
104 98
   while (getline(cin, hostname)) {
105 99
     // Expected hostname:
106 100
     // site.75031cad206c4014ad7a3387091d15ab.openstack.preview.opendev.org
@@ -110,9 +104,9 @@ int main(int, char**)
110 104
     // site.688b70499b9a41a08f498ed6e932960c.openstack
111 105
     // site.dbefc23dcc594577a8bfa4db4f9b0a8f.openstack
112 106
 
113
-    string val = cache.get(hostname);
114
-    if (val != cache.notfound) {
115
-      cout << val << endl;
107
+    auto val = cache.get(hostname);
108
+    if (val.has_value()) {
109
+      cout << val.value() << endl;
116 110
       continue;
117 111
     }
118 112
 

Loading…
Cancel
Save