Skip to content

Commit

Permalink
Add 'ThirdParty/cpp-lru-cache/' from commit 'de1c4a03569bf3bd540e7f55…
Browse files Browse the repository at this point in the history
…ab5c2961411dbe22'

git-subtree-dir: ThirdParty/cpp-lru-cache
git-subtree-mainline: d774da7
git-subtree-split: de1c4a0
  • Loading branch information
youbetterdont committed Mar 8, 2020
2 parents d774da7 + de1c4a0 commit 1f9346e
Show file tree
Hide file tree
Showing 8 changed files with 261 additions and 0 deletions.
11 changes: 11 additions & 0 deletions ThirdParty/cpp-lru-cache/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
CMakeCache.txt
CMakeFiles
Makefile
install_manifest.txt
cmake_install.cmake

nbproject
build

cpp-lru-cache-test
obj-x86_64-linux-gnu/
13 changes: 13 additions & 0 deletions ThirdParty/cpp-lru-cache/.travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
language: cpp

compiler:
- gcc

script:
- mkdir build
- cd build
- cmake ..
- make check

notifications:
email: false
43 changes: 43 additions & 0 deletions ThirdParty/cpp-lru-cache/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
cmake_minimum_required(VERSION 2.8)

project(CPP-LRU_CACHE)

find_package(Threads REQUIRED)

set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
set(EXT_PROJECTS_DIR ${PROJECT_SOURCE_DIR}/ext)
add_subdirectory(${EXT_PROJECTS_DIR}/gtest)

enable_testing()

include_directories(
${GTEST_INCLUDE_DIRS}
${PROJECT_SOURCE_DIR}/include)

add_executable(
cpp-lru-cache-test
src/test)

add_dependencies(cpp-lru-cache-test googletest)

target_link_libraries(
cpp-lru-cache-test
${GTEST_LIBS_DIR}/libgtest.a
${GTEST_LIBS_DIR}/libgtest_main.a
${CMAKE_THREAD_LIBS_INIT})

set_target_properties(cpp-lru-cache-test PROPERTIES
PREFIX ""
SUFFIX ""
COMPILE_FLAGS "-std=c++0x -W -Wall -pedantic")

set(CMAKE_CXX_FLAGS_DEBUG "-O0 -g3")
set(CMAKE_CSS_FLAGS_RELEASE "-03 -g")

add_test(
NAME cpp-lru-cache-test
COMMAND cpp-lru-cache-test
)

add_custom_target(check COMMAND ${CMAKE_CTEST_COMMAND} --verbose
DEPENDS cpp-lru-cache-test)
27 changes: 27 additions & 0 deletions ThirdParty/cpp-lru-cache/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
Copyright (c) 2014, lamerman
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

* Neither the name of lamerman nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 changes: 29 additions & 0 deletions ThirdParty/cpp-lru-cache/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
cpp-lru-cache
=============

Simple and reliable LRU (Least Recently Used) cache for c++ based on hashmap and linkedlist. The library is header only, simple test and example are included.
It includes standard components and very little own logics that guarantees reliability.

Example:

```
/**Creates cache with maximum size of three. When the
size in achieved every next element will replace the
least recently used one */
cache::lru_cache<std::string, std::string> cache(3);
cache.put("one", "one");
cache.put("two", "two");
const std::string& from_cache = cache.get("two")
```

How to run tests:

```
mkdir build
cd build
cmake ..
make check
```
21 changes: 21 additions & 0 deletions ThirdParty/cpp-lru-cache/ext/gtest/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
cmake_minimum_required(VERSION 2.8)
project(gtest_builder C CXX)
include(ExternalProject)

ExternalProject_Add(googletest
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG release-1.7.0
CMAKE_ARGS -DCMAKE_ARCHIVE_OUTPUT_DIRECTORY_DEBUG:PATH=DebugLibs
-DCMAKE_ARCHIVE_OUTPUT_DIRECTORY_RELEASE:PATH=ReleaseLibs
-DCMAKE_CXX_FLAGS=${MSVC_COMPILER_DEFS}
-Dgtest_force_shared_crt=ON
PREFIX "${CMAKE_CURRENT_BINARY_DIR}"
INSTALL_COMMAND ""
UPDATE_COMMAND ""
)

