Skip to content

Commit

Permalink
Merge pull request #5 from mogproject/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
mogproject committed Feb 7, 2023
2 parents 7485a75 + 65a4ab0 commit fc47526
Show file tree
Hide file tree
Showing 49 changed files with 5,100 additions and 21 deletions.
77 changes: 77 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
---
Language: Cpp
# BasedOnStyle: Google
AccessModifierOffset: -1
AlignAfterOpenBracket: true
AlignConsecutiveAssignments: false
AlignEscapedNewlinesLeft: true
AlignOperands: true
AlignTrailingComments: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortBlocksOnASingleLine: true
AllowShortCaseLabelsOnASingleLine: true
AllowShortFunctionsOnASingleLine: All
AllowShortIfStatementsOnASingleLine: true
AllowShortLoopsOnASingleLine: true
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakBeforeMultilineStrings: true
AlwaysBreakTemplateDeclarations: true
BinPackArguments: true
BinPackParameters: true
BreakBeforeBinaryOperators: None
BreakBeforeBraces: Custom
BraceWrapping:
AfterClass: false
AfterControlStatement: false
AfterEnum: false
AfterFunction: false
AfterNamespace: false
AfterStruct: false
AfterUnion: false
BeforeCatch: false
BeforeElse: false
BreakBeforeTernaryOperators: true
BreakConstructorInitializersBeforeComma: false
ColumnLimit: 120
CommentPragmas: '^ IWYU pragma:'
ConstructorInitializerAllOnOneLineOrOnePerLine: true
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
Cpp11BracedListStyle: true
DerivePointerAlignment: true
DisableFormat: false
ExperimentalAutoDetectBinPacking: false
ForEachMacros: [ foreach, Q_FOREACH, BOOST_FOREACH ]
IndentCaseLabels: true
IndentWidth: 2
IndentWrappedFunctionNames: false
KeepEmptyLinesAtTheStartOfBlocks: false
MacroBlockBegin: ''
MacroBlockEnd: ''
MaxEmptyLinesToKeep: 1
NamespaceIndentation: None
ObjCBlockIndentWidth: 2
ObjCSpaceAfterProperty: false
ObjCSpaceBeforeProtocolList: false
PenaltyBreakBeforeFirstCallParameter: 1
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakString: 1000
PenaltyExcessCharacter: 1
PenaltyReturnTypeOnItsOwnLine: 200
PointerAlignment: Left
SpaceAfterCStyleCast: false
SpaceBeforeAssignmentOperators: true
SpaceBeforeParens: ControlStatements
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 2
SpacesInAngles: false
SpacesInContainerLiterals: true
SpacesInCStyleCastParentheses: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
Standard: Auto
TabWidth: 2
UseTab: Never
...

30 changes: 28 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
RM = /bin/rm
PYTHON=python3

SRC_CPP=src/main/cpp
BUILD_DIR=$(PWD)/build
TEST_BIN_DIR=$(BUILD_DIR)/test
TEST_EXEC=$(TEST_BIN_DIR)/modular_test

SRC_PY=src/main/python
TEST_PY=src/test/python
STUB_PY=src/main/stubs
Expand All @@ -9,11 +15,31 @@ PYTEST_OPTS="" # --full-trace
export PYTHONPATH=$(SRC_PY)
export MYPYPATH=$(STUB_PY)

test:
PROFILE_ON ?= false
TRACE_ON ?= false

build:
cd $(SRC_CPP) && cmake -S . -B $(BUILD_DIR)/Release -DCMAKE_BUILD_TYPE=Release -DPROFILE_ON=$(PROFILE_ON) -DTRACE_ON=$(TRACE_ON)
cd $(SRC_CPP) && cmake --build $(BUILD_DIR)/Release

test: test-cpp test-py

test-py:
mypy $(SRC_PY)
$(PYTHON) -m pytest -x --cov=$(SRC_PY) --cov-report=lcov:./coverage/lcov.info $(PYTEST_OPTS) $(TEST_PY)

