From 7d70fa25612e1d6d68d97c7a2792df7a15fcc34f Mon Sep 17 00:00:00 2001 From: itsmosalah Date: Sat, 24 May 2025 15:39:22 -0500 Subject: [PATCH 1/4] Refactor: make Node constructor parameters descriptive and consistently ordered --- include/doubly_linked_list.hpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/include/doubly_linked_list.hpp b/include/doubly_linked_list.hpp index 44fb04d..08a7406 100644 --- a/include/doubly_linked_list.hpp +++ b/include/doubly_linked_list.hpp @@ -10,12 +10,12 @@ struct Node { Node* prev; template - Node(U&& a_value, Node* a_prev, std::unique_ptr a_next) - : value(std::forward(a_value)), prev(a_prev), next(std::move(a_next)) {} + Node(Node* prev_node, std::unique_ptr next_node, U&& value) + : value(std::forward(value)), prev(prev_node), next(std::move(next_node)) {} template - Node(Node* p, std::unique_ptr n, Args&&... args) - : value(std::forward(args)...), prev(p), next(std::move(n)) {} + Node(Node* prev_node, std::unique_ptr next_node, Args&&... args) + : value(std::forward(args)...), prev(prev_node), next(std::move(next_node)) {} }; From 0c18d049e22e6e67295c1c5249575285521f84b3 Mon Sep 17 00:00:00 2001 From: itsmosalah Date: Sat, 24 May 2025 16:15:14 -0500 Subject: [PATCH 2/4] Fix bug: change list code to reflect the Node constructor refactor --- include/doubly_linked_list.hpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/include/doubly_linked_list.hpp b/include/doubly_linked_list.hpp index 08a7406..97f340a 100644 --- a/include/doubly_linked_list.hpp +++ b/include/doubly_linked_list.hpp @@ -10,12 +10,12 @@ struct Node { Node* prev; template - Node(Node* prev_node, std::unique_ptr next_node, U&& value) - : value(std::forward(value)), prev(prev_node), next(std::move(next_node)) {} + Node(Node* a_prev, std::unique_ptr a_next, U&& a_value) + : value(std::forward(a_value)), prev(a_prev), next(std::move(a_next)) {} template - Node(Node* prev_node, std::unique_ptr next_node, Args&&... args) - : value(std::forward(args)...), prev(prev_node), next(std::move(next_node)) {} + Node(Node* a_prev, std::unique_ptr a_next, Args&&... args) + : value(std::forward(args)...), prev(a_prev), next(std::move(a_next)) {} }; @@ -40,13 +40,13 @@ class DoublyLinkedList { template void push_back_internal(U&& value) { - auto new_node = std::make_unique>(std::forward(value), tail, nullptr); + auto new_node = std::make_unique>(tail, nullptr, std::forward(value)); link_new_back_node(std::move(new_node)); } template void push_front_internal(U&& value) { - auto new_node = std::make_unique>(std::forward(value), nullptr, std::move(head)); + auto new_node = std::make_unique>(nullptr, std::move(head), std::forward(value)); if (new_node->next) { new_node->next->prev = new_node.get(); } From 03117e8d9d4658fdf1c950068fc433918be1ffb1 Mon Sep 17 00:00:00 2001 From: itsmosalah Date: Sat, 24 May 2025 16:21:54 -0500 Subject: [PATCH 3/4] Fix cmakelist project name, add main.cpp with example code and a .sh file to run it --- CMakeLists.txt | 13 ++++++++----- run_main.sh | 18 ++++++++++++++++++ src/main.cpp | 22 ++++++++++++++++++++++ 3 files changed, 48 insertions(+), 5 deletions(-) create mode 100755 run_main.sh create mode 100644 src/main.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 735dfac..bb058d4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,12 +1,12 @@ cmake_minimum_required(VERSION 3.10) -project(ordered_map LANGUAGES CXX) +project(ordered_map_lib) set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) # Create an INTERFACE library since this is header-only -add_library(ordered_map INTERFACE) -target_include_directories(ordered_map INTERFACE include) +add_library(ordered_map_lib INTERFACE) +target_include_directories(ordered_map_lib INTERFACE include) # Enable testing enable_testing() @@ -43,9 +43,12 @@ add_executable( tests/ordered_map/find_tests.cpp tests/ordered_map/move_to_front_tests.cpp ) -target_link_libraries(tests PRIVATE ordered_map Catch2::Catch2WithMain) +target_link_libraries(tests PRIVATE ordered_map_lib Catch2::Catch2WithMain) # Register tests include(CTest) include(Catch) -catch_discover_tests(tests) \ No newline at end of file +catch_discover_tests(tests) + +add_executable(main src/main.cpp) +target_link_libraries(main PRIVATE ordered_map_lib) \ No newline at end of file diff --git a/run_main.sh b/run_main.sh new file mode 100755 index 0000000..ce3647d --- /dev/null +++ b/run_main.sh @@ -0,0 +1,18 @@ +#!/bin/bash + +# Create build directory if it doesn't exist +mkdir -p build + +# Navigate to build directory +cd build + +# Run CMake if CMakeCache.txt doesn't exist +if [ ! -f CMakeCache.txt ]; then + cmake .. +fi + +# Build the project +cmake --build . + +# Run the main executable +./main diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..4a02267 --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,22 @@ +#include "ordered_map.hpp" +#include +int main() { + std::cout << "Hello, World!" << std::endl; + // Create an ordered map + OrderedMap map; + + // Insert key-value pairs + map.insert("apple", 1); + map.insert("banana", 2); + map.insert("cherry", 3); + + // Access values + int value = map["apple"]; // Returns 1 + + // Iterate in insertion order + for (const auto& pair : map) { + std::cout << pair.first << ": " << pair.second << std::endl; + } + + return 0; +} From 8c899b3c601d432206d07114091809c4b7e6431a Mon Sep 17 00:00:00 2001 From: itsmosalah Date: Sat, 24 May 2025 16:25:35 -0500 Subject: [PATCH 4/4] Clean the example code and copy it into the readme --- README.md | 28 +++++++++++++++++----------- src/main.cpp | 3 ++- 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index ea78ed1..2b9a671 100644 --- a/README.md +++ b/README.md @@ -30,21 +30,27 @@ A C++ implementation of an ordered map data structure that maintains insertion o ```cpp #include "ordered_map.hpp" +#include -// Create an ordered map -OrderedMap map; +int main() { -// Insert key-value pairs -map.insert("apple", 1); -map.insert("banana", 2); -map.insert("cherry", 3); + // Create an ordered map + OrderedMap map; -// Access values -int value = map["apple"]; // Returns 1 + // Insert key-value pairs + map.insert("apple", 1); + map.insert("banana", 2); + map.insert("cherry", 3); -// Iterate in insertion order -for (const auto& pair : map) { - std::cout << pair.first << ": " << pair.second << std::endl; + // Access values + int value = map["apple"]; // Returns 1 + + // Iterate in insertion order + for (const auto& pair : map) { + std::cout << pair.first << ": " << pair.second << std::endl; + } + + return 0; } ``` diff --git a/src/main.cpp b/src/main.cpp index 4a02267..8433d70 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,7 +1,8 @@ #include "ordered_map.hpp" #include + int main() { - std::cout << "Hello, World!" << std::endl; + // Create an ordered map OrderedMap map;