Skip to content

Commit

Permalink
style: Change brace rules
Browse files Browse the repository at this point in the history
  • Loading branch information
oboukli committed Sep 9, 2023
1 parent bdf015c commit a32e800
Show file tree
Hide file tree
Showing 27 changed files with 314 additions and 169 deletions.
20 changes: 10 additions & 10 deletions .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -42,26 +42,26 @@ AllowShortLambdasOnASingleLine: All
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: MultiLine
AlwaysBreakBeforeMultilineStrings: true
AlwaysBreakTemplateDeclarations: Yes
AttributeMacros:
- __capability
BinPackArguments: true
BinPackParameters: true
BinPackArguments: false
BinPackParameters: false
BitFieldColonSpacing: Both
BraceWrapping:
AfterCaseLabel: false
AfterClass: false
AfterControlStatement: Never
AfterControlStatement: Always
AfterEnum: false
AfterExternBlock: false
AfterFunction: false
AfterFunction: true
AfterNamespace: false
AfterObjCDeclaration: false
AfterStruct: false
AfterUnion: false
BeforeCatch: false
BeforeElse: false
BeforeElse: true
BeforeLambdaBody: false
BeforeWhile: false
IndentBraces: false
Expand All @@ -73,7 +73,7 @@ BreakAfterJavaFieldAnnotations: false
BreakArrays: true
BreakBeforeBinaryOperators: All
BreakBeforeConceptDeclarations: Always
BreakBeforeBraces: Attach
BreakBeforeBraces: Custom
BreakBeforeInlineASMColon: OnlyMultiline
BreakBeforeTernaryOperators: false
BreakConstructorInitializers: AfterColon
Expand Down Expand Up @@ -122,9 +122,9 @@ IndentPPDirectives: None
IndentRequiresClause: true
IndentWidth: 4
IndentWrappedFunctionNames: false
InsertBraces: false
InsertBraces: true
InsertNewlineAtEOF: true
InsertTrailingCommas: None
InsertTrailingCommas: Wrapped
IntegerLiteralSeparator:
Binary: 0
Decimal: 0
Expand Down
3 changes: 2 additions & 1 deletion benchmark/first_missing_positive_benchmark.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@