test-cpp:
@echo "GTEST_FILTER: $(GTEST_FILTER)"
cd $(SRC_CPP) && cmake -DBUILD_TESTS=ON -S . -B $(BUILD_DIR)/Debug
cd $(SRC_CPP) && cmake --build $(BUILD_DIR)/Debug
$(TEST_EXEC) --output-on-failure $(GTEST_OPTS)

clean:
@echo "Cleaning..."
@$(RM) -rf build/*
@echo "Cleaning done."

lab:
jupyter-lab

.PHONY: test lab
.PHONY: build test test-cpp clean lab
20 changes: 15 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,21 @@ The code implements the algorithm described in *Simpler, Linear-Time Modular Dec

## Dependencies

- NetworkX (`pip install networkx`)
- NumPy (`pip install numpy`)
- C++
- gcc version 11 or 12
- CMake
- Python
- NetworkX (`pip install networkx`)
- NumPy (`pip install numpy`)

#### Dependencies for Unit Testing

- pytest (`pip install pytest`)
- pytest-cov (`pip install pytest-cov`)
- MyPy (`pip install mypy`)
- C++
- GoogleTest (automatically installed during the build process)
- Python
- pytest (`pip install pytest`)
- pytest-cov (`pip install pytest-cov`)
- MyPy (`pip install mypy`)

## Installation

Expand All @@ -28,6 +35,9 @@ TBD
| Task | Command | Note |
| :--- | :--- | :--- |
| Run all unit tests | `make test` | Coverage info will be created as `coverage/lcov.info`. |
| Run C++ unit tests | `make test-cpp` ||
| Run Python unit tests | `make test-py` ||
| Clean build and binary files | `make clean` ||
| Open Jupyter Lab | `make lab` | Jupyter Notebooks are in the `notebooks` directory.|

## Special Thanks
Expand Down
65 changes: 65 additions & 0 deletions src/main/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
cmake_minimum_required(VERSION 3.14)

# Avoid warning about DOWNLOAD_EXTRACT_TIMESTAMP in CMake 3.24:
if (CMAKE_VERSION VERSION_GREATER_EQUAL "3.24.0")
cmake_policy(SET CMP0135 NEW)
endif()

# options
option(BUILD_TESTS "Build test programs" OFF)

# specify the C++ standard
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED True)

# specify compilers on local machine (Mac)
if (EXISTS /opt/homebrew/bin/g++-12)
set(CMAKE_C_COMPILER /opt/homebrew/bin/gcc-12)
set(CMAKE_CXX_COMPILER /opt/homebrew/bin/g++-12)
elseif(EXISTS /opt/homebrew/bin/gcc-11)
set(CMAKE_C_COMPILER /opt/homebrew/bin/gcc-11)
set(CMAKE_CXX_COMPILER /opt/homebrew/bin/g++-11)
endif ()

message("Compiler: ${CMAKE_CXX_COMPILER}")

# source files
file(GLOB_RECURSE MAIN_SRC
ds/*.[ch]pp
modular/*.[ch]pp
readwrite/*.[ch]pp
util/*.[ch]pp
)

file(GLOB BENCH_SRC modular-bench.cpp)

include_directories(
.
)

if (CMAKE_BUILD_TYPE STREQUAL "Debug")
set (CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -fno-omit-frame-pointer -fsanitize=leak")
endif ()

# project
project(modular)
set(CMAKE_CXX_FLAGS "-Wall -funroll-loops -fno-stack-limit -O3")

# build options
if (NOT DEFINED PROFILE_ON)
set(PROFILE_ON false)
endif ()

if (NOT DEFINED TRACE_ON)
set(TRACE_ON false)
endif ()

add_compile_definitions(PROFILE_ON=${PROFILE_ON})
add_compile_definitions(TRACE_ON=${TRACE_ON})

# tests with GoogleTest
if (BUILD_TESTS)
add_subdirectory(../../test/cpp ../test)
else ()
add_executable(modular-bench ${BENCH_SRC} ${MAIN_SRC})
endif ()
132 changes: 132 additions & 0 deletions src/main/cpp/ds/graph/Graph.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
#pragma once

#include "ds/set/ArrayBitset.hpp"
#include "ds/set/SortedVectorSet.hpp"

namespace ds {
namespace graph {
/**
* @brief Graph with integer labels.
*
* @tparam Set set implementation class
*/
class Graph {
private:
/** Number of vertices. */
std::size_t n_;

/** Number of vertices. */
std::size_t m_;

/** True if the adjacency sets are in dense representation. */
bool dense_;

/** Adjacency sets. */
std::vector<basic_set<int>*> adj_;

/** Masks for removed vertices. */
basic_set<int>* removed_;

bool is_valid(int v) const { return 0 <= v && v < static_cast<int>(adj_.size()) && !removed_->get(v); }

basic_set<int>* create_set(std::size_t n, bool dense) {
if (dense) {
if (n <= 1 << 6) {
return new ArrayBitset6(n);
} else if (n <= 1 << 7) {
return new ArrayBitset7(n);
} else if (n <= 1 << 8) {
return new ArrayBitset8(n);
} else if (n <= 1 << 9) {
return new ArrayBitset9(n);
} else if (n <= 1 << 10) {
return new ArrayBitset10(n);
} else if (n <= 1 << 11) {
return new ArrayBitset11(n);
} else if (n <= 1 << 12) {
return new ArrayBitset12(n);
} else if (n <= 1 << 13) {
return new ArrayBitset13(n);
} else {
throw std::invalid_argument("Graph: n too large for dense representation");
}
} else {
return new SortedVectorSet();
}
}

public:
Graph(std::size_t n = 0, std::vector<std::pair<int, int>> const& edges = {}, bool dense = false)
: n_(n), m_(0), dense_(dense) {
for (int i = 0; i < static_cast<int>(n); ++i) adj_.push_back(create_set(n, dense));
removed_ = create_set(n, dense);
for (auto& p : edges) add_edge(p.first, p.second);
}

std::size_t number_of_nodes() const { return n_; }
std::size_t number_of_edges() const { return m_; }

int add_vertex() {
int x;
if (removed_->empty()) { // all vertices are in use
if (dense_) {
throw std::invalid_argument("add_vertex: cannot extend capacity");
} else {
adj_.push_back(create_set(n_, dense_));
// `removed_` should be SortedVectorSet so no need to replace
x = n_++;
}
} else {
// reuse one of the removed vertices
x = dense_ ? removed_->pop_front() : removed_->pop_back();
++n_;
}
return x;
}

void add_edge(int u, int v) {
if (!is_valid(u)) throw std::invalid_argument("add_edge: invalid u");
if (!is_valid(v)) throw std::invalid_argument("add_edge: invalid v");
if (u == v) throw std::invalid_argument("add_edge: loop is not allowed");

if (!has_edge(u, v)) {
adj_[u]->set(v);
adj_[v]->set(u);
++m_;
}
}

void remove_vertex(int v) {
if (!is_valid(v)) throw std::invalid_argument("remove_vertex: invalid v");

--n_;
m_ -= adj_[v]->size();

for (auto u : adj_[v]->to_vector()) adj_[u]->reset(v);
adj_[v]->clear();
removed_->set(v);
}

void remove_edge(int u, int v) {
if (!is_valid(u)) throw std::invalid_argument("remove_edge: invalid u");
if (!is_valid(v)) throw std::invalid_argument("remove_edge: invalid v");
if (!adj_[u]->get(v)) throw std::invalid_argument("remove_edge: edge does not exist");

adj_[u]->reset(v);
adj_[v]->reset(u);
--m_;
}

std::vector<int> neighbors(int v) const { return adj_[v]->to_vector(); }

int degree(int v) const { return adj_[v]->size(); }

bool has_vertex(int v) const { return is_valid(v); }

bool has_edge(int u, int v) const {
if (!is_valid(u) || !is_valid(v) || u == v) return false;
return degree(u) <= degree(v) ? adj_[u]->get(v) : adj_[v]->get(u);
}
};
} // namespace graph
} // namespace ds
Loading

0 comments on commit fc47526

Please sign in to comment.