Skip to content

Commit b0d75ff

Browse files
authored
Merge pull request #5 from mohameds-dev/dev
Dev
2 parents 70134bb + 8c899b3 commit b0d75ff

5 files changed

Lines changed: 71 additions & 21 deletions

File tree

CMakeLists.txt

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
cmake_minimum_required(VERSION 3.10)
2-
project(ordered_map LANGUAGES CXX)
2+
project(ordered_map_lib)
33

44
set(CMAKE_CXX_STANDARD 17)
55
set(CMAKE_CXX_STANDARD_REQUIRED ON)
66

77
# Create an INTERFACE library since this is header-only
8-
add_library(ordered_map INTERFACE)
9-
target_include_directories(ordered_map INTERFACE include)
8+
add_library(ordered_map_lib INTERFACE)
9+
target_include_directories(ordered_map_lib INTERFACE include)
1010

1111
# Enable testing
1212
enable_testing()
@@ -43,9 +43,12 @@ add_executable(
4343
tests/ordered_map/find_tests.cpp
4444
tests/ordered_map/move_to_front_tests.cpp
4545
)
46-
target_link_libraries(tests PRIVATE ordered_map Catch2::Catch2WithMain)
46+
target_link_libraries(tests PRIVATE ordered_map_lib Catch2::Catch2WithMain)
4747

4848
# Register tests
4949
include(CTest)
5050
include(Catch)
51-
catch_discover_tests(tests)
51+
catch_discover_tests(tests)
52+
53+
add_executable(main src/main.cpp)
54+
target_link_libraries(main PRIVATE ordered_map_lib)

README.md

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,27 @@ A C++ implementation of an ordered map data structure that maintains insertion o
3030

3131
```cpp
3232
#include "ordered_map.hpp"
33+
#include <iostream>
3334

34-
// Create an ordered map
35-
OrderedMap<std::string, int> map;
35+
int main() {
3636

37-
// Insert key-value pairs
38-
map.insert("apple", 1);
39-
map.insert("banana", 2);
40-
map.insert("cherry", 3);
37+
// Create an ordered map
38+
OrderedMap<std::string, int> map;
4139

42-
// Access values
43-
int value = map["apple"]; // Returns 1
40+
// Insert key-value pairs
41+
map.insert("apple", 1);
42+
map.insert("banana", 2);
43+
map.insert("cherry", 3);
4444

45-
// Iterate in insertion order
46-
for (const auto& pair : map) {
47-
std::cout << pair.first << ": " << pair.second << std::endl;
45+
// Access values
46+
int value = map["apple"]; // Returns 1
47+
48+
// Iterate in insertion order
49+
for (const auto& pair : map) {
50+
std::cout << pair.first << ": " << pair.second << std::endl;
51+
}
52+
53+
return 0;
4854
}
4955
```
5056

include/doubly_linked_list.hpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ struct Node {
1010
Node* prev;
1111

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

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

2020
};
2121

@@ -40,13 +40,13 @@ class DoublyLinkedList {
4040

4141
template<typename U>
4242
void push_back_internal(U&& value) {
43-
auto new_node = std::make_unique<Node<T>>(std::forward<U>(value), tail, nullptr);
43+
auto new_node = std::make_unique<Node<T>>(tail, nullptr, std::forward<U>(value));
4444
link_new_back_node(std::move(new_node));
4545
}
4646

4747
template<typename U>
4848
void push_front_internal(U&& value) {
49-
auto new_node = std::make_unique<Node<T>>(std::forward<U>(value), nullptr, std::move(head));
49+
auto new_node = std::make_unique<Node<T>>(nullptr, std::move(head), std::forward<U>(value));
5050
if (new_node->next) {
5151
new_node->next->prev = new_node.get();
5252
}

run_main.sh

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/bin/bash
2+
3+
# Create build directory if it doesn't exist
4+
mkdir -p build
5+
6+
# Navigate to build directory
7+
cd build
8+
9+
# Run CMake if CMakeCache.txt doesn't exist
10+
if [ ! -f CMakeCache.txt ]; then
11+
cmake ..
12+
fi
13+
14+
# Build the project
15+
cmake --build .
16+
17+
# Run the main executable
18+
./main

src/main.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include "ordered_map.hpp"
2+
#include <iostream>
3+
4+
int main() {
5+
6+
// Create an ordered map
7+
OrderedMap<std::string, int> map;
8+
9+
// Insert key-value pairs
10+
map.insert("apple", 1);
11+
map.insert("banana", 2);
12+
map.insert("cherry", 3);
13+
14+
// Access values
15+
int value = map["apple"]; // Returns 1
16+
17+
// Iterate in insertion order
18+
for (const auto& pair : map) {
19+
std::cout << pair.first << ": " << pair.second << std::endl;
20+
}
21+
22+
return 0;
23+
}

0 commit comments

Comments
 (0)