Skip to content
Merged
Show file tree
Hide file tree
Changes from 10 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
61 changes: 10 additions & 51 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -1,54 +1,13 @@
---
Language: Cpp
BasedOnStyle: LLVM
AccessModifierOffset: -4
AlignConsecutiveAssignments: false
AlignConsecutiveDeclarations: false
AlignOperands: false
AlignTrailingComments: false
AlwaysBreakTemplateDeclarations: Yes
BraceWrapping:
AfterCaseLabel: true
AfterClass: true
AfterControlStatement: true
AfterEnum: true
AfterFunction: true
AfterNamespace: true
AfterStruct: true
AfterUnion: true
AfterExternBlock: false
BeforeCatch: true
BeforeElse: true
BeforeLambdaBody: true
BeforeWhile: true
SplitEmptyFunction: true
SplitEmptyRecord: true
SplitEmptyNamespace: true
BreakBeforeBraces: Custom
BreakConstructorInitializers: AfterColon
BreakConstructorInitializersBeforeComma: false
ColumnLimit: 120
ConstructorInitializerAllOnOneLineOrOnePerLine: false
IncludeCategories:
- Regex: '^<.*'
Priority: 1
- Regex: '^".*'
Priority: 2
- Regex: '.*'
Priority: 3
IncludeIsMainRegex: '([-_](test|unittest))?$'
IndentCaseBlocks: true
IndentWidth: 4
InsertNewlineAtEOF: true
MacroBlockBegin: ''
MacroBlockEnd: ''
MaxEmptyLinesToKeep: 2
NamespaceIndentation: All
---
Language: Cpp
# Force pointers to the type for C++.
DerivePointerAlignment: false
PointerAlignment: Left
SpaceInEmptyParentheses: false
SpacesInAngles: false
SpacesInConditionalStatement: false
SpacesInCStyleCastParentheses: false
SpacesInParentheses: false
TabWidth: 4
...
ColumnLimit: 100
AllowAllArgumentsOnNextLine: true
AllowAllParametersOfDeclarationOnNextLine: true
BinPackArguments: false
BinPackParameters: false
AlignAfterOpenBracket: BlockIndent
5 changes: 5 additions & 0 deletions .clangd
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CompileFlags:
Add:
- "-std=c++23"
- "-stdlib=libc++"
CompilationDatabase: .build
10 changes: 6 additions & 4 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
{
"clangd.arguments": [
"--compile-commands-dir=${workspaceFolder}/build",
"--compile-commands-dir=${workspaceFolder}/.build",
"--background-index",
"--clang-tidy",
"--completion-style=detailed",
"--header-insertion=iwyu",
"--query-driver=/usr/bin/clang++"
]
"--header-insertion=iwyu"
],
"files.associations": {
"algorithm": "cpp"
}
}
25 changes: 20 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,21 @@ CPMAddPackage(
VERSION 2.4.12
)

# Add RapidCheck for property-based testing
CPMAddPackage(
NAME rapidcheck
GITHUB_REPOSITORY emil-e/rapidcheck
GIT_TAG master
OPTIONS "RC_ENABLE_TESTS OFF" "RC_ENABLE_EXAMPLES OFF"
)

# Suppress deprecation warnings originating from RapidCheck headers only
if (TARGET rapidcheck)
target_compile_options(rapidcheck INTERFACE
$<$<CXX_COMPILER_ID:Clang,AppleClang,GNU>:-Wno-deprecated-declarations>
)
endif()

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

add_library(positionless INTERFACE)
Expand All @@ -32,12 +47,12 @@ target_compile_options(positionless INTERFACE
$<$<CXX_COMPILER_ID:MSVC>:/W4>
)

add_executable(unit_tests test/partitioning_tests.cpp)
target_link_libraries(unit_tests PRIVATE positionless doctest::doctest)
target_compile_options(unit_tests PRIVATE
$<$<CXX_COMPILER_ID:Clang,AppleClang,GNU>:-Wall -Wextra -Wpedantic>
$<$<CXX_COMPILER_ID:MSVC>:/W4>
add_executable(unit_tests
test/tests_main.cpp
test/partitioning_tests.cpp
test/algorithms_tests.cpp
)
target_link_libraries(unit_tests PRIVATE positionless doctest::doctest rapidcheck)

enable_testing()
add_test(NAME unit_tests COMMAND unit_tests)
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,25 @@ We want to have an implementation of positionless algorithms and a translation f


## Positionless features
TODO
- Construction from a range (forward access, bidirectional and random access)
- `paritioning` accessors:
- `parts_count() -> size_t`
- `part(size_t part_index) -> std::pair<Iterator, Iterator>`
- `is_part_empty(size_t part_index) -> bool`
- `part_size(size_t part_index) -> size_t` -- constant time for random access, otherwise linear
- growing parts (at the end):
- `grow`
- `grow_by`
- shrinking parts (for bidirectional collections):
- `shrink`
- `shrink_by`
- transferring elements between parts:
- `transfer_to_prev`
- `transfer_to_next`
- creation and destruction of parts:
- `add_part_end` / `add_part_begin`
- `add_parts_end` / `add_parts_begin`
- `remove_part`

## Translation from iterators
TODO
Expand Down
29 changes: 29 additions & 0 deletions include/positionless/algorithms.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#pragma once

#include "positionless/detail/precondition.hpp"
#include "positionless/partitioning.hpp"

#include <algorithm>
#include <iterator>

namespace positionless {

/// Swaps the first element from part `i` with the first element from part `j`.
///
/// - Precondition: `i < parts_count()`
/// - Precondition: `j < parts_count()`
/// - Precondition: parts `i` and `j` are not empty
template <std::forward_iterator Iterator>
inline void swap_first(partitioning<Iterator>& p, size_t i, size_t j) {
PRECONDITION(i < p.parts_count());
PRECONDITION(j < p.parts_count());
PRECONDITION(!p.is_part_empty(i));
PRECONDITION(!p.is_part_empty(j));

auto [begin_i, end_i] = p.part(i);
auto [begin_j, end_j] = p.part(j);

std::iter_swap(begin_i, begin_j);
}

} // namespace positionless
16 changes: 16 additions & 0 deletions include/positionless/detail/precondition.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#pragma once

#if defined(NDEBUG)

/// Assert-like precondition check that calls `std::terminate` on failure.
#define PRECONDITION(expr) \
do { \
if (!(expr)) \
std::terminate(); \
} while (false)
#else

#include <cassert>
#define PRECONDITION(expr) assert(expr)

#endif
Loading