TEST_CASE(
"forfun::first_missing_positive benchmarking",
"[benchmark][first_missing_positive]") {
"[benchmark][first_missing_positive]")
{
using ContainerType = std::array<int, 128>;

ankerl::nanobench::Bench()
Expand Down
18 changes: 12 additions & 6 deletions benchmark/lru_cache_benchmark.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@

template <typename T>
std::enable_if_t<std::is_base_of_v<forfun::lrucache::LRUCacheBase, T>, void>
test(int const capacity) {
test(int const capacity)
{
int volatile val{};
T cache(capacity);

for (int i{0}; i < capacity; ++i) {
for (int i{0}; i < capacity; ++i)
{
cache.put(i, i);
}

Expand All @@ -44,13 +46,16 @@ test(int const capacity) {

val = cache.get(3);

for (int i{0}; i < capacity; ++i) {
for (int i{0}; i < capacity; ++i)
{
val = cache.get(i);
}
}

TEST_CASE("forfun::lrucache benchmarking", "[benchmark][lrucache]") {
SECTION("small") {
TEST_CASE("forfun::lrucache benchmarking", "[benchmark][lrucache]")
{
SECTION("small")
{
static constexpr int const lrucache_capacity{32};

ankerl::nanobench::Bench()
Expand All @@ -73,7 +78,8 @@ TEST_CASE("forfun::lrucache benchmarking", "[benchmark][lrucache]") {
;
}

SECTION("large") {
SECTION("large")
{
static constexpr int const lrucache_capacity{128};

ankerl::nanobench::Bench()
Expand Down
9 changes: 6 additions & 3 deletions benchmark/palindrome_benchmark.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ inline constexpr std::string_view const palindrome{
"oooooooooooooooooooooooooooooooooooooooooooooooooo"
"oooooooooooooooooooooooooooooooooooooooooooooooooo"};

TEST_CASE("forfun::palindrome benchmarking", "[benchmark][palindrome]") {
SECTION("case-sensitive") {
TEST_CASE("forfun::palindrome benchmarking", "[benchmark][palindrome]")
{
SECTION("case-sensitive")
{
ankerl::nanobench::Bench()

.title("Palindrome (case-sensitive)")
Expand Down Expand Up @@ -67,7 +69,8 @@ TEST_CASE("forfun::palindrome benchmarking", "[benchmark][palindrome]") {
;
}

SECTION("case-insensitive") {
SECTION("case-insensitive")
{
ankerl::nanobench::Bench()

.title("Palindrome (case-insensitive)")
Expand Down
4 changes: 2 additions & 2 deletions benchmark/palindromic_number_benchmark.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

inline constexpr int const p{1234554321};

TEST_CASE(
"palindromic_number benchmarking", "[benchmark][palindromic_number]") {
TEST_CASE("palindromic_number benchmarking", "[benchmark][palindromic_number]")
{
static_assert(p >= 0 && p <= std::numeric_limits<decltype(p)>::max());

ankerl::nanobench::Bench()
Expand Down
32 changes: 22 additions & 10 deletions include/forfun/first_missing_positive.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,15 @@ namespace forfun::first_missing_positive {
namespace {

template <typename RandomIt>
constexpr inline void quasi_sort(RandomIt first, RandomIt const src) noexcept {
constexpr inline void quasi_sort(RandomIt first, RandomIt const src) noexcept
{
auto const n{*src};

if (n > 0) {
if (n > 0)
{
RandomIt const dest{first + std::max(0, n - 1)};
if (auto const tmp{*dest}; tmp != n) {
if (auto const tmp{*dest}; tmp != n)
{
*dest = n;
*src = tmp;

Expand All @@ -43,27 +46,36 @@ constexpr inline void quasi_sort(RandomIt first, RandomIt const src) noexcept {
} // namespace

template <typename T>
[[nodiscard]] constexpr inline int lowest_missing(T& numbers) noexcept {
[[nodiscard]] constexpr inline int lowest_missing(T& numbers) noexcept
{
auto begin{numbers.begin()};
auto end{numbers.end()};
auto max{numbers.size()};

for (auto it{begin}; it != end; ++it) {
for (auto it{begin}; it != end; ++it)
{
int const current{*it};
if (current < 1) {
if (current < 1)
{
--max;
} else if (static_cast<std::size_t>(current) > max) {
}
else if (static_cast<std::size_t>(current) > max)
{
--max;
*it = 0;
} else {
}
else
{
quasi_sort(begin, it);
}
}

int min_num{1};
auto const endIt = begin + max;
for (auto it{begin}; it != endIt; ++it) {
if (*it == min_num) {
for (auto it{begin}; it != endIt; ++it)
{
if (*it == min_num)
{
++min_num;
}
}
Expand Down
3 changes: 2 additions & 1 deletion include/forfun/lru_cache.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ namespace forfun::lrucache {

class LRUCacheBase {
public:
virtual ~LRUCacheBase() {
virtual ~LRUCacheBase()
{
}

virtual int get(int const key) noexcept = 0;
Expand Down
52 changes: 35 additions & 17 deletions include/forfun/palindrome.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,32 @@ namespace forfun::palindrome {
namespace raw {

[[nodiscard]] constexpr inline bool
is_palindrome(std::string_view const& s) noexcept {
is_palindrome(std::string_view const& s) noexcept
{
auto const end{s.length() - 1};
auto const mid{s.length() / 2};

for (std::size_t i{0}; i < mid; ++i) {
if (s[i] != s[end - i]) {
for (std::size_t i{0}; i < mid; ++i)
{
if (s[i] != s[end - i])
{
return false;
}
}

return true;
}

[[nodiscard]] inline bool is_palindrome_ci(std::string_view const& s) noexcept {
[[nodiscard]] inline bool is_palindrome_ci(std::string_view const& s) noexcept
{
auto const end{s.length() - 1};
auto const mid{s.length() / 2};

for (std::size_t i{0}; i < mid; ++i) {
for (std::size_t i{0}; i < mid; ++i)
{
if (std::tolower(static_cast<unsigned char>(s[i]))
!= std::tolower(static_cast<unsigned char>(s[end - i]))) {
!= std::tolower(static_cast<unsigned char>(s[end - i])))
{
return false;
}
}
Expand All @@ -52,12 +58,15 @@ is_palindrome(std::string_view const& s) noexcept {
namespace fast {

[[nodiscard]] constexpr inline bool
is_palindrome(std::string_view const& s) noexcept {
is_palindrome(std::string_view const& s) noexcept
{
auto upper{s.cend() - 1};
auto const mid{s.cbegin() + (s.length() / 2)};

for (auto lower{s.cbegin()}; lower < mid; ++lower) {
if ((*lower) != (*upper)) {
for (auto lower{s.cbegin()}; lower < mid; ++lower)
{
if ((*lower) != (*upper))
{
return false;
}

Expand All @@ -67,13 +76,16 @@ is_palindrome(std::string_view const& s) noexcept {
return true;
}

[[nodiscard]] inline bool is_palindrome_ci(std::string_view const& s) noexcept {
[[nodiscard]] inline bool is_palindrome_ci(std::string_view const& s) noexcept
{
auto upper{s.cend() - 1};
auto const mid{s.cbegin() + (s.length() / 2)};

for (auto lower{s.cbegin()}; lower < mid; ++lower) {
for (auto lower{s.cbegin()}; lower < mid; ++lower)
{
if (std::tolower(static_cast<unsigned char>((*lower)))
!= std::tolower(static_cast<unsigned char>((*upper)))) {
!= std::tolower(static_cast<unsigned char>((*upper))))
{
return false;
}

Expand All @@ -90,22 +102,27 @@ namespace stl_bloated {
/// Adapted from original source:
/// https://en.cppreference.com/w/cpp/algorithm/equal
[[nodiscard]] constexpr inline bool
is_palindrome(std::string_view const& s) noexcept {
is_palindrome(std::string_view const& s) noexcept
{
return std::equal(
s.cbegin(), std::next(s.cbegin(), s.size() / 2), s.crbegin());
}

namespace {
[[nodiscard]] inline bool
equal_case_insensitive(char const a, char const b) noexcept {
equal_case_insensitive(char const a, char const b) noexcept
{
return std::tolower(static_cast<unsigned char>(a))
== std::tolower(static_cast<unsigned char>(b));
}
} // namespace

[[nodiscard]] inline bool is_palindrome_ci(std::string_view const& s) noexcept {
[[nodiscard]] inline bool is_palindrome_ci(std::string_view const& s) noexcept
{
return std::equal(
s.cbegin(), std::next(s.cbegin(), s.size() / 2), s.crbegin(),
s.cbegin(),
std::next(s.cbegin(), s.size() / 2),
s.crbegin(),
equal_case_insensitive);
}

Expand All @@ -116,7 +133,8 @@ namespace stl_fast {
/// Adapted from original source:
/// https://en.cppreference.com/w/cpp/algorithm/equal
[[nodiscard]] constexpr inline bool
is_palindrome(std::string_view const& s) noexcept {
is_palindrome(std::string_view const& s) noexcept
{
auto const begin{s.cbegin()};
return std::equal(begin, begin + (s.size() / 2), s.crbegin());
}
Expand Down
6 changes: 4 additions & 2 deletions include/forfun/palindromic_number.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ namespace fast {

template <typename T>
requires std::integral<T>
[[nodiscard]] constexpr bool is_palindrome(T const n) noexcept {
[[nodiscard]] constexpr bool is_palindrome(T const n) noexcept
{
T nn{};
auto d{n};
while (d > T{0}) {
while (d > T{0})
{
nn = (nn * T{10}) + d % T{10};
d /= T{10};
}
Expand Down
6 changes: 4 additions & 2 deletions include/forfun/project_euler/p0001_multiples_of_3_or_5.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,16 @@

namespace {
[[nodiscard]] inline /* constexpr */ int
sum_2x(int const n, int const q) noexcept {
sum_2x(int const n, int const q) noexcept
{
auto const d = std::div(n, q);

return d.quot * (q + n - d.rem);
}
} // namespace

[[nodiscard]] /* constexpr */ int find_sum_mult_three_five(int n) noexcept {
[[nodiscard]] /* constexpr */ int find_sum_mult_three_five(int n) noexcept
{
--n;

return (sum_2x(n, 3) + sum_2x(n, 5) - sum_2x(n, 15)) / 2;
Expand Down
14 changes: 9 additions & 5 deletions include/forfun/sorting/insertion.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,15 @@
namespace forfun::sorting {

template <typename T, typename RandomIt>
constexpr inline void insertion_sort(
T& container, RandomIt const begin, RandomIt const end) noexcept {
if (begin != end) {
for (RandomIt i{begin + 1}; i != end; ++i) {
for (RandomIt j{i}; *j < *(j - 1) && j != begin; --j) {
constexpr inline void
insertion_sort(T& container, RandomIt const begin, RandomIt const end) noexcept
{
if (begin != end)
{
for (RandomIt i{begin + 1}; i != end; ++i)
{
for (RandomIt j{i}; *j < *(j - 1) && j != begin; --j)
{
std::iter_swap(j, j - 1);
}
}
Expand Down
Loading

0 comments on commit a32e800

Please sign in to comment.