Browse Source

Updated LastSeen to use vector.

Added static best_guess() that tries
to determine with the number of items
the best number of items to track.

Currently, it uses max_items / 2.
bugz 4 years ago
parent
commit
707cc53ccb
2 changed files with 33 additions and 16 deletions
  1. 27 14
      lastseen.cpp
  2. 6 2
      lastseen.h

+ 27 - 14
lastseen.cpp

@@ -1,25 +1,38 @@
 #include "lastseen.h"
 
-LastSeen::LastSeen(int max_nondups) {
-  this->max = max_nondups;
-  this->tracker = new int[max_nondups];
-  for (int i = 0; i < max_nondups; i++) {
-    this->tracker[i] = -1;
-  }
+LastSeen::LastSeen(int max) {
+  this->max = max;
+
+  tracker.reserve(max);
+  while ((int)tracker.size() < max)
+      tracker.insert(tracker.begin(), -1);
 }
 
-LastSeen::~LastSeen() { delete this->tracker; }
+/*
+int LastSeen::bestsize(int max_items) {
+  int best = max_items / 2;
+  return best;
+}
+*/
+
+int LastSeen::getmax(void) {
+  return this->max;
+}
+
+LastSeen::~LastSeen() {  };
 
 bool LastSeen::seen_before(int this_one) {
-  int i;
-  for (i = 0; i < this->max; i++) {
-    if (this->tracker[i] == this_one)
+  for (auto it = this->tracker.begin(); it != this->tracker.end(); ++it ) {
+    if (*it == this_one)
       return true;
   }
+
   // Ok, it hasn't been seen before.
-  for (i = 0; i < this->max - 1; i++) {
-    this->tracker[i] = this->tracker[i + 1];
-  };
-  this->tracker[i] = this_one;
+  this->tracker.erase(this->tracker.begin());
+  this->tracker.push_back(this_one);
   return false;
 }
+
+int LastSeen::best_guess(int max) {
+  return max / 2;
+}

+ 6 - 2
lastseen.h

@@ -1,15 +1,19 @@
 #ifndef LASTSEEN_H
 #define LASTSEEN_H
 
+#include <vector>
+
 class LastSeen {
 private:
-  int *tracker;
+  std::vector<int> tracker;
   int max;
 
 public:
-  LastSeen(int max_nondups);
+  LastSeen(int max);
   ~LastSeen();
+  int getmax(void);
   bool seen_before(int this_one);
+  static int best_guess(int max);
 };
 
 #endif