-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1d6871c
commit 90342b6
Showing
6 changed files
with
86 additions
and
18 deletions.
There are no files selected for viewing
This file contains 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 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 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 |
---|---|---|
@@ -1,30 +1,48 @@ | ||
#include "allocator/memory_manager_allocator.h" | ||
#include "cc/forward_list.h" | ||
|
||
#include <iostream> | ||
#include <vector> | ||
#include <map> | ||
#include <unordered_map> | ||
|
||
template<typename T> | ||
T factorial(T n){ | ||
if(n < 0){ | ||
return 0; | ||
} | ||
|
||
if(n == 0 || n == 1){ | ||
return 1; | ||
} | ||
|
||
T f = 1; | ||
for(T i = 2; i <= n; ++i) { | ||
f *= i; | ||
} | ||
|
||
return f; | ||
} | ||
int main() | ||
{ | ||
//MemoryManagerAllocator<int> mma; | ||
//std::vector<int, MemoryManagerAllocator<int>> v; | ||
//v.push_back(1); | ||
//mma.getMemoryManager().dump(); | ||
|
||
using MapValueType = std::unordered_map<std::size_t, std::size_t>::value_type; | ||
MemoryManagerAllocator<MapValueType> mma; | ||
std::unordered_map<std::size_t, std::size_t, std::hash<std::size_t>, std::equal_to<std::size_t>, decltype(mma)> map; | ||
for (std::size_t i = 0; i < 10; ++i){ | ||
map[i] = i; | ||
std::cout << map[i] << '\n'; | ||
std::map<int, int> map; | ||
for (int i = 0; i < 10; ++i){ | ||
map[i]= factorial(i); | ||
} | ||
std::cout << "Map with std::allocator:\n"; | ||
for (const auto& [key, value] : map) { | ||
std::cout << key << ": " << value << '\n'; | ||
} | ||
|
||
for (std::size_t i = 0; i < 10; ++i) { | ||
std::cout << map[i] << '\n'; | ||
using ValueType = std::map<int, int>::value_type; | ||
using MMA = MemoryManagerAllocator<ValueType>; | ||
std::map<int, int, std::less<>, MMA> mmamap; | ||
for (int i = 0; i < 10; ++i) { | ||
mmamap[i] = factorial(i); | ||
} | ||
std::cout << "Map with custom allocator:\n"; | ||
for (const auto& [key, value] : mmamap) { | ||
std::cout << key << ": " << value << '\n'; | ||
} | ||
//std::cout << map[0] << '\n'; | ||
//map.get_allocator().getMemoryManager().dump(); | ||
|
||
return 0; | ||
} |
This file contains 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,24 @@ | ||
add_library(cc_lib STATIC | ||
"src/lib.cpp" | ||
"include/cc/forward_list.h" | ||
) | ||
|
||
target_include_directories(cc_lib | ||
PUBLIC include | ||
PRIVATE src include/cc) | ||
|
||
set_target_properties(cc_lib PROPERTIES LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib") | ||
target_compile_features(cc_lib PUBLIC cxx_std_20) | ||
if(MSVC) | ||
# Force to always compile with W4 | ||
if(CMAKE_CXX_FLAGS MATCHES "/W[0-4]") | ||
string(REGEX REPLACE "/W[0-4]" "/W4" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") | ||
else() | ||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4") | ||
endif() | ||
elseif(CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUCXX) | ||
# Update if necessary | ||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -pedantic") | ||
endif() | ||
|
||
#target_link_libraries(allocator_lib PUBLIC GTest::gtest_main) |
This file contains 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,25 @@ | ||
#pragma once | ||
|
||
#include <memory> | ||
#include <cinttypes> | ||
|
||
namespace cc { | ||
|
||
template<typename T, typename Allocator = std::allocator<T>> | ||
class ForwardList | ||
{ | ||
public: | ||
private: // types | ||
struct Node | ||
{ | ||
T data; | ||
std::unique_ptr<Node> nextNode = nullptr; | ||
}; | ||
|
||
private: // data | ||
Allocator allocator; | ||
std::unique_ptr<Node> firstNode = nullptr; | ||
std::size_t size = 0; | ||
}; | ||
|
||
} // namespace cc |
Empty file.