ExternalProject_Get_Property(googletest source_dir)
set(GTEST_INCLUDE_DIRS ${source_dir}/include PARENT_SCOPE)

ExternalProject_Get_Property(googletest binary_dir)
set(GTEST_LIBS_DIR ${binary_dir} PARENT_SCOPE)
72 changes: 72 additions & 0 deletions ThirdParty/cpp-lru-cache/include/lrucache.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* File: lrucache.hpp
* Author: Alexander Ponomarev
*
* Created on June 20, 2013, 5:09 PM
*/

#ifndef _LRUCACHE_HPP_INCLUDED_
#define _LRUCACHE_HPP_INCLUDED_

#include <unordered_map>
#include <list>
#include <cstddef>
#include <stdexcept>

namespace cache {

template<typename key_t, typename value_t>
class lru_cache {
public:
typedef typename std::pair<key_t, value_t> key_value_pair_t;
typedef typename std::list<key_value_pair_t>::iterator list_iterator_t;

lru_cache(size_t max_size) :
_max_size(max_size) {
}

void put(const key_t& key, const value_t& value) {
auto it = _cache_items_map.find(key);
_cache_items_list.push_front(key_value_pair_t(key, value));
if (it != _cache_items_map.end()) {
_cache_items_list.erase(it->second);
_cache_items_map.erase(it);
}
_cache_items_map[key] = _cache_items_list.begin();

if (_cache_items_map.size() > _max_size) {
auto last = _cache_items_list.end();
last--;
_cache_items_map.erase(last->first);
_cache_items_list.pop_back();
}
}

const value_t& get(const key_t& key) {
auto it = _cache_items_map.find(key);
if (it == _cache_items_map.end()) {
throw std::range_error("There is no such key in cache");
} else {
_cache_items_list.splice(_cache_items_list.begin(), _cache_items_list, it->second);
return it->second->second;
}
}

bool exists(const key_t& key) const {
return _cache_items_map.find(key) != _cache_items_map.end();
}

size_t size() const {
return _cache_items_map.size();
}

private:
std::list<key_value_pair_t> _cache_items_list;
std::unordered_map<key_t, list_iterator_t> _cache_items_map;
size_t _max_size;
};

} // namespace cache

#endif /* _LRUCACHE_HPP_INCLUDED_ */

45 changes: 45 additions & 0 deletions ThirdParty/cpp-lru-cache/src/test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include "lrucache.hpp"
#include "gtest/gtest.h"

const int NUM_OF_TEST1_RECORDS = 100;
const int NUM_OF_TEST2_RECORDS = 100;
const int TEST2_CACHE_CAPACITY = 50;

TEST(CacheTest, SimplePut) {
cache::lru_cache<int, int> cache_lru(1);
cache_lru.put(7, 777);
EXPECT_TRUE(cache_lru.exists(7));
EXPECT_EQ(777, cache_lru.get(7));
EXPECT_EQ(1, cache_lru.size());
}

TEST(CacheTest, MissingValue) {
cache::lru_cache<int, int> cache_lru(1);
EXPECT_THROW(cache_lru.get(7), std::range_error);
}

TEST(CacheTest1, KeepsAllValuesWithinCapacity) {
cache::lru_cache<int, int> cache_lru(TEST2_CACHE_CAPACITY);

for (int i = 0; i < NUM_OF_TEST2_RECORDS; ++i) {
cache_lru.put(i, i);
}

for (int i = 0; i < NUM_OF_TEST2_RECORDS - TEST2_CACHE_CAPACITY; ++i) {
EXPECT_FALSE(cache_lru.exists(i));
}

for (int i = NUM_OF_TEST2_RECORDS - TEST2_CACHE_CAPACITY; i < NUM_OF_TEST2_RECORDS; ++i) {
EXPECT_TRUE(cache_lru.exists(i));
EXPECT_EQ(i, cache_lru.get(i));
}

size_t size = cache_lru.size();
EXPECT_EQ(TEST2_CACHE_CAPACITY, size);
}

int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
int ret = RUN_ALL_TESTS();
return ret;
}

0 comments on commit 1f9346e

Please sign in to comment.