clang-format and clang-tidy fixes for f8e4e3fb

This commit is contained in:
github-actions
2021-10-10 13:10:15 +00:00
parent afad8e3ac9
commit 3f9be3a99b
2 changed files with 109 additions and 101 deletions

View File

@@ -1,34 +1,36 @@
/*
* @file
* @brief Determines the date of Easter after 1582
*
* @details
* The date of Easter is determined in each year through a calculation known as "computus."
* Easter is celebrated on the first Sunday after the Paschal full moon, which is the first full moon on or after 21 March.
* Determining this date in advance requires a correlation between the lunar months and the solar year, while also accounting for the month, date, and weekday of the Julian or Gregorian calendar.
* The complexity of the algorithm arises because of the desire to associate the date of Easter with the date of the Jewish feast of Passover which, Christians believe, is when Jesus was crucified.
*
* You can read more about the date of Easter here:
* https://en.wikipedia.org/wiki/Date_of_Easter
*
* @author [AlternateWalls](https://github.com/AlternateWalls)
*/
/*
* @file
* @brief Determines the date of Easter after 1582
*
* @details
* The date of Easter is determined in each year through a calculation known as
* "computus." Easter is celebrated on the first Sunday after the Paschal full
* moon, which is the first full moon on or after 21 March. Determining this
* date in advance requires a correlation between the lunar months and the solar
* year, while also accounting for the month, date, and weekday of the Julian or
* Gregorian calendar. The complexity of the algorithm arises because of the
* desire to associate the date of Easter with the date of the Jewish feast of
* Passover which, Christians believe, is when Jesus was crucified.
*
* You can read more about the date of Easter here:
* https://en.wikipedia.org/wiki/Date_of_Easter
*
* @author [AlternateWalls](https://github.com/AlternateWalls)
*/
#include <cassert> /// for assert
#include <iostream> /// for IO operations
#include <cassert> /// for assert
#include <iostream> /// for IO operations
/*
* @brief Contains information for Easter date
*/
class EasterYearMonthDay
{
public:
* @brief Contains information for Easter date
*/
class EasterYearMonthDay {
public:
int year; ///< year Easter is on
int month; ///< month Easter is on
int day; ///< day Easter is on
EasterYearMonthDay(int newYear, int newMonth, int newDay)
{
EasterYearMonthDay(int newYear, int newMonth, int newDay) {
year = newYear; // Assigns year to class
month = newMonth;
day = newDay;
@@ -44,8 +46,7 @@ public:
*/
EasterYearMonthDay findEaster(int y) {
if (y > 1582)
{
if (y > 1582) {
int a = y % 19; // Year's location on Metonic Calendar
int b = y / 100; // Century index
int c = y % 100;
@@ -53,58 +54,55 @@ EasterYearMonthDay findEaster(int y) {
int e = b % 4; // Takes into account leap years
int f = (b + 8) / 25;
int g = (b - f + 1) / 3;
int h = (19 * a + b - d - g + 15) % 30; // Days from Mar. 21st until the full moon
int h = (19 * a + b - d - g + 15) %
30; // Days from Mar. 21st until the full moon
int i = c / 4;
int k = c % 4;
int r = (32 + 2 * e + 2 * i - h - k) % 7; // The number of days from Paschal full moon to next Sunday
int r = (32 + 2 * e + 2 * i - h - k) %
7; // The number of days from Paschal full moon to next Sunday
int m = (a + 11 * h + 22 * r) / 451;
int n = (h + r - 7 * m + 114) / 31; // Month of Easter
int p = (h + r - 7 * m + 114) % 31; // p + 1 is the day of Easter
// Assign values
EasterYearMonthDay date(y, n, p + 1); // Assign values to new instance of class
EasterYearMonthDay date(
y, n, p + 1); // Assign values to new instance of class
//Return date
// Return date
return date;
}
else
{
} else {
EasterYearMonthDay date(0, 0, 0);
//Return date
// Return date
return date;
}
}
static void test() {
/*
* @brief Inputting different year values and comparing the outputs to a
* calendar
*/
static void test()
{
/*
* @brief Inputting different year values and comparing the outputs to a calendar
*/
// 2003 | April 20th
assert(findEaster(2003).month == 4); // Should return true
assert(findEaster(2003).day == 20); // Should return true
//2003 | April 20th
assert(findEaster(2003).month == 4); //Should return true
assert(findEaster(2003).day == 20); //Should return true
// 1910 | March 27th
assert(findEaster(1910).month == 3); // Should return true
assert(findEaster(1910).day == 27); // Should return true
//1910 | March 27th
assert(findEaster(1910).month == 3); //Should return true
assert(findEaster(1910).day == 27); //Should return true
//1877 | April 1st
assert(findEaster(1877).month != 3); // Should return true
assert(findEaster(1877).day != 22); // Should return true
//1400 | Invalid date
assert(findEaster(1400).month == 0); // Should return true
assert(findEaster(1400).day == 0); // Should return true
}
//Main
int main()
{
test();
return 0;
}
// 1877 | April 1st
assert(findEaster(1877).month != 3); // Should return true
assert(findEaster(1877).day != 22); // Should return true
// 1400 | Invalid date
assert(findEaster(1400).month == 0); // Should return true
assert(findEaster(1400).day == 0); // Should return true
}
// Main
int main() {
test();
return 0;
}

View File

@@ -4,8 +4,8 @@
* data stream
*
* @details
* Given a stream of integers, the algorithm calculates the median of a fixed size
* window at the back of the stream. The leading time complexity of this
* Given a stream of integers, the algorithm calculates the median of a fixed
* size window at the back of the stream. The leading time complexity of this
* algorithm is O(log(N), and it is inspired by the known algorithm to [find
* median from (infinite) data
* stream](https://www.tutorialcup.com/interview/algorithm/find-median-from-data-stream.htm),
@@ -17,13 +17,13 @@
* pushing and popping. Each new value is pushed to the window back, while a
* value from the front of the window is popped. In addition, the algorithm
* manages a multi-value binary search tree (BST), implemented by std::multiset.
* For each new value that is inserted into the window, it is also inserted to the
* BST. When a value is popped from the window, it is also erased from the BST.
* Both insertion and erasion to/from the BST are O(logN) in time, with N the
* size of the window. Finally, the algorithm keeps a pointer to the root of the
* BST, and updates its position whenever values are inserted or erased to/from
* BST. The root of the tree is the median! Hence, median retrieval is always
* O(1)
* For each new value that is inserted into the window, it is also inserted to
* the BST. When a value is popped from the window, it is also erased from the
* BST. Both insertion and erasion to/from the BST are O(logN) in time, with N
* the size of the window. Finally, the algorithm keeps a pointer to the root of
* the BST, and updates its position whenever values are inserted or erased
* to/from BST. The root of the tree is the median! Hence, median retrieval is
* always O(1)
*
* Time complexity: O(logN). Space complexity: O(N). N - size of window
* @author [Yaniv Hollander](https://github.com/YanivHollander)
@@ -32,8 +32,8 @@
#include <cstdlib> /// for std::rand - needed in testing
#include <ctime> /// for std::time - needed in testing
#include <list> /// for std::list - used to manage sliding window
#include <set> /// for std::multiset - used to manage multi-value sorted sliding window values
#include <vector> /// for std::vector - needed in testing
#include <set> /// for std::multiset - used to manage multi-value sorted sliding window values
#include <vector> /// for std::vector - needed in testing
/**
* @namespace probability
@@ -55,7 +55,7 @@ using size_type = Window::size_type;
*/
class WindowedMedian {
const size_type _windowSize; ///< sliding window size
Window _window; ///< a sliding window of values along the stream
Window _window; ///< a sliding window of values along the stream
std::multiset<int> _sortedValues; ///< a DS to represent a balanced
/// multi-value binary search tree (BST)
std::multiset<int>::const_iterator
@@ -103,13 +103,14 @@ class WindowedMedian {
}
/// However, if the erased value is on the right branch or the median
/// itself, and the number of elements is odd, the new median will be the
/// left child of the current one
/// itself, and the number of elements is odd, the new median will be
/// the left child of the current one
else if (value >= *_itMedian && sz % 2 != 0) {
--_itMedian; // O(1) - traversing one step to the left child
}
/// Find the (first) position of the value we want to erase, and erase it
/// Find the (first) position of the value we want to erase, and erase
/// it
const auto it = _sortedValues.find(value); // O(logN)
_sortedValues.erase(it); // O(logN)
}
@@ -126,16 +127,16 @@ class WindowedMedian {
* @param value New value to insert
*/
void insert(int value) {
/// Push new value to the back of the sliding window - O(1)
_window.push_back(value);
insertToSorted(value); // Insert value to the multi-value BST - O(logN)
if (_window.size() > _windowSize) { /// If exceeding size of window, pop
/// from its left side
eraseFromSorted(_window.front()); /// Erase from the multi-value BST
/// the window left side value
_window
.pop_front(); /// Pop the left side value from the window - O(1)
if (_window.size() > _windowSize) { /// If exceeding size of window,
/// pop from its left side
eraseFromSorted(
_window.front()); /// Erase from the multi-value BST
/// the window left side value
_window.pop_front(); /// Pop the left side value from the window -
/// O(1)
}
}
@@ -170,8 +171,8 @@ class WindowedMedian {
0.5f * *next(window.begin(), window.size() / 2 - 1); /// O(N)
}
};
} /// namespace windowed_median
} /// namespace probability
} // namespace windowed_median
} // namespace probability
/**
* @brief Self-test implementations
@@ -195,32 +196,41 @@ static void test(const std::vector<int> &vals, int windowSize) {
* @returns 0 on exit
*/
int main(int argc, const char *argv[]) {
/// A few fixed test cases
test({1, 2, 3, 4, 5, 6, 7, 8, 9}, 3); /// Array of sorted values; odd window size
test({9, 8, 7, 6, 5, 4, 3, 2, 1}, 3); /// Array of sorted values - decreasing; odd window size
test({9, 8, 7, 6, 5, 4, 5, 6}, 4); /// Even window size
test({3, 3, 3, 3, 3, 3, 3, 3, 3}, 3); /// Array with repeating values
test({3, 3, 3, 3, 7, 3, 3, 3, 3}, 3); /// Array with same values except one
test({4, 3, 3, -5, -5, 1, 3, 4, 5}, 5); /// Array that includes repeating values including negatives
/// Array with large values - sum of few pairs exceeds MAX_INT. Window size is even - testing calculation of
/// average median between two middle values
test({1, 2, 3, 4, 5, 6, 7, 8, 9},
3); /// Array of sorted values; odd window size
test({9, 8, 7, 6, 5, 4, 3, 2, 1},
3); /// Array of sorted values - decreasing; odd window size
test({9, 8, 7, 6, 5, 4, 5, 6}, 4); /// Even window size
test({3, 3, 3, 3, 3, 3, 3, 3, 3}, 3); /// Array with repeating values
test({3, 3, 3, 3, 7, 3, 3, 3, 3}, 3); /// Array with same values except one
test({4, 3, 3, -5, -5, 1, 3, 4, 5},
5); /// Array that includes repeating values including negatives
/// Array with large values - sum of few pairs exceeds MAX_INT. Window size
/// is even - testing calculation of average median between two middle
/// values
test({470211272, 101027544, 1457850878, 1458777923, 2007237709, 823564440,
1115438165, 1784484492, 74243042, 114807987}, 6);
1115438165, 1784484492, 74243042, 114807987},
6);
/// Random test cases
std::srand(static_cast<unsigned int>(std::time(nullptr)));
std::vector<int> vals;
for (int i = 8; i < 100; i++) {
const auto n = 1 + std::rand() / ((RAND_MAX + 5u) / 20); /// Array size in the range [5, 20]
auto windowSize = 1 + std::rand() / ((RAND_MAX + 3u) / 10); /// Window size in the range [3, 10]
const auto n =
1 + std::rand() /
((RAND_MAX + 5u) / 20); /// Array size in the range [5, 20]
auto windowSize =
1 + std::rand() / ((RAND_MAX + 3u) /
10); /// Window size in the range [3, 10]
vals.clear();
vals.reserve(n);
for (int i = 0; i < n; i++) {
vals.push_back(rand() - RAND_MAX); /// Random array values (positive/negative)
vals.push_back(
rand() - RAND_MAX); /// Random array values (positive/negative)
}
test(vals, windowSize); /// Testing randomized test
test(vals, windowSize); /// Testing randomized test
}
return 0;
}