Skip to content

Commit 81c4bc7

Browse files
authored
Merge pull request #1 from hylo-lang/partitioning
Add first implementation of positionless
2 parents 28d3eb7 + 738d1df commit 81c4bc7

14 files changed

Lines changed: 1185 additions & 72 deletions

.clang-format

Lines changed: 10 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,13 @@
11
---
2-
Language: Cpp
32
BasedOnStyle: LLVM
4-
AccessModifierOffset: -4
5-
AlignConsecutiveAssignments: false
6-
AlignConsecutiveDeclarations: false
7-
AlignOperands: false
8-
AlignTrailingComments: false
9-
AlwaysBreakTemplateDeclarations: Yes
10-
BraceWrapping:
11-
AfterCaseLabel: true
12-
AfterClass: true
13-
AfterControlStatement: true
14-
AfterEnum: true
15-
AfterFunction: true
16-
AfterNamespace: true
17-
AfterStruct: true
18-
AfterUnion: true
19-
AfterExternBlock: false
20-
BeforeCatch: true
21-
BeforeElse: true
22-
BeforeLambdaBody: true
23-
BeforeWhile: true
24-
SplitEmptyFunction: true
25-
SplitEmptyRecord: true
26-
SplitEmptyNamespace: true
27-
BreakBeforeBraces: Custom
28-
BreakConstructorInitializers: AfterColon
29-
BreakConstructorInitializersBeforeComma: false
30-
ColumnLimit: 120
31-
ConstructorInitializerAllOnOneLineOrOnePerLine: false
32-
IncludeCategories:
33-
- Regex: '^<.*'
34-
Priority: 1
35-
- Regex: '^".*'
36-
Priority: 2
37-
- Regex: '.*'
38-
Priority: 3
39-
IncludeIsMainRegex: '([-_](test|unittest))?$'
40-
IndentCaseBlocks: true
41-
IndentWidth: 4
42-
InsertNewlineAtEOF: true
43-
MacroBlockBegin: ''
44-
MacroBlockEnd: ''
45-
MaxEmptyLinesToKeep: 2
46-
NamespaceIndentation: All
3+
---
4+
Language: Cpp
5+
# Force pointers to the type for C++.
6+
DerivePointerAlignment: false
477
PointerAlignment: Left
48-
SpaceInEmptyParentheses: false
49-
SpacesInAngles: false
50-
SpacesInConditionalStatement: false
51-
SpacesInCStyleCastParentheses: false
52-
SpacesInParentheses: false
53-
TabWidth: 4
54-
...
8+
ColumnLimit: 100
9+
AllowAllArgumentsOnNextLine: true
10+
AllowAllParametersOfDeclarationOnNextLine: true
11+
BinPackArguments: false
12+
BinPackParameters: false
13+
AlignAfterOpenBracket: BlockIndent

.clangd

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
CompileFlags:
2+
Add:
3+
- "-std=c++23"
4+
- "-stdlib=libc++"
5+
CompilationDatabase: .build

.vscode/settings.json

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
{
22
"clangd.arguments": [
3-
"--compile-commands-dir=${workspaceFolder}/build",
3+
"--compile-commands-dir=${workspaceFolder}/.build",
44
"--background-index",
55
"--clang-tidy",
66
"--completion-style=detailed",
7-
"--header-insertion=iwyu",
8-
"--query-driver=/usr/bin/clang++"
9-
]
7+
"--header-insertion=iwyu"
8+
],
9+
"files.associations": {
10+
"algorithm": "cpp"
11+
}
1012
}

CMakeLists.txt

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,21 @@ CPMAddPackage(
2323
VERSION 2.4.12
2424
)
2525

26+
# Add RapidCheck for property-based testing
27+
CPMAddPackage(
28+
NAME rapidcheck
29+
GITHUB_REPOSITORY emil-e/rapidcheck
30+
GIT_TAG master
31+
OPTIONS "RC_ENABLE_TESTS OFF" "RC_ENABLE_EXAMPLES OFF"
32+
)
33+
34+
# Suppress deprecation warnings originating from RapidCheck headers only
35+
if (TARGET rapidcheck)
36+
target_compile_options(rapidcheck INTERFACE
37+
$<$<CXX_COMPILER_ID:Clang,AppleClang,GNU>:-Wno-deprecated-declarations>
38+
)
39+
endif()
40+
2641
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
2742

