Skip to content

Instantly share code, notes, and snippets.

@brokenpylons
Last active May 21, 2019 11:04
Show Gist options
  • Select an option

  • Save brokenpylons/14a31bf046317a884b672ccb18a29439 to your computer and use it in GitHub Desktop.

Select an option

Save brokenpylons/14a31bf046317a884b672ccb18a29439 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <vector>
#include <random>
#include <algorithm>
#include <utility>
class Date {
public:
Date()
: Date(0, 0, 0)
{}
Date(int year, int month, int day)
: year_(year),
month_(month),
day_(day)
{}
int year() const
{
return year_;
}
int month() const
{
return month_;
}
int day() const
{
return day_;
}
bool operator==(const Date &other) const
{
return year_ == other.year_ &&
month_ == other.month_ &&
day_ == other.day_;
}
bool operator!=(const Date &other) const
{
return !(*this == other);
}
bool operator<(const Date &other) const
{
if (year_ < other.year_) {
return true;
}
if (year_ == other.year_ && month_ < other.month_) {
return true;
}
if (month_ == other.month_ && day_ < other.day_) {
return true;
}
return false;
}
bool operator>(const Date &other) const
{
return other < *this;
}
bool operator<=(const Date &other) const
{
return !(other < *this);
}
bool operator>=(const Date &other) const
{
return !(*this < other);
}
friend std::ostream &operator<<(std::ostream &os, const Date &date)
{
return os << date.year_ << "-" << date.month_ << "-" << date.day_;
}
private:
int year_;
int month_;
int day_;
};
std::vector<Date> random_dates(std::size_t n)
{
std::random_device r;
std::default_random_engine gen(r());
// International Fixed Calendar
std::uniform_int_distribution<int> year_dist(1900, 2020);
std::uniform_int_distribution<int> month_dist(1, 13);
std::uniform_int_distribution<int> day_dist(1, 28);
std::vector<Date> dates(n);
for (std::size_t i = 0; i < n; i++) {
dates[i] = Date(year_dist(gen), month_dist(gen), day_dist(gen));
}
return dates;
}
std::ostream &operator<<(std::ostream &os, const std::vector<Date> &dates)
{
for (const auto &date : dates) {
os << date << " ";
}
return os;
}
bool by_day(const Date &d1, const Date &d2)
{
return d1.day() < d2.day();
}
struct by_day_t {
bool operator()(const Date &d1, const Date &d2) const
{
return d1.day() < d2.day();
}
};
class day_equals_t {
public:
day_equals_t(int day)
: day(day)
{}
bool operator()(const Date &d) const
{
return d.day() == day;
}
private:
int day;
};
int main()
{
auto dates = random_dates(10);
std::cout << dates << std::endl;
std::sort(std::begin(dates), std::end(dates), std::greater<Date>());
std::cout << dates << std::endl;
std::sort(std::begin(dates), std::end(dates), std::less<Date>());
std::cout << dates << std::endl;
std::sort(std::begin(dates), std::end(dates), by_day);
std::cout << dates << std::endl;
std::sort(std::begin(dates), std::end(dates), by_day_t());
std::cout << dates << std::endl;
std::sort(std::begin(dates), std::end(dates), [](const Date &d1, const Date &d2)
{
return d1.day() < d2.day();
});
std::cout << dates << std::endl;
auto it1 = std::find_if(std::begin(dates), std::end(dates), day_equals_t(1));
if (it1 != std::end(dates)) {
std::cout << "FOUND " << *it1 << std::endl;
}
int day = 1;
auto it2 = std::find_if(std::begin(dates), std::end(dates), [day](const Date &d)
{
return d.day() == day;
});
if (it2 != std::end(dates)) {
std::cout << "FOUND " << *it2 << std::endl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment