Skip to content

Commit

Permalink
style: Change style and reformat code
Browse files Browse the repository at this point in the history
  • Loading branch information
oboukli committed Jun 16, 2023
1 parent 0566dc8 commit 2b3cd24
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 28 deletions.
4 changes: 2 additions & 2 deletions .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -156,14 +156,14 @@ PenaltyIndentedWhitespace: 0
PenaltyReturnTypeOnItsOwnLine: 60
PointerAlignment: Left
PPIndentWidth: -1
QualifierAlignment: Leave
QualifierAlignment: Right
ReferenceAlignment: Pointer
ReflowComments: true
RemoveBracesLLVM: false
RemoveSemicolon: false
RequiresClausePosition: OwnLine
RequiresExpressionIndentation: OuterScope
SeparateDefinitionBlocks: Leave
SeparateDefinitionBlocks: Always
ShortNamespaceLines: 1
SortIncludes: CaseSensitive
SortJavaStaticImport: Before
Expand Down
33 changes: 17 additions & 16 deletions src/lc-lru-cache/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ class LRUCacheBase {
public:
virtual ~LRUCacheBase() {
}
virtual int get(const int key) noexcept = 0;
virtual void put(const int key, const int value) noexcept = 0;

virtual int get(int const key) noexcept = 0;
virtual void put(int const key, int const value) noexcept = 0;
};
} // namespace

Expand All @@ -29,13 +30,13 @@ struct CacheItem {
/// Least recently used (LRU) cache.
class LRUCache final : public LRUCacheBase {
public:
explicit LRUCache(const int capacity) noexcept :
explicit LRUCache(int const capacity) noexcept :
capacity_{capacity}, cache_{std::make_unique<CacheItem[]>(capacity)} {
}

~LRUCache() = default;

[[nodiscard]] int get(const int key) noexcept override {
[[nodiscard]] int get(int const key) noexcept override {
int result{-1};
std::int64_t lowest_tick_count{ticks_};

Expand Down Expand Up @@ -64,7 +65,7 @@ class LRUCache final : public LRUCacheBase {
return result;
}

void put(const int key, const int value) noexcept override {
void put(int const key, int const value) noexcept override {
assert(size_ <= capacity_);

for (int i{0}; i < size_; ++i) {
Expand Down Expand Up @@ -105,14 +106,14 @@ namespace solution2 {
/// Based on a solution by Simon Toth https://compiler-explorer.com/z/8PWETEYT8
class LRUCache final : public LRUCacheBase {
public:
explicit LRUCache(const int capacity) noexcept : capacity_{capacity} {
explicit LRUCache(int const capacity) noexcept : capacity_{capacity} {
lookup_.reserve(capacity);
}

~LRUCache() = default;

[[nodiscard]] int get(const int key) noexcept override {
const auto existing_key_iter{lookup_.find(key)};
[[nodiscard]] int get(int const key) noexcept override {
auto const existing_key_iter{lookup_.find(key)};
if (existing_key_iter == lookup_.end()) {
return -1;
}
Expand All @@ -122,25 +123,25 @@ class LRUCache final : public LRUCacheBase {
return existing_key_iter->second->second;
}

void put(const int key, const int value) noexcept override {
void put(int const key, int const value) noexcept override {
assert(size_ <= capacity_);
assert(size_ == lookup_.size());
assert(size_ == cache_.size());

// Update if key exists.
const auto iter_existing_key{lookup_.find(key)};
auto const iter_existing_key{lookup_.find(key)};
if (iter_existing_key != lookup_.end()) {
iter_existing_key->second->second = value;

return;
}

// Add new if cache is not full.
const auto cache_end{cache_.end()};
auto const cache_end{cache_.end()};
if (size_ < capacity_) {
const auto emplaced_key_iter{cache_.emplace(cache_end, key, value)};
auto const emplaced_key_iter{cache_.emplace(cache_end, key, value)};
assert(cache_.end() == cache_end);
[[maybe_unused]] const auto new_key_insert_result{
[[maybe_unused]] auto const new_key_insert_result{
lookup_.insert({key, emplaced_key_iter})};
assert(new_key_insert_result.second);
++size_;
Expand All @@ -149,11 +150,11 @@ class LRUCache final : public LRUCacheBase {
}

// Replace oldest key if cache is full.
[[maybe_unused]] const auto num_elements_removed{
[[maybe_unused]] auto const num_elements_removed{
lookup_.erase(cache_.begin()->first)};
assert(num_elements_removed == 1);
cache_.splice(cache_end, cache_, cache_.begin());
[[maybe_unused]] const auto replacement_key_insert_result{
[[maybe_unused]] auto const replacement_key_insert_result{
lookup_.insert({key, std::prev(cache_end)})};
assert(replacement_key_insert_result.second);
assert(cache_.end() == cache_end);
Expand All @@ -172,7 +173,7 @@ class LRUCache final : public LRUCacheBase {

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

{
T cache{2};
Expand Down
2 changes: 1 addition & 1 deletion src/palindrome/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include <string>

template <typename T = std::string, std::integral Tsize = typename T::size_type>
[[nodiscard]] bool is_palindrome(const T& s) noexcept {
[[nodiscard]] bool is_palindrome(T const& s) noexcept {
Tsize const len = (s.length()) / Tsize{2};
auto begin = s.cbegin();
auto end = --(s.cend());
Expand Down
2 changes: 1 addition & 1 deletion src/palindromic-number/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include <cassert>
#include <cstdlib>

[[nodiscard]] bool is_palindrome(const int n) noexcept {
[[nodiscard]] bool is_palindrome(int const n) noexcept {
if (n < 0) {
return false;
}
Expand Down
17 changes: 9 additions & 8 deletions src/sonar/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ struct Sonar {
int x;
int y;
};

std::vector<Coord> coords;
bool ping(Area area) const;
};
Expand All @@ -31,22 +32,22 @@ struct Sonar {
///
/// The area includes the borders.

int count_ships(const Sonar& sonar, const Area area) {
int count_ships(Sonar const& sonar, const Area area) {
if (!sonar.ping(area)) {
return 0;
}

const int width{std::abs(area.right - area.left)};
const int height{std::abs(area.bottom - area.top)};
int const width{std::abs(area.right - area.left)};
int const height{std::abs(area.bottom - area.top)};

if ((width + height) == 0) {
return 1;
}

const int mw0{area.left + (width / 2)};
const int mw1{mw0 + 1};
const int mh0{area.top + (height / 2)};
const int mh1{mh0 + 1};
int const mw0{area.left + (width / 2)};
int const mw1{mw0 + 1};
int const mh0{area.top + (height / 2)};
int const mh1{mh0 + 1};

return count_ships(
sonar,
Expand Down Expand Up @@ -165,7 +166,7 @@ int main() {
}

bool Sonar::ping(Area area) const {
return std::ranges::any_of(coords, [&area](const Coord& coord) {
return std::ranges::any_of(coords, [&area](Coord const& coord) {
return coord.x >= area.top && coord.x <= area.bottom
&& coord.y >= area.left && coord.y <= area.right;
});
Expand Down

0 comments on commit 2b3cd24

Please sign in to comment.