summaryrefslogtreecommitdiff
path: root/chromium/net/quic/congestion_control/quic_max_sized_map.h
diff options
context:
space:
mode:
authorZeno Albisser <zeno.albisser@digia.com>2013-08-15 21:46:11 +0200
committerZeno Albisser <zeno.albisser@digia.com>2013-08-15 21:46:11 +0200
commit679147eead574d186ebf3069647b4c23e8ccace6 (patch)
treefc247a0ac8ff119f7c8550879ebb6d3dd8d1ff69 /chromium/net/quic/congestion_control/quic_max_sized_map.h
downloadqtwebengine-chromium-679147eead574d186ebf3069647b4c23e8ccace6.tar.gz
Initial import.
Diffstat (limited to 'chromium/net/quic/congestion_control/quic_max_sized_map.h')
-rw-r--r--chromium/net/quic/congestion_control/quic_max_sized_map.h77
1 files changed, 77 insertions, 0 deletions
diff --git a/chromium/net/quic/congestion_control/quic_max_sized_map.h b/chromium/net/quic/congestion_control/quic_max_sized_map.h
new file mode 100644
index 00000000000..a4ed7769d61
--- /dev/null
+++ b/chromium/net/quic/congestion_control/quic_max_sized_map.h
@@ -0,0 +1,77 @@
+// Copyright (c) 2013 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// Simple max sized map. Automatically deletes the oldest element when the
+// max limit is reached.
+// Note: the ConstIterator will NOT be valid after an Insert or RemoveAll.
+#ifndef NET_QUIC_CONGESTION_CONTROL_QUIC_MAX_SIZED_MAP_H_
+#define NET_QUIC_CONGESTION_CONTROL_QUIC_MAX_SIZED_MAP_H_
+
+#include <stdlib.h>
+
+#include <list>
+#include <map>
+
+#include "base/basictypes.h"
+
+namespace net {
+
+template <class Key, class Value>
+class QuicMaxSizedMap {
+ public:
+ typedef typename std::multimap<Key, Value>::const_iterator ConstIterator;
+
+ explicit QuicMaxSizedMap(size_t max_numer_of_items)
+ : max_numer_of_items_(max_numer_of_items) {
+ }
+
+ size_t MaxSize() const {
+ return max_numer_of_items_;
+ }
+
+ size_t Size() const {
+ return table_.size();
+ }
+
+ void Insert(const Key& k, const Value& value) {
+ if (Size() == MaxSize()) {
+ ListIterator list_it = insert_order_.begin();
+ table_.erase(*list_it);
+ insert_order_.pop_front();
+ }
+ TableIterator it = table_.insert(std::pair<Key, Value>(k, value));
+ insert_order_.push_back(it);
+ }
+
+ void RemoveAll() {
+ table_.clear();
+ insert_order_.clear();
+ }
+
+ // STL style const_iterator support.
+ ConstIterator Find(const Key& k) const {
+ return table_.find(k);
+ }
+
+ ConstIterator Begin() const {
+ return ConstIterator(table_.begin());
+ }
+
+ ConstIterator End() const {
+ return ConstIterator(table_.end());
+ }
+
+ private:
+ typedef typename std::multimap<Key, Value>::iterator TableIterator;
+ typedef typename std::list<TableIterator>::iterator ListIterator;
+
+ const size_t max_numer_of_items_;
+ std::multimap<Key, Value> table_;
+ std::list<TableIterator> insert_order_;
+
+ DISALLOW_COPY_AND_ASSIGN(QuicMaxSizedMap);
+};
+
+} // namespace net
+#endif // NET_QUIC_CONGESTION_CONTROL_QUIC_MAX_SIZED_MAP_H_