Initial Commit
This commit is contained in:
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
build
|
||||||
10
CMakeLists.txt
Normal file
10
CMakeLists.txt
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
cmake_minimum_required(VERSION 3.10)
|
||||||
|
project(overall)
|
||||||
|
set(CMAKE_CXX_STANDARD 17)
|
||||||
|
set(CMAKE_CXX_STANDARD_REQUIRED True)
|
||||||
|
set(CMAKE_EXPORT_COMPILE_COMMANDS True)
|
||||||
|
|
||||||
|
find_package (Threads REQUIRED)
|
||||||
|
|
||||||
|
add_subdirectory(daggy)
|
||||||
|
add_subdirectory(tests)
|
||||||
5
daggy/CMakeLists.txt
Normal file
5
daggy/CMakeLists.txt
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
project(daggy)
|
||||||
|
|
||||||
|
file(GLOB SOURCES src/*.cpp)
|
||||||
|
add_library(daggy SHARED ${SOURCES})
|
||||||
|
target_include_directories(daggy PUBLIC include)
|
||||||
56
daggy/include/daggy/DAG.hpp
Normal file
56
daggy/include/daggy/DAG.hpp
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <stdexcept>
|
||||||
|
#include <unordered_map>
|
||||||
|
#include <unordered_set>
|
||||||
|
|
||||||
|
namespace daggy {
|
||||||
|
|
||||||
|
template<typename I, typename T>
|
||||||
|
class DAG {
|
||||||
|
public:
|
||||||
|
DAG() {}
|
||||||
|
|
||||||
|
void addVertex(I id, T && data);
|
||||||
|
void dropVertex(I id);
|
||||||
|
void updateVertex(I id, T && data);
|
||||||
|
|
||||||
|
void addEdge(I src, I dst);
|
||||||
|
void dropEdge(I src, I dst);
|
||||||
|
|
||||||
|
private:
|
||||||
|
struct Vertex {
|
||||||
|
T data;
|
||||||
|
|
||||||
|
// It's a bit redundant to preserve both, but
|
||||||
|
// it makes it possible to traverse both ways as
|
||||||
|
// needed.
|
||||||
|
std::unordered_set<I> parents;
|
||||||
|
std::unordered_set<I> children;
|
||||||
|
};
|
||||||
|
|
||||||
|
std::unordered_map<I, Vertex> vertices;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename I, typename T>
|
||||||
|
void DAG<I,T>::addVertex(I id, T && data) {
|
||||||
|
vertices.emplace(id, Vertex{std::move(data), {}});
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename I, typename T>
|
||||||
|
void DAG<I,T>::addEdge(I from, I to) {
|
||||||
|
auto src = vertices.find(from);
|
||||||
|
if (src == vertices.end())
|
||||||
|
throw std::runtime_error("Invalid from during edge insertion");
|
||||||
|
auto dst = vertices.find(to);
|
||||||
|
if (dst == vertices.end())
|
||||||
|
throw std::runtime_error("Invalid dst during edge insertion");
|
||||||
|
|
||||||
|
// ensure that no cycles are introduced
|
||||||
|
|
||||||
|
|
||||||
|
// Add the edge
|
||||||
|
src->second.children.insert(to);
|
||||||
|
dst->second.parents.insert(from);
|
||||||
|
}
|
||||||
|
}
|
||||||
4
daggy/src/DAG.cpp
Normal file
4
daggy/src/DAG.cpp
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
#include <daggy/DAG.hpp>
|
||||||
|
|
||||||
|
namespace daggy {
|
||||||
|
}
|
||||||
5
tests/CMakeLists.txt
Normal file
5
tests/CMakeLists.txt
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
project(tests)
|
||||||
|
file(GLOB UNIT_TESTS unit_*.cpp)
|
||||||
|
file(GLOB INTEGRATION_TESTS int_*.cpp)
|
||||||
|
add_executable(tests main.cpp ${UNIT_TESTS} ${INTEGRATION_TESTS})
|
||||||
|
target_link_libraries(tests daggy)
|
||||||
17698
tests/catch.hpp
Normal file
17698
tests/catch.hpp
Normal file
File diff suppressed because it is too large
Load Diff
9
tests/int_basic.cpp
Normal file
9
tests/int_basic.cpp
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
#include "daggy/DAG.hpp"
|
||||||
|
|
||||||
|
#include "catch.hpp"
|
||||||
|
|
||||||
|
TEST_CASE("General tests", "[general]") {
|
||||||
|
REQUIRE(1 == 1);
|
||||||
|
}
|
||||||
13
tests/main.cpp
Normal file
13
tests/main.cpp
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
#include "daggy/DAG.hpp"
|
||||||
|
|
||||||
|
#define CATCH_CONFIG_MAIN
|
||||||
|
#include "catch.hpp"
|
||||||
|
|
||||||
|
TEST_CASE("Sanity tests", "[sanity]") {
|
||||||
|
REQUIRE(1 == 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
// compile and run
|
||||||
|
// g++ -std=c++17 -o test test.cpp && ./test
|
||||||
14
tests/unit_dag.cpp
Normal file
14
tests/unit_dag.cpp
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
#include "daggy/DAG.hpp"
|
||||||
|
|
||||||
|
#include "catch.hpp"
|
||||||
|
|
||||||
|
TEST_CASE("DAG Basic Tests", "[dag]") {
|
||||||
|
daggy::DAG<int, std::string> dag;
|
||||||
|
|
||||||
|
dag.addVertex(0, "hello");
|
||||||
|
dag.addVertex(1, "goodbye");
|
||||||
|
|
||||||
|
dag.addEdge(0, 1);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user