Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
dwarfovich committed Sep 9, 2024
1 parent 1d6871c commit 90342b6
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 18 deletions.
3 changes: 2 additions & 1 deletion HW3/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cmake_minimum_required (VERSION 3.12)
cmake_minimum_required(VERSION 3.12)

option(BUILD_TESTS "BuildTests" ON)
option(BUILD_BENCHMARK "BuildBenchmark" OFF)
Expand All @@ -17,6 +17,7 @@ set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "$<0:>${CMAKE_BINARY_DIR}/lib")

add_subdirectory(allocator_driver)
add_subdirectory(allocator_lib)
add_subdirectory(cc_lib)

# deb-package.
set(CPACK_GENERATOR DEB)
Expand Down
2 changes: 1 addition & 1 deletion HW3/allocator_driver/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
add_executable(allocator_driver main.cpp)
target_link_libraries(allocator_driver PUBLIC allocator_lib)
target_link_libraries(allocator_driver PUBLIC allocator_lib cc_lib)

set_target_properties(allocator_driver
PROPERTIES
Expand Down
50 changes: 34 additions & 16 deletions HW3/allocator_driver/main.cpp
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;
}
24 changes: 24 additions & 0 deletions HW3/cc_lib/CMakeLists.txt
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)
25 changes: 25 additions & 0 deletions HW3/cc_lib/include/cc/forward_list.h
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 added HW3/cc_lib/src/lib.cpp
Empty file.

0 comments on commit 90342b6

Please sign in to comment.