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/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/include/doubly_linked_list.hpp b/include/doubly_linked_list.hpp index 44fb04d..97f340a 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) + 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* p, std::unique_ptr n, Args&&... args) - : value(std::forward(args)...), prev(p), next(std::move(n)) {} + 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(); } 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..8433d70 --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,23 @@ +#include "ordered_map.hpp" +#include + +int main() { + + // 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; +}