Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit a82513e8 authored by Jack He's avatar Jack He
Browse files

GD: Add ListMap and LruCache

* ListMap is a map whose keys are sorted by the order of when the
  key was added to the map
* LruCache is a map that evict the least used key when reaching
  capacity
* LruCache depends on ListMap for its internal functions

Bug: 157533831
Bug: 157534088
Tag: #gd-refactor
Test: atest bluetooth_test_gd
Change-Id: Ie112f96e48f108c5bb589ab3469d0892b51b28d7
parent 7d484c8b
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -14,5 +14,7 @@ filegroup {
        "observer_registry_test.cc",
        "link_key_unittest.cc",
        "init_flags_test.cc",
        "list_map_test.cc",
        "lru_cache_test.cc",
    ],
}
+214 −0
Original line number Diff line number Diff line
/*
 * Copyright 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#pragma once

#include <functional>
#include <iterator>
#include <list>
#include <mutex>
#include <optional>
#include <thread>
#include <type_traits>
#include <unordered_map>

#include "os/log.h"

namespace bluetooth {
namespace common {

// A map that maintains order of its element as a list. An element that is put earlier will appear before an element
// that is put later when iterating through this map's entries. Keys must be unique.
//
// Performance:
//   - Key look-up and modification is O(1)
//   - Value operated by replacement, no in-place modification
//   - Memory consumption is:
//     O(2*capacity*sizeof(K) + capacity*(sizeof(nullptr)+sizeof(V)))
//   - NOT THREAD SAFE
//
// Template:
//   - Key key
//   - T value
template <typename Key, typename T>
class ListMap {
 public:
  using value_type = std::pair<const Key, T>;
  // different from c++17 node_type on purpose as we want node to be copyable
  using node_type = std::pair<Key, T>;
  using iterator = typename std::list<value_type>::iterator;
  using const_iterator = typename std::list<value_type>::const_iterator;

  // Constructor of the list map
  ListMap() = default;

  // for move
  ListMap(ListMap&& other) noexcept = default;
  ListMap& operator=(ListMap&& other) noexcept = default;

  // copy-constructor
  // iterators in key_map_ cannot be copied directly
  ListMap(const ListMap& other) : node_list_(other.node_list_) {
    for (auto iter = node_list_.begin(); iter != node_list_.end(); iter++) {
      key_map_.emplace(iter->first, iter);
    }
  }

  // copy-assignment
  // iterators in key_map_ cannot be copied directly
  ListMap& operator=(const ListMap& other) {
    if (&other == this) {
      return *this;
    }
    node_list_ = other.node_list_;
    key_map_.clear();
    for (auto iter = node_list_.begin(); iter != node_list_.end(); iter++) {
      key_map_.emplace(iter->first, iter);
    }
    return *this;
  }

  ~ListMap() {
    clear();
  }

  // Clear the list map
  void clear() {
    key_map_.clear();
    node_list_.clear();
  }

  // const version of find()
  const_iterator find(const Key& key) const {
    return const_cast<ListMap*>(this)->find(key);
  }

  // Get the value of a key. Return iterator to the item if found, end() if not found
  iterator find(const Key& key) {
    auto map_iterator = key_map_.find(key);
    if (map_iterator == key_map_.end()) {
      return end();
    }
    return map_iterator->second;
  }

  // Check if key exist in the map. Return true if key exist in map, false if not.
  bool contains(const Key& key) const {
    return find(key) != end();
  }

  // Try emplace an element before a specific position |pos| of the list map. If the |key| already exists, does nothing.
  // Moved arguments won't be moved when key already exists. Return <iterator, true> when key does not exist, <iterator,
  // false> when key exist and iterator is the position where it was placed.
  template <class... Args>
  std::pair<iterator, bool> try_emplace(const_iterator pos, const Key& key, Args&&... args) {
    auto map_iterator = key_map_.find(key);
    if (map_iterator != key_map_.end()) {
      return std::make_pair(end(), false);
    }
    auto list_iterator = node_list_.emplace(pos, key, std::forward<Args>(args)...);
    key_map_.emplace(key, list_iterator);
    return std::make_pair(list_iterator, true);
  }

  // Try emplace an element before the end of the list map. If the key already exists, does nothing. Moved arguments
  // won't be moved when key already exists return <iterator, true> when key does not exist, <iterator, false> when key
  // exist and iterator is the position where it was placed
  template <class... Args>
  std::pair<iterator, bool> try_emplace_back(const Key& key, Args&&... args) {
    return try_emplace(end(), key, std::forward<Args>(args)...);
  }

  // Put a key-value pair to the map before position. If key already exist, |pos| will be ignored and existing value
  // will be replaced
  void insert_or_assign(const_iterator pos, const Key& key, T value) {
    auto map_iterator = key_map_.find(key);
    if (map_iterator != key_map_.end()) {
      map_iterator->second->second = std::move(value);
      return;
    }
    auto list_iterator = node_list_.emplace(pos, key, std::move(value));
    key_map_.emplace(key, list_iterator);
  }

  // Put a key-value pair to the tail of the map or replace the current value without moving the key if key exists
  void insert_or_assign(const Key& key, T value) {
    insert_or_assign(end(), key, std::move(value));
  }

  // STL splice, same as std::list::splice
  // - pos: element before which the content will be inserted
  // - other: another container to transfer the content from
  // - it: the element to transfer from other to *this
  void splice(const_iterator pos, ListMap<Key, T>& other, const_iterator it) {
    if (&other != this) {
      auto map_node = other.key_map_.extract(it->first);
      key_map_.insert(std::move(map_node));
    }
    node_list_.splice(pos, other.node_list_, it);
  }

  // Remove a key from the list map and return removed value if key exits, std::nullopt if not. The return value will be
  // evaluated to true in a boolean context if a value is contained by std::optional, false otherwise.
  std::optional<node_type> extract(const Key& key) {
    auto map_iterator = key_map_.find(key);
    if (map_iterator == key_map_.end()) {
      return std::nullopt;
    }
    std::optional<node_type> removed_node(std::move(*map_iterator->second));
    node_list_.erase(map_iterator->second);
    key_map_.erase(map_iterator);
    return removed_node;
  }

  // Remove an iterator pointed item from the list map and return the iterator immediately after the erased item
  iterator erase(const_iterator iter) {
    key_map_.erase(iter->first);
    return node_list_.erase(iter);
  }

  // Return size of the list map
  inline size_t size() const {
    return node_list_.size();
  }

  // Return iterator interface for begin
  inline iterator begin() {
    return node_list_.begin();
  }

  // Iterator interface for begin, const
  inline const_iterator begin() const {
    return node_list_.begin();
  }

  // Iterator interface for end
  inline iterator end() {
    return node_list_.end();
  }

  // Iterator interface for end, const
  inline const_iterator end() const {
    return node_list_.end();
  }

 private:
  std::list<value_type> node_list_;
  std::unordered_map<Key, iterator> key_map_;
};

}  // namespace common
}  // namespace bluetooth
 No newline at end of file
+339 −0
Original line number Diff line number Diff line
/*
 * Copyright 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <chrono>
#include <memory>

#include <gmock/gmock.h>
#include <gtest/gtest.h>

#include "common/list_map.h"

namespace testing {

using bluetooth::common::ListMap;

TEST(ListMapTest, empty_test) {
  ListMap<int, int> list_map;
  EXPECT_EQ(list_map.size(), 0);
  EXPECT_EQ(list_map.find(42), list_map.end());
  list_map.clear();  // should not crash
  EXPECT_EQ(list_map.find(42), list_map.end());
  EXPECT_FALSE(list_map.contains(42));
  EXPECT_FALSE(list_map.extract(42));
}

TEST(ListMapTest, copy_test) {
  ListMap<int, std::shared_ptr<int>> list_map;
  list_map.insert_or_assign(1, std::make_shared<int>(100));
  auto iter = list_map.find(1);
  EXPECT_EQ(*iter->second, 100);
  ListMap<int, std::shared_ptr<int>> new_list_map = list_map;
  iter = new_list_map.find(1);
  EXPECT_EQ(*iter->second, 100);
  *iter->second = 300;
  iter = new_list_map.find(1);
  EXPECT_EQ(*iter->second, 300);
  // Since copy is used, shared_ptr should increase count
  EXPECT_EQ(iter->second.use_count(), 2);
}

TEST(ListMapTest, move_test) {
  ListMap<int, std::shared_ptr<int>> list_map;
  list_map.insert_or_assign(1, std::make_shared<int>(100));
  auto iter = list_map.find(1);
  EXPECT_EQ(*iter->second, 100);
  ListMap<int, std::shared_ptr<int>> new_list_map = std::move(list_map);
  iter = new_list_map.find(1);
  EXPECT_EQ(*iter->second, 100);
  *iter->second = 300;
  iter = new_list_map.find(1);
  EXPECT_EQ(*iter->second, 300);
  // Since move is used, shared_ptr should not increase count
  EXPECT_EQ(iter->second.use_count(), 1);
}

TEST(ListMapTest, move_insert_unique_ptr_test) {
  ListMap<int, std::unique_ptr<int>> list_map;
  list_map.insert_or_assign(1, std::make_unique<int>(100));
  auto iter = list_map.find(1);
  EXPECT_EQ(*iter->second, 100);
  list_map.insert_or_assign(1, std::make_unique<int>(400));
  iter = list_map.find(1);
  EXPECT_EQ(*iter->second, 400);
}

TEST(ListMapTest, move_insert_list_map_test) {
  ListMap<int, ListMap<int, int>> list_map;
  ListMap<int, int> m1;
  m1.insert_or_assign(1, 100);
  list_map.insert_or_assign(1, std::move(m1));
  auto iter = list_map.find(1);
  EXPECT_THAT(iter->second, ElementsAre(Pair(1, 100)));
  ListMap<int, int> m2;
  m2.insert_or_assign(2, 200);
  list_map.insert_or_assign(1, std::move(m2));
  iter = list_map.find(1);
  EXPECT_THAT(iter->second, ElementsAre(Pair(2, 200)));
}

TEST(ListMapTest, erase_one_item_test) {
  ListMap<int, int> list_map;
  list_map.insert_or_assign(1, 10);
  list_map.insert_or_assign(2, 20);
  list_map.insert_or_assign(3, 30);
  auto iter = list_map.find(2);
  iter = list_map.erase(iter);
  EXPECT_EQ(iter->first, 3);
  EXPECT_EQ(iter->second, 30);
}

TEST(ListMapTest, erase_in_for_loop_test) {
  ListMap<int, int> list_map;
  list_map.insert_or_assign(1, 10);
  list_map.insert_or_assign(2, 20);
  list_map.insert_or_assign(3, 30);
  for (auto iter = list_map.begin(); iter != list_map.end();) {
    if (iter->first == 2) {
      iter = list_map.erase(iter);
    } else {
      ++iter;
    }
  }
  EXPECT_THAT(list_map, ElementsAre(Pair(1, 10), Pair(3, 30)));
}

TEST(ListMapTest, splice_different_list_test) {
  ListMap<int, int> list_map;
  list_map.insert_or_assign(1, 10);
  list_map.insert_or_assign(2, 20);
  list_map.insert_or_assign(3, 30);
  ListMap<int, int> list_map_2;
  list_map_2.insert_or_assign(4, 40);
  list_map_2.insert_or_assign(5, 50);
  list_map.splice(list_map.find(2), list_map_2, list_map_2.find(4));
  EXPECT_EQ(list_map_2.find(4), list_map_2.end());
  auto iter = list_map.find(4);
  EXPECT_NE(iter, list_map.end());
  EXPECT_EQ(iter->second, 40);
  EXPECT_THAT(list_map, ElementsAre(Pair(1, 10), Pair(4, 40), Pair(2, 20), Pair(3, 30)));
}

TEST(ListMapTest, splice_same_list_test) {
  ListMap<int, int> list_map;
  list_map.insert_or_assign(1, 10);
  list_map.insert_or_assign(2, 20);
  list_map.insert_or_assign(3, 30);
  list_map.splice(list_map.find(2), list_map, list_map.find(3));
  EXPECT_THAT(list_map, ElementsAre(Pair(1, 10), Pair(3, 30), Pair(2, 20)));
  list_map.extract(2);
  list_map.insert_or_assign(list_map.begin(), 4, 40);
  EXPECT_THAT(list_map, ElementsAre(Pair(4, 40), Pair(1, 10), Pair(3, 30)));
  auto iter = list_map.find(4);
  EXPECT_EQ(iter->second, 40);
  list_map.splice(list_map.begin(), list_map, list_map.find(4));
  list_map.splice(list_map.begin(), list_map, list_map.find(3));
  list_map.splice(list_map.begin(), list_map, list_map.find(1));
  EXPECT_THAT(list_map, ElementsAre(Pair(1, 10), Pair(3, 30), Pair(4, 40)));
  iter = list_map.find(4);
  EXPECT_EQ(iter->second, 40);
  iter = list_map.find(3);
  EXPECT_EQ(iter->second, 30);
}

TEST(ListMapTest, put_get_and_contains_key_test) {
  ListMap<int, int> list_map;
  EXPECT_EQ(list_map.size(), 0);
  EXPECT_EQ(list_map.find(42), list_map.end());
  EXPECT_FALSE(list_map.contains(42));
  list_map.insert_or_assign(56, 200);
  EXPECT_EQ(list_map.find(42), list_map.end());
  EXPECT_FALSE(list_map.contains(42));
  auto iter = list_map.find(56);
  EXPECT_NE(iter, list_map.end());
  EXPECT_TRUE(list_map.contains(56));
  EXPECT_EQ(iter->second, 200);
  EXPECT_TRUE(list_map.extract(56));
  EXPECT_FALSE(list_map.contains(56));
}

TEST(ListMapTest, try_emplace_at_position_test) {
  ListMap<int, int> list_map;
  list_map.insert_or_assign(1, 10);
  list_map.insert_or_assign(2, 20);
  auto iter = list_map.find(2);
  EXPECT_EQ(iter->second, 20);
  auto result = list_map.try_emplace(iter, 42, 420);
  EXPECT_TRUE(result.second);
  iter = list_map.find(42);
  EXPECT_EQ(iter->second, 420);
  EXPECT_EQ(iter, result.first);
  ASSERT_THAT(list_map, ElementsAre(Pair(1, 10), Pair(42, 420), Pair(2, 20)));
  EXPECT_FALSE(list_map.try_emplace(result.first, 42, 420).second);
}

TEST(ListMapTest, try_emplace_back_test) {
  ListMap<int, int> list_map;
  list_map.insert_or_assign(1, 10);
  list_map.insert_or_assign(2, 20);
  auto result = list_map.try_emplace_back(42, 420);
  EXPECT_TRUE(result.second);
  auto iter = list_map.find(42);
  EXPECT_EQ(iter->second, 420);
  EXPECT_EQ(iter, result.first);
  ASSERT_THAT(list_map, ElementsAre(Pair(1, 10), Pair(2, 20), Pair(42, 420)));
  EXPECT_FALSE(list_map.try_emplace_back(42, 420).second);
}

TEST(ListMapTest, insert_at_position_test) {
  ListMap<int, int> list_map;
  list_map.insert_or_assign(1, 10);
  list_map.insert_or_assign(2, 20);
  auto iter = list_map.find(2);
  EXPECT_EQ(iter->second, 20);
  list_map.insert_or_assign(iter, 42, 420);
  iter = list_map.find(42);
  EXPECT_EQ(iter->second, 420);
  ASSERT_THAT(list_map, ElementsAre(Pair(1, 10), Pair(42, 420), Pair(2, 20)));
}

TEST(ListMapTest, in_place_modification_test) {
  ListMap<int, int> list_map;
  list_map.insert_or_assign(1, 10);
  list_map.insert_or_assign(2, 20);
  auto iter = list_map.find(2);
  iter->second = 200;
  ASSERT_THAT(list_map, ElementsAre(Pair(1, 10), Pair(2, 200)));
}

TEST(ListMapTest, get_test) {
  ListMap<int, int> list_map;
  list_map.insert_or_assign(1, 10);
  list_map.insert_or_assign(2, 20);
  auto iter = list_map.find(1);
  EXPECT_NE(iter, list_map.end());
  EXPECT_EQ(iter->second, 10);
}

TEST(ListMapTest, remove_test) {
  ListMap<int, int> list_map;
  for (int key = 0; key <= 30; key++) {
    list_map.insert_or_assign(key, key * 100);
  }
  for (int key = 0; key <= 30; key++) {
    EXPECT_TRUE(list_map.contains(key));
  }
  for (int key = 0; key <= 30; key++) {
    auto removed = list_map.extract(key);
    EXPECT_TRUE(removed);
    EXPECT_EQ(*removed, std::make_pair(key, key * 100));
  }
  for (int key = 0; key <= 30; key++) {
    EXPECT_FALSE(list_map.contains(key));
  }
}

TEST(ListMapTest, clear_test) {
  ListMap<int, int> list_map;
  for (int key = 0; key < 10; key++) {
    list_map.insert_or_assign(key, key * 100);
  }
  for (int key = 0; key < 10; key++) {
    EXPECT_TRUE(list_map.contains(key));
  }
  list_map.clear();
  for (int key = 0; key < 10; key++) {
    EXPECT_FALSE(list_map.contains(key));
  }

  for (int key = 0; key < 10; key++) {
    list_map.insert_or_assign(key, key * 1000);
  }
  for (int key = 0; key < 10; key++) {
    EXPECT_TRUE(list_map.contains(key));
  }
}

TEST(ListMapTest, container_test) {
  ListMap<int, int> list_map;
  list_map.insert_or_assign(1, 10);
  list_map.insert_or_assign(2, 20);
  ASSERT_THAT(list_map, ElementsAre(Pair(1, 10), Pair(2, 20)));
}

TEST(ListMapTest, iterator_test) {
  ListMap<int, int> list_map;
  list_map.insert_or_assign(1, 10);
  list_map.insert_or_assign(2, 20);
  std::list<std::pair<int, int>> list(list_map.begin(), list_map.end());
  ASSERT_THAT(list, ElementsAre(Pair(1, 10), Pair(2, 20)));
}

TEST(ListMapTest, for_loop_test) {
  ListMap<int, int> list_map;
  list_map.insert_or_assign(1, 10);
  list_map.insert_or_assign(2, 20);
  std::list<std::pair<int, int>> list;
  for (const auto& node : list_map) {
    list.emplace_back(node);
  }
  ASSERT_THAT(list, ElementsAre(Pair(1, 10), Pair(2, 20)));
  list.clear();
  for (auto& node : list_map) {
    list.emplace_back(node);
    node.second = node.second * 2;
  }
  ASSERT_THAT(list, ElementsAre(Pair(1, 10), Pair(2, 20)));
  list.clear();
  for (const auto& node : list_map) {
    list.emplace_back(node);
  }
  ASSERT_THAT(list, ElementsAre(Pair(1, 20), Pair(2, 40)));
}

TEST(ListMapTest, pressure_test) {
  auto started = std::chrono::high_resolution_clock::now();
  int num_entries = 0xFFFF;  // 2^16 = 65535
  ListMap<int, int> list_map;

  // fill the list_map
  for (int key = 0; key < num_entries; key++) {
    list_map.insert_or_assign(key, key);
  }

  // make sure the list_map is full
  for (int key = 0; key < num_entries; key++) {
    EXPECT_TRUE(list_map.contains(key));
  }

  // clear the entire list_map
  for (int key = 0; key < num_entries; key++) {
    auto iter = list_map.find(key);
    EXPECT_NE(iter, list_map.end());
    EXPECT_EQ(iter->second, key);
    EXPECT_TRUE(list_map.extract(key));
  }
  EXPECT_EQ(list_map.size(), 0);

  // test execution time
  auto done = std::chrono::high_resolution_clock::now();
  int execution_time = std::chrono::duration_cast<std::chrono::microseconds>(done - started).count();
  // Shouldn't be more than 1000ms
  int execution_time_per_cycle_us = 10;
  EXPECT_LT(execution_time, execution_time_per_cycle_us * num_entries);
}

}  // namespace testing
+212 −0

File added.

Preview size limit exceeded, changes collapsed.

+429 −0

File added.

Preview size limit exceeded, changes collapsed.