2843
add_library(positionless INTERFACE)
@@ -32,12 +47,12 @@ target_compile_options(positionless INTERFACE
3247
$<$<CXX_COMPILER_ID:MSVC>:/W4>
3348
)
3449

35-
add_executable(unit_tests test/partitioning_tests.cpp)
36-
target_link_libraries(unit_tests PRIVATE positionless doctest::doctest)
37-
target_compile_options(unit_tests PRIVATE
38-
$<$<CXX_COMPILER_ID:Clang,AppleClang,GNU>:-Wall -Wextra -Wpedantic>
39-
$<$<CXX_COMPILER_ID:MSVC>:/W4>
50+
add_executable(unit_tests
51+
test/tests_main.cpp
52+
test/partitioning_tests.cpp
53+
test/algorithms_tests.cpp
4054
)
55+
target_link_libraries(unit_tests PRIVATE positionless doctest::doctest rapidcheck)
4156

4257
enable_testing()
4358
add_test(NAME unit_tests COMMAND unit_tests)

README.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,25 @@ We want to have an implementation of positionless algorithms and a translation f
44

55

66
## Positionless features
7-
TODO
7+
- Construction from a range (forward access, bidirectional and random access)
8+
- `paritioning` accessors:
9+
- `parts_count() -> size_t`
10+
- `part(size_t part_index) -> std::pair<Iterator, Iterator>`
11+
- `is_part_empty(size_t part_index) -> bool`
12+
- `part_size(size_t part_index) -> size_t` -- constant time for random access, otherwise linear
13+
- growing parts (at the end):
14+
- `grow`
15+
- `grow_by`
16+
- shrinking parts (for bidirectional collections):
17+
- `shrink`
18+
- `shrink_by`
19+
- transferring elements between parts:
20+
- `transfer_to_prev`
21+
- `transfer_to_next`
22+
- creation and destruction of parts:
23+
- `add_part_end` / `add_part_begin`
24+
- `add_parts_end` / `add_parts_begin`
25+
- `remove_part`
826

927
## Translation from iterators
1028
TODO
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#pragma once
2+
3+
#include "positionless/detail/precondition.hpp"
4+
#include "positionless/partitioning.hpp"
5+
6+
#include <algorithm>
7+
#include <iterator>
8+
9+
namespace positionless {
10+
11+
/// Swaps the first element from part `i` with the first element from part `j`.
12+
///
13+
/// - Precondition: `i < p.parts_count()`
14+
/// - Precondition: `j < p.parts_count()`
15+
/// - Precondition: parts `i` and `j` are not empty
16+
template <std::forward_iterator Iterator>
17+
inline void swap_first(partitioning<Iterator>& p, size_t i, size_t j) {
18+
PRECONDITION(i < p.parts_count());
19+
PRECONDITION(j < p.parts_count());
20+
PRECONDITION(!p.is_part_empty(i));
21+
PRECONDITION(!p.is_part_empty(j));
22+
23+
auto [begin_i, end_i] = p.part(i);
24+
auto [begin_j, end_j] = p.part(j);
25+
26+
std::iter_swap(begin_i, begin_j);
27+
}
28+
29+
} // namespace positionless
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#pragma once
2+
3+
#if defined(NDEBUG)
4+
5+
#include <exception>
6+
7+
/// Assert-like precondition check that calls `std::terminate` on failure.
8+
#define PRECONDITION(expr) \
9+
do { \
10+
if (!(expr)) \
11+
std::terminate(); \
12+
} while (false)
13+
#else
14+
15+
#include <cassert>
16+
#define PRECONDITION(expr) assert(expr)
17+
18+
#endif

0 commit comments

Comments
 (0)