-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from mogproject/dev
Dev
- Loading branch information
Showing
49 changed files
with
5,100 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
... | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 () |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.