Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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()
Expand Down Expand Up @@ -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)
catch_discover_tests(tests)

add_executable(main src/main.cpp)
target_link_libraries(main PRIVATE ordered_map_lib)
28 changes: 17 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,27 @@ A C++ implementation of an ordered map data structure that maintains insertion o

```cpp
#include "ordered_map.hpp"
#include <iostream>

// Create an ordered map
OrderedMap<std::string, int> map;
int main() {

// Insert key-value pairs
map.insert("apple", 1);
map.insert("banana", 2);
map.insert("cherry", 3);
// Create an ordered map
OrderedMap<std::string, int> 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;
}
```

Expand Down
10 changes: 5 additions & 5 deletions include/doubly_linked_list.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ struct Node {
Node* prev;

template<typename U>
Node(U&& a_value, Node* a_prev, std::unique_ptr<Node> a_next)
Node(Node* a_prev, std::unique_ptr<Node> a_next, U&& a_value)
: value(std::forward<U>(a_value)), prev(a_prev), next(std::move(a_next)) {}

template <typename... Args>
Node(Node* p, std::unique_ptr<Node> n, Args&&... args)
: value(std::forward<Args>(args)...), prev(p), next(std::move(n)) {}
Node(Node* a_prev, std::unique_ptr<Node> a_next, Args&&... args)
: value(std::forward<Args>(args)...), prev(a_prev), next(std::move(a_next)) {}

};

Expand All @@ -40,13 +40,13 @@ class DoublyLinkedList {

template<typename U>
void push_back_internal(U&& value) {
auto new_node = std::make_unique<Node<T>>(std::forward<U>(value), tail, nullptr);
auto new_node = std::make_unique<Node<T>>(tail, nullptr, std::forward<U>(value));
link_new_back_node(std::move(new_node));
}

template<typename U>
void push_front_internal(U&& value) {
auto new_node = std::make_unique<Node<T>>(std::forward<U>(value), nullptr, std::move(head));
auto new_node = std::make_unique<Node<T>>(nullptr, std::move(head), std::forward<U>(value));
if (new_node->next) {
new_node->next->prev = new_node.get();
}
Expand Down
18 changes: 18 additions & 0 deletions run_main.sh
Original file line number Diff line number Diff line change
@@ -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
23 changes: 23 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include "ordered_map.hpp"
#include <iostream>

int main() {

// Create an ordered map
OrderedMap<std::string, int> 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;
}
Loading