Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Moved HeavyHitters out of header into cpp.

  • Loading branch information...
commit c725b6280b6da9de3e465ca0d8c22871f78909d9 1 parent e157200
Andrew Thomson authored
Showing with 82 additions and 74 deletions.
  1. +73 −0 HeavyHitters.cpp
  2. +9 −74 HeavyHitters.hpp
73 HeavyHitters.cpp
View
@@ -1 +1,74 @@
#include "HeavyHitters.hpp"
+
+HeavyHittersTracker::HeavyHittersTracker(size_t n) :
+ n_(n),
+ colorToValue_()
+{
+}
+
+std::map<std::string, double>::iterator HeavyHittersTracker::begin()
+{
+ std::map<std::string, double>::iterator iterator;
+ iterator = colorToValue_.begin();
+
+ return iterator;
+}
+
+std::map<std::string, double>::iterator HeavyHittersTracker::end()
+{
+ std::map<std::string, double>::iterator iterator;
+ iterator = colorToValue_.end();
+
+ return iterator;
+}
+
+void HeavyHittersTracker::Update(std::string c, double v)
+{
+ std::map<std::string, double>::iterator c_iterator;
+ c_iterator = colorToValue_.find(c);
+
+ if(c_iterator != colorToValue_.end()) {
+
+ // Color already in map
+ double old_v = c_iterator->second;
+ colorToValue_.erase(c);
+ colorToValue_[c] = old_v + v;
+ } else {
+ // Color is not in map, add it.
+
+ if(colorToValue_.size() < n_) {
+
+ // Were not full, so just insert.
+ colorToValue_[c] = v;
+ } else {
+ // We are full, replace smallest.
+ std::string smallest_color = getSmallestColor();
+ double old_v = colorToValue_[smallest_color];
+ colorToValue_.erase(smallest_color);
+ colorToValue_[c] = old_v + v;
+ }
+ }
+}
+
+std::string HeavyHittersTracker::getSmallestColor()
+{
+ std::string smallest_color;
+ double smallest_value;
+
+ std::map<std::string, double>::iterator iterator;
+ iterator = colorToValue_.begin();
+
+ smallest_color = iterator->first;
+ smallest_value = iterator->second;
+ ++iterator;
+
+ while(iterator != colorToValue_.end()) {
+ if(smallest_value >= iterator->second) {
+ smallest_color = iterator->first;
+ smallest_value = iterator->second;
+ }
+ ++iterator;
+ }
+ return smallest_color;
+}
+
83 HeavyHitters.hpp
View
@@ -7,87 +7,22 @@
class HeavyHittersTracker
{
-
private:
-
+
size_t n_;
std::map<std::string, double> colorToValue_;
public:
-
- HeavyHittersTracker(size_t n) :
- n_(n),
- colorToValue_()
- {
- }
-
- std::map<std::string, double>::iterator begin()
- {
- std::map<std::string, double>::iterator iterator;
- iterator = colorToValue_.begin();
-
- return iterator;
- }
-
- std::map<std::string, double>::iterator end()
- {
- std::map<std::string, double>::iterator iterator;
- iterator = colorToValue_.end();
-
- return iterator;
- }
-
- void Update(std::string c, double v)
- {
- std::map<std::string, double>::iterator c_iterator;
- c_iterator = colorToValue_.find(c);
-
- if(c_iterator != colorToValue_.end()) {
-
- // Color already in map
- double old_v = c_iterator->second;
- colorToValue_.erase(c);
- colorToValue_[c] = old_v + v;
- } else {
- // Color is not in map, add it.
-
- if(colorToValue_.size() < n_) {
-
- // Were not full, so just insert.
- colorToValue_[c] = v;
- } else {
- // We are full, replace smallest.
- std::string smallest_color = getSmallestColor();
- double old_v = colorToValue_[smallest_color];
- colorToValue_.erase(smallest_color);
- colorToValue_[c] = old_v + v;
- }
- }
- }
-
+
+ HeavyHittersTracker(size_t n);
+ std::map<std::string, double>::iterator begin();
+ std::map<std::string, double>::iterator end();
+ void Update(std::string c, double v);
+
private:
- std::string getSmallestColor()
- {
- std::string smallest_color;
- double smallest_value;
-
- std::map<std::string, double>::iterator iterator;
- iterator = colorToValue_.begin();
-
- smallest_color = iterator->first;
- smallest_value = iterator->second;
- ++iterator;
-
- while(iterator != colorToValue_.end()) {
- if(smallest_value >= iterator->second) {
- smallest_color = iterator->first;
- smallest_value = iterator->second;
- }
- ++iterator;
- }
- return smallest_color;
- }
+ std::string getSmallestColor();
+
};
#endif /* HEAVY_HITTERS_HPP */
Please sign in to comment.
Something went wrong with that request. Please try again.