-
Notifications
You must be signed in to change notification settings - Fork 2
Map: implement tests #15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 49 commits
Commits
Show all changes
58 commits
Select commit
Hold shift + click to select a range
b4580a2
Add wrapper for std::map
SerhiiMalyi a1b58a1
Fixes after review
SerhiiMalyi 170144a
add kmtest submodule
ElwooD07 63001e4
add cmake build configuration
ElwooD07 ad3f8e3
make kf as static library
ElwooD07 613439c
fix build with STL features
ElwooD07 4c10758
implement tests for Hex, fix compilation and linkage
ElwooD07 935a393
fix _Xout_of_range logic
belyshevdenis 5956d53
fix misspelling in Hex.h
belyshevdenis b1f8f69
fix parameters check in kf::Hex::decode
ElwooD07 31c0ec0
Merge branch 'add-cmake' of https://github.com/apriorit/kf into add-c…
ElwooD07 0379561
Merge branch 'map' into map-test
3b0039b
fix tests build and linkage
3f92843
implement tests for Map
a515987
implement tests for Map
18b7b07
add kfbenchmark, make benchmark for kf::Map
2188e4c
Merge branch 'map-test' of https://github.com/apriorit/kf into map-test
f7b57c2
Update kfbenchmark/pch.cpp
belyshevdenis 20ce1a0
Update test/MapTest.cpp
belyshevdenis 155adf6
Update include/kf/Hex.h
belyshevdenis bca43c8
Merge branch 'map-test' of https://github.com/apriorit/kf into map-test
a90f6f2
Merge branch 'map-test' of https://github.com/apriorit/kf into map-test
ab2d09a
move GetTickCount to the usage space
b4ff4ea
move GetTickCount to the usage space
2f95ae7
make kf project as header-only target
69a8388
replace gitmodules with FetchContent
2b0e120
Merge branch 'map-test' of https://github.com/apriorit/kf into map-test
5b1fefe
Merge branch 'add-cmake' into map-test
a6a669a
rename kfbenchmark to benchmark, fix its build
4003078
remove debug message
db986e7
Merge branch 'add-cmake' into map-test
7af9d5e
split generation of standalone project by CMake from simple inclusion…
belyshevdenis d97ce2c
update README.md and add it to the project root
belyshevdenis 5e6a1a0
Merge branch 'add-cmake' into map-test
belyshevdenis 4fb02b5
refactoring
belyshevdenis 5d3249f
Merge branch 'main' into map-test
belyshevdenis 2d301b4
Fix build with kf headers included
belyshevdenis 32c5548
fix MapTest and MapBenchmark
belyshevdenis 10fd12a
fix build in Release mode
belyshevdenis df4d496
move common CXX settings to the main CMakeLists.txt
belyshevdenis 8a76252
fix kf/stl/new, remove extra operators
belyshevdenis 25016e2
revert hex and test
belyshevdenis 49ba782
remove kf-benchmark
belyshevdenis f77cd4e
revert CMakeLists.txt for kf.test
belyshevdenis bcf0ae1
fix new operator
belyshevdenis 9b4c388
delete CMakeLists.txt for include
belyshevdenis db5c3ac
changes for review
belyshevdenis 4c6139f
changes for review
belyshevdenis d927ae5
move BuildHelpers out of main kf project
belyshevdenis d6f576f
fixes for review
belyshevdenis 39f0ae9
add more noexcept for EarlyAllocator
belyshevdenis 3f32e00
fixes for review
belyshevdenis ce3e1e7
fixes for review
belyshevdenis d061620
delete _Xbad_alloc implementation
belyshevdenis 77eed3a
fix _Raise_handler_impl
belyshevdenis 23ddf7d
Merge branch 'main' into map-test
belyshevdenis 6728e17
Clean up and fix optional access
SergiusTheBest 6a26aed
Rename and move map to stl directory
SergiusTheBest File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -49,4 +49,4 @@ if(${IS_TOPLEVEL_PROJECT}) | |
| endif() | ||
| endif() | ||
|
|
||
| # TODO: add install target | ||
| # TODO: add install target | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,166 @@ | ||
| #pragma once | ||
| #include <kf/MapAllocator.h> | ||
| #include <map> | ||
| #include <optional> | ||
|
|
||
| #if _ITERATOR_DEBUG_LEVEL > 0 | ||
| #error "_ITERATOR_DEBUG_LEVEL must not be greater than 0" | ||
| #endif | ||
|
|
||
| namespace kf | ||
| { | ||
| template<typename KeyType, typename ValueType, POOL_TYPE poolType, typename LessComparer = std::less<KeyType>> | ||
| class Map | ||
| { | ||
| public: | ||
| using AllocatorType = MapAllocator<std::pair<const KeyType, ValueType>, poolType>; | ||
| using MapType = std::map<KeyType, ValueType, LessComparer, AllocatorType>; | ||
| using Iterator = MapType::iterator; | ||
| using ConstIterator = MapType::const_iterator; | ||
| using SizeType = MapType::size_type; | ||
|
|
||
| public: | ||
| Map() = default; | ||
|
|
||
| Map(const Map&) = delete; | ||
| Map& operator=(const Map&) = delete; | ||
|
|
||
| Map(Map&& other) = default; | ||
| Map& operator=(Map&& other) = default; | ||
|
|
||
| [[nodiscard]] NTSTATUS initialize() | ||
| { | ||
| AllocatorType allocator; | ||
| if (!allocator.initialize()) | ||
| { | ||
| return STATUS_INSUFFICIENT_RESOURCES; | ||
| } | ||
|
|
||
| // Prepare memory for head node | ||
| if (!allocator.prepareMemory(kNodeSize)) | ||
| { | ||
| return STATUS_INSUFFICIENT_RESOURCES; | ||
| } | ||
|
|
||
| m_internalMap = make_unique<MapType, poolType>(allocator); | ||
| if (!m_internalMap) | ||
| { | ||
| return STATUS_INSUFFICIENT_RESOURCES; | ||
| } | ||
|
|
||
| return STATUS_SUCCESS; | ||
| } | ||
|
|
||
| std::optional<std::reference_wrapper<ValueType>> operator[](KeyType&& key) | ||
| { | ||
| if (!m_internalMap->get_allocator().prepareMemory(kNodeSize)) | ||
| { | ||
| return {}; | ||
| } | ||
|
|
||
| return std::ref((*m_internalMap)[std::forward<KeyType>(key)]); | ||
| } | ||
|
|
||
| template <class... ValuesType> | ||
| std::optional<std::pair<Iterator, bool>> emplace(ValuesType&&... values) | ||
| { | ||
| if (!m_internalMap->get_allocator().prepareMemory(kNodeSize)) | ||
| { | ||
| return {}; | ||
| } | ||
|
|
||
| return m_internalMap->emplace(std::forward<ValuesType>(values)...); | ||
| } | ||
|
|
||
| [[nodiscard]] Iterator find(const KeyType& key) | ||
| { | ||
| return m_internalMap->find(key); | ||
| } | ||
|
|
||
| [[nodiscard]] ConstIterator find(const KeyType& key) const | ||
| { | ||
| return m_internalMap->find(key); | ||
| } | ||
|
|
||
| #if _HAS_CXX20 | ||
| [[nodiscard]] bool contains(const KeyType& key) const | ||
| { | ||
| return m_internalMap->contains(key); | ||
| } | ||
| #endif | ||
belyshevdenis marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| void clear() noexcept | ||
| { | ||
| m_internalMap->clear(); | ||
| } | ||
|
|
||
| Iterator erase(ConstIterator where) noexcept | ||
| { | ||
| return m_internalMap->erase(where); | ||
| } | ||
|
|
||
| Iterator erase(ConstIterator first, ConstIterator last) noexcept | ||
| { | ||
| return m_internalMap->erase(first, last); | ||
| } | ||
|
|
||
| SizeType erase(const KeyType& key) noexcept | ||
| { | ||
| return m_internalMap->erase(key); | ||
| } | ||
|
|
||
| [[nodiscard]] Iterator begin() noexcept | ||
| { | ||
| return m_internalMap->begin(); | ||
| } | ||
|
|
||
| [[nodiscard]] ConstIterator begin() const noexcept | ||
| { | ||
| return m_internalMap->begin(); | ||
| } | ||
|
|
||
| [[nodiscard]] Iterator end() noexcept | ||
| { | ||
| return m_internalMap->end(); | ||
| } | ||
|
|
||
| [[nodiscard]] ConstIterator end() const noexcept | ||
| { | ||
| return m_internalMap->end(); | ||
| } | ||
|
|
||
| [[nodiscard]] Iterator upper_bound(const KeyType& key) | ||
| { | ||
| return m_internalMap->upper_bound(key); | ||
| } | ||
|
|
||
| [[nodiscard]] ConstIterator upper_bound(const KeyType& key) const | ||
| { | ||
| return m_internalMap->upper_bound(key); | ||
| } | ||
|
|
||
| [[nodiscard]] Iterator lower_bound(const KeyType& key) | ||
| { | ||
| return m_internalMap->lower_bound(key); | ||
| } | ||
|
|
||
| [[nodiscard]] ConstIterator lower_bound(const KeyType& key) const | ||
| { | ||
| return m_internalMap->lower_bound(key); | ||
| } | ||
|
|
||
| bool empty() const noexcept | ||
| { | ||
| return m_internalMap->empty(); | ||
| } | ||
|
|
||
| SizeType size() const noexcept | ||
| { | ||
| return m_internalMap->size(); | ||
| } | ||
|
|
||
| private: | ||
| static constexpr size_t kNodeSize = sizeof(MapType::_Alnode_traits::value_type); | ||
| std::unique_ptr<MapType> m_internalMap; | ||
| }; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| #pragma once | ||
| #include <kf/stl/memory> | ||
|
|
||
| namespace kf | ||
| { | ||
| struct AllocatorState | ||
| { | ||
| std::unique_ptr<char[]> buffer; | ||
| size_t size; | ||
| }; | ||
|
|
||
| template<typename T, POOL_TYPE poolType> | ||
| class MapAllocator | ||
| { | ||
| public: | ||
| using value_type = T; | ||
|
|
||
| MapAllocator() = default; | ||
|
|
||
| // Needed because allocator has extra parameter for POOL_TYPE | ||
| template <typename U> | ||
| struct rebind | ||
| { | ||
| using other = MapAllocator<U, poolType>; | ||
| }; | ||
|
|
||
| friend class MapAllocator; | ||
|
|
||
| template<typename U> | ||
| MapAllocator(const MapAllocator<U, poolType>& other) | ||
| : m_state(other.m_state) | ||
| { | ||
| } | ||
|
|
||
| [[nodiscard]] bool initialize() | ||
| { | ||
| m_state = make_shared<AllocatorState, poolType>(); | ||
| return m_state != nullptr; | ||
| } | ||
|
|
||
| T* allocate(std::size_t n) | ||
| { | ||
| if (!m_state || !m_state->buffer) | ||
| { | ||
| return nullptr; | ||
| } | ||
|
|
||
| if (m_state->size < n) | ||
| { | ||
| return nullptr; | ||
| } | ||
|
|
||
| auto buffer = reinterpret_cast<T*>(m_state->buffer.release()); | ||
| m_state->size = 0; | ||
| return buffer; | ||
| } | ||
|
|
||
| void deallocate(T*, std::size_t) | ||
| { | ||
| m_state->buffer.reset(); | ||
| m_state->size = 0; | ||
| } | ||
|
|
||
| [[nodiscard]] bool prepareMemory(std::size_t size) | ||
| { | ||
| if (!m_state) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| if (m_state->buffer) | ||
| { | ||
| return true; | ||
| } | ||
|
|
||
| m_state->buffer.reset(new(poolType) char[size]); | ||
| if (!m_state->buffer) | ||
| { | ||
| return false; | ||
| } | ||
| m_state->size = size; | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| private: | ||
| std::shared_ptr<AllocatorState> m_state; | ||
| }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.