CDaySet

Overview

Types and Definitions

C_DaySetMeta

cMeta const C_DaySetMeta;

Stores the cMeta intance that all CDaySet instances reference.

CDaySet

struct CDaySet;
typedef struct CDaySet CDaySet;

Abstract type that represents a day set.

Functions

create

new_day_set_c

CDaySet* new_day_set_c();

Creates an empty day set.

target_day_set_c

CDaySet* target_day_set_c( cDatePeriod period );

Creates an empty day set that allocates enough memory to store days inside the period without reallocation.

overall

count_days_c

int64_t count_days_c( CDaySet const* set );

Counts the number of days that the day set contains.

Example
#include "clingo/lang/expect.h"
#include "clingo/time/CDaySet.h"

int main( void )
{
   init_tap_c_();

   CDaySet* ds = new_day_set_c();

   expect_c_( day_set_is_empty_c( ds ) );
   expect_c_( count_days_c( ds ) == 0 );

   expect_c_( set_on_day_set_c( ds, date_c( 2014, 10, 3 ) ) );

   expect_c_( count_days_c( ds) == 1 );

   release_c( ds );

   return finish_tap_c_();
}

day_set_is_empty_c

bool day_set_is_empty_c( CDaySet const* set );

Returns true if the day set contains no days, otherwise false.

day_set_period_c

bool day_set_period_c( CDaySet const* set );

Returns a period that covers all the days in the day set.

Example
#include "clingo/lang/expect.h"
#include "clingo/time/CDaySet.h"

int main( void )
{
   init_tap_c_();

   CDaySet* ds = new_day_set_c();

   set_many_on_day_set_c_( ds, date_c( 2014, 12, 24 ),
                               date_c( 2014, 12, 25 ),
                               date_c( 2014, 12, 26 ),
                               date_c( 2014, 12, 31 ) );

   cDatePeriod period = day_set_period_c( ds );
   cDatePeriod exp = make_date_period_c( 2014, 12, 24, 2014, 12, 31 );
   expect_c_( eq_date_period_c( period, exp ) );

   release_c( ds );

   return finish_tap_c_();
}

itr

first_in_day_set_c

cDate first_in_day_set_c( CDaySet const* set );

Returns the earliest date in the day set.

Example
#include "clingo/lang/expect.h"
#include "clingo/time/CDaySet.h"

int main( void )
{
   init_tap_c_();

   CDaySet* daySet = new_day_set_c();

   set_on_day_set_c( daySet, date_c( 2014, 2, 27 ) );
   set_on_day_set_c( daySet, date_c( 2014, 2, 13 ) );

   cDate exp = date_c( 2014, 2, 13 );
   expect_c_( eq_date_c( first_in_day_set_c( daySet ), exp ) );

   release_c( daySet );

   return finish_tap_c_();
}

iterate_day_set_c_

#define iterate_day_set_c_( Day, DaySet )

Macro function to define a for statement to iterate from the earliest day to the latest day in the day set.

riterate_day_set_c_

#define riterate_day_set_c_( Day, DaySet )

Macro function to define a for statement to iterate from the latest day to the earliest day in the day set.

last_in_day_set_c

cDate last_in_day_set_c( CDaySet const* set );

Returns the latest date in the day set.

Example
#include "clingo/lang/expect.h"
#include "clingo/time/CDaySet.h"

int main( void )
{
   init_tap_c_();

   CDaySet* daySet = new_day_set_c();

   set_on_day_set_c( daySet, date_c( 2014, 2, 27 ) );
   set_on_day_set_c( daySet, date_c( 2014, 2, 13 ) );

   cDate exp = date_c( 2014, 2, 27 );
   expect_c_( eq_date_c( last_in_day_set_c( daySet ), exp ) );

   release_c( daySet );

   return finish_tap_c_();
}

next_in_day_set_c

cDate next_in_day_set_c( CDaySet const* set, cDate date );

Returns the date in the day set after date or an invalid date.

Example
#include "clingo/lang/expect.h"
#include "clingo/time/CDaySet.h"

int main( void )
{
   init_tap_c_();

   CDaySet* daySet = new_day_set_c();
   cDate tmp;

   // try to get the next day in an empty day set
   tmp = next_in_day_set_c( daySet, date_c( 2014, 10, 20 ) );
   expect_c_( not date_is_valid_c( tmp ) );

   set_many_on_day_set_c_( daySet, date_c( 2014, 12, 1 ),
                                   date_c( 2014, 12, 6 ),
                                   date_c( 2014, 12, 7 ),
                                   date_c( 2014, 12, 23 ) );

   tmp = next_in_day_set_c( daySet, date_c( 2014, 10, 20 ) );
   expect_c_( eq_date_c( tmp, date_c( 2014, 12, 1 ) ) );

   tmp = next_in_day_set_c( daySet, date_c( 2014, 12, 1 ) );
   expect_c_( eq_date_c( tmp, date_c( 2014, 12, 6 ) ) );

   tmp = next_in_day_set_c( daySet, date_c( 2014, 12, 6 ) );
   expect_c_( eq_date_c( tmp, date_c( 2014, 12, 7 ) ) );

   tmp = next_in_day_set_c( daySet, date_c( 2014, 12, 7 ) );
   expect_c_( eq_date_c( tmp, date_c( 2014, 12, 23 ) ) );

   tmp = next_in_day_set_c( daySet, date_c( 2014, 12, 13 ) );
   expect_c_( eq_date_c( tmp, date_c( 2014, 12, 23 ) ) );

   tmp = next_in_day_set_c( daySet, date_c( 2014, 12, 23 ) );
   expect_c_( not date_is_valid_c( tmp ) );

   // try to get the next day after an invalid date
   tmp = next_in_day_set_c( daySet, null_date_c() );
   expect_c_( not date_is_valid_c( tmp ) );

   release_c( daySet );

   return finish_tap_c_();
}

prev_in_day_set_c

cDate prev_in_day_set_c( CDaySet const* set, cDate date );

Returns the date in the day set before date or an invalid date.

Example
#include "clingo/lang/expect.h"
#include "clingo/time/CDaySet.h"

int main( void )
{
   init_tap_c_();

   CDaySet* daySet = new_day_set_c();
   cDate tmp;

   // try to get the prev day in an empty day set
   tmp = prev_in_day_set_c( daySet, date_c( 2014, 10, 20 ) );
   expect_c_( not date_is_valid_c( tmp ) );

   set_many_on_day_set_c_( daySet, date_c( 2014, 12, 1 ),
                                   date_c( 2014, 12, 6 ),
                                   date_c( 2014, 12, 7 ),
                                   date_c( 2014, 12, 23 ) );

   tmp = prev_in_day_set_c( daySet, date_c( 2015, 12, 30 ) );
   expect_c_( eq_date_c( tmp, date_c( 2014, 12, 23 ) ) );

   tmp = prev_in_day_set_c( daySet, date_c( 2014, 12, 23 ) );
   expect_c_( eq_date_c( tmp, date_c( 2014, 12, 7 ) ) );

   tmp = prev_in_day_set_c( daySet, date_c( 2014, 12, 17 ) );
   expect_c_( eq_date_c( tmp, date_c( 2014, 12, 7 ) ) );

   tmp = prev_in_day_set_c( daySet, date_c( 2014, 12, 7 ) );
   expect_c_( eq_date_c( tmp, date_c( 2014, 12, 6 ) ) );

   tmp = prev_in_day_set_c( daySet, date_c( 2014, 12, 6 ) );
   expect_c_( eq_date_c( tmp, date_c( 2014, 12, 1 ) ) );

   tmp = prev_in_day_set_c( daySet, date_c( 2014, 12, 1 ) );
   expect_c_( not date_is_valid_c( tmp ) );

   // try to get the prev day after an invalid date
   tmp = prev_in_day_set_c( daySet, null_date_c() );
   expect_c_( not date_is_valid_c( tmp ) );

   release_c( daySet );

   return finish_tap_c_();
}

in

date_period_in_day_set_c

bool date_period_in_day_set_c( CDaySet const* set, cDatePeriod period );

Returns true if the day set contains all days of the date period, otherwise false.

Example
#include "clingo/lang/expect.h"
#include "clingo/time/CDaySet.h"

int main( void )
{
   init_tap_c_();

   CDaySet* ds = new_day_set_c();

   set_on_day_set_c( ds, date_c( 2014, 12, 24 ) );
   set_on_day_set_c( ds, date_c( 2014, 12, 25 ) );
   set_on_day_set_c( ds, date_c( 2014, 12, 26 ) );

   cDatePeriod holyNight = single_date_period_c( date_c( 2014, 12, 24 ) );
   expect_c_( date_period_in_day_set_c( ds, holyNight ) );

   cDatePeriod holiday = make_date_period_c( 2014, 12, 24, 2014, 12, 26 );
   expect_c_( date_period_in_day_set_c( ds, holiday ) );

   cDatePeriod lastDays = make_date_period_c( 2014, 12, 24, 2014, 12, 31 );
   expect_c_( not date_period_in_day_set_c( ds, lastDays ) );

   release_c( ds );

   return finish_tap_c_();
}

days_in_day_set_c

bool days_in_day_set_c( CDaySet const* set, CDaySet const* days );

Returns true if the day set contains all days, otherwise false.

Example
#include "clingo/lang/expect.h"
#include "clingo/time/CDaySet.h"

int main( void )
{
   init_tap_c_();

   CDaySet* fullSet = new_day_set_c();

   cDate holiday = date_c( 2014, 12, 1 );
   set_date_period_on_day_set_c( fullSet, sized_date_period_c( holiday, 6 ) );
   set_on_day_set_c( fullSet, date_c( 2014, 12, 9 ) );

   CDaySet* subSet = new_day_set_c();
   set_many_on_day_set_c_( subSet, date_c( 2014, 12, 1 ),
                                   date_c( 2014, 12, 3 ),
                                   date_c( 2014, 12, 5 ),
                                   date_c( 2014, 12, 9 ) );

   expect_c_( days_in_day_set_c( fullSet, subSet ) );
   expect_c_( not days_in_day_set_c( subSet, fullSet ) );

   CDaySet* emptySet = new_day_set_c();
   expect_c_( days_in_day_set_c( fullSet, emptySet ) );

   release_all_c_( fullSet, subSet, emptySet );

   return finish_tap_c_();
}

in_day_set_c

bool in_day_set_c( CDaySet const* set, cDate date );

Returns true if the day set contains the date, otherwise false.

Example
#include "clingo/lang/expect.h"
#include "clingo/time/CDaySet.h"

int main( void )
{
   init_tap_c_();

   CDaySet* daySet = new_day_set_c();

   set_on_day_set_c( daySet, date_c( 2014, 2, 27 ) );
   set_on_day_set_c( daySet, date_c( 2014, 2, 13 ) );

   expect_c_( in_day_set_c( daySet, date_c( 2014, 2, 27 ) ) );
   expect_c_( not in_day_set_c( daySet, date_c( 2014, 2, 26 ) ) );

   release_c( daySet );

   return finish_tap_c_();
}

set

set_date_period_on_day_set_c

bool set_date_period_on_day_set_c( cDaySet* set, cDatePeriod period );

Adds all days of the date period to the day set. Returns false if it fails to add the days.

Example
#include "clingo/lang/expect.h"
#include "clingo/time/CDaySet.h"

int main( void )
{
   init_tap_c_();

   CDaySet* ds = new_day_set_c();

   cDatePeriod holiday = make_date_period_c( 2014, 12, 24, 2014, 12, 26 );
   set_date_period_on_day_set_c( ds, holiday );

   expect_c_( !in_day_set_c( ds, date_c( 2014, 12, 23 ) ) );
   expect_c_(  in_day_set_c( ds, date_c( 2014, 12, 24 ) ) );
   expect_c_(  in_day_set_c( ds, date_c( 2014, 12, 25 ) ) );
   expect_c_(  in_day_set_c( ds, date_c( 2014, 12, 26 ) ) );
   expect_c_( !in_day_set_c( ds, date_c( 2014, 12, 27 ) ) );

   release_c( ds );

   return finish_tap_c_();
}

set_days_on_day_set_c

bool set_days_on_day_set_c( CDaySet* set, CDaySet const* days );

Adds all days to the day set. Returns false if it fails to add the days.

Example
#include "clingo/lang/expect.h"
#include "clingo/time/CDaySet.h"

int main( void )
{
   init_tap_c_();

   CDaySet* daySet = new_day_set_c();
   {
      cDatePeriod tmp = sized_date_period_c( date_c( 2014, 12, 1 ), 6 );
      set_date_period_on_day_set_c( daySet, tmp );
      set_on_day_set_c( daySet, date_c( 2014, 12, 9 ) );
   }

   CDaySet* salt = new_day_set_c();
   {
      set_on_day_set_c( daySet, date_c( 2014, 12, 7 ) );
      cDatePeriod tmp = sized_date_period_c( date_c( 2014, 12, 11 ), 2 );
      set_date_period_on_day_set_c( salt, tmp );
   }

   set_days_on_day_set_c( daySet, salt );
   expect_c_( count_days_c( daySet ) == 10 );
   expect_c_( in_day_set_c( daySet, date_c( 2014, 12,  7 ) ) );
   expect_c_( in_day_set_c( daySet, date_c( 2014, 12, 11 ) ) );
   expect_c_( in_day_set_c( daySet, date_c( 2014, 12, 12 ) ) );


   CDaySet* empty = new_day_set_c();

   set_days_on_day_set_c( daySet, empty );
   expect_c_( count_days_c( daySet ) == 10 );

   release_all_c_( daySet, salt, empty );

   return finish_tap_c_();
}

set_many_on_day_set_c

#define set_many_on_day_set_c_( Set, ... )                                     \
   set_many_on_day_set_c( (Set), (cDateSlice)slice_c_( cDate, __VA_ARGS__ ) )
bool set_many_on_day_set_c( CDaySet* set, cDateSlice slice );

Adds all dates from the slice to the day set. Returns false if it fails to add the dates.

set_on_day_set_c

bool set_on_day_set_c( CDaySet* set, cDate date );

Adds a date to the day set. Returns false if it fails to add the dates.

Example
#include "clingo/lang/expect.h"
#include "clingo/time/CDaySet.h"

int main( void )
{
   init_tap_c_();

   CDaySet* daySet = new_day_set_c();

   set_on_day_set_c( daySet, date_c( 2014, 2, 27 ) );

   expect_c_( in_day_set_c( daySet, date_c( 2014, 2, 27 ) ) );
   expect_c_( not day_set_is_empty_c( daySet ) );
   expect_c_( count_days_c( daySet ) == 1 );

   release_c( daySet );

   return finish_tap_c_();
}

remove

remove_date_period_from_day_set_c

void remove_date_period_from_day_set_c( CDaySet* set, cDatePeriod period );

Removes all days that the period covers from the day set.

Example
#include "clingo/lang/expect.h"
#include "clingo/time/CDaySet.h"

#define expect_eq_( A, B )                                                     \
   expect_c_( eq_date_c( (A), (B) ) )

int main( void )
{
   init_tap_c_();

   CDaySet* ds = new_day_set_c();

   cDatePeriod holiday = make_date_period_c( 2014, 12, 24, 2014, 12, 31 );
   set_date_period_on_day_set_c( ds, holiday );
   expect_c_( count_days_c( ds ) == 8 );
   expect_eq_( first_in_day_set_c( ds ), date_c( 2014, 12, 24 ) );
   expect_eq_( last_in_day_set_c( ds ), date_c( 2014, 12, 31 ) );

   cDatePeriod mid = sized_date_period_c( date_c( 2014, 12, 27 ), 2 );
   remove_date_period_from_day_set_c( ds, mid );
   expect_c_( count_days_c( ds ) == 6 );
   expect_eq_( first_in_day_set_c( ds ), date_c( 2014, 12, 24 ) );
   expect_eq_( last_in_day_set_c( ds ), date_c( 2014, 12, 31 ) );

   cDatePeriod tail = sized_date_period_c( date_c( 2014, 12, 27 ), 300 );
   remove_date_period_from_day_set_c( ds, tail );
   expect_c_( count_days_c( ds ) == 3 );
   expect_eq_( first_in_day_set_c( ds ), date_c( 2014, 12, 24 ) );
   expect_eq_( last_in_day_set_c( ds ), date_c( 2014, 12, 26 ) );

   release_c( ds );

   return finish_tap_c_();
}

remove_days_from_day_set_c

void remove_days_from_day_set_c( CDaySet* set, CDaySet const* days );

Removes all days from the day set.

Exampel
#include "clingo/lang/expect.h"
#include "clingo/time/CDaySet.h"

int main( void )
{
   init_tap_c_();

   CDaySet* daySet = new_day_set_c();
   cDatePeriod p1 = sized_date_period_c( date_c( 2014, 12, 1 ), 6 );
   set_date_period_on_day_set_c( daySet, p1 );
   set_on_day_set_c( daySet, date_c( 2014, 12, 9 ) );

   CDaySet* dirty = new_day_set_c();
   set_on_day_set_c( dirty, date_c( 2014, 12, 2 ) );
   cDatePeriod p2 = sized_date_period_c( date_c( 2014, 12, 4 ), 2 );
   set_date_period_on_day_set_c( dirty, p2 );

   remove_days_from_day_set_c( daySet, dirty );
   expect_c_( count_days_c( daySet ) == 4 );
   expect_c_( in_day_set_c( daySet, date_c( 2014, 12, 1 ) ) );
   expect_c_( in_day_set_c( daySet, date_c( 2014, 12, 3 ) ) );
   expect_c_( in_day_set_c( daySet, date_c( 2014, 12, 6 ) ) );
   expect_c_( in_day_set_c( daySet, date_c( 2014, 12, 9 ) ) );

   CDaySet* empty = new_day_set_c();

   remove_days_from_day_set_c( daySet, empty );
   expect_c_( count_days_c( daySet ) == 4 );

   release_all_c_( daySet, dirty, empty );

   return finish_tap_c_();
}

remove_from_day_set_c

void remove_from_day_set_c( CDaySet* set, cDate date );

Removes a single day from the day set.

Example
#include "clingo/lang/expect.h"
#include "clingo/time/CDaySet.h"

#define expect_eq_( A, B )                                                     \
   expect_c_( eq_date_c( (A), (B) ) )

int main( void )
{
   init_tap_c_();

   CDaySet* ds = new_day_set_c();

   cDatePeriod holiday = make_date_period_c( 2014, 12, 24, 2014, 12, 27 );
   set_date_period_on_day_set_c( ds, holiday );
   expect_c_( count_days_c( ds ) == 4 );
   expect_eq_( first_in_day_set_c( ds ), date_c( 2014, 12, 24 ) );
   expect_eq_( last_in_day_set_c( ds ), date_c( 2014, 12, 27 ) );

   remove_from_day_set_c( ds, date_c( 2014, 12, 27 ) );
   expect_c_( count_days_c( ds ) == 3 );
   expect_eq_( first_in_day_set_c( ds ), date_c( 2014, 12, 24 ) );
   expect_eq_( last_in_day_set_c( ds ), date_c( 2014, 12, 26 ) );

   remove_from_day_set_c( ds, date_c( 2014, 12, 25 ) );
   expect_c_( count_days_c( ds ) == 2 );
   expect_eq_( first_in_day_set_c( ds ), date_c( 2014, 12, 24 ) );
   expect_eq_( last_in_day_set_c( ds ), date_c( 2014, 12, 26 ) );

   release_c( ds );

   return finish_tap_c_();
}

io

write_day_set_c

#define write_day_set_c_( Rec, Set )                                           \
   write_day_set_c( (Rec), (Set), "" )
bool write_day_set_c( cRecorder rec[static 1],
                      CDaySet const* set,
                      char const fmt[static 1] );

Writes the day set in a text format into the recorder. The format can be "dbg", "zip" or a customisable list matrix.

The fix format "dbg" writes the struct layout of CDaySet into the recorder.

The fix format "zip" writes the first day in the day set and all days as a compressed bit string with the following rules: .zip

single 1 value between 0 values

write a '+'

single 0 value between 1 values

write a '_'

more then one 1 value beween 0 values

write the number of 1 followed by a 'i'

more then one 0 value beween 1 values

write the number of 0 followed by a 'z'

The customisable list matrix is the default format and it is possible the set the two charatcters that represend a not existing and existing days of a month. The default charatcter set is .1.

Example
#include "clingo/lang/expect.h"
#include "clingo/time/CDaySet.h"

TEMP_SLICE_DEF_C_(
   test,
   {
      CDaySet* set;
      char const* fmt;
      char const* exp;
   }
)
#define t_( ... ) ((test){__VA_ARGS__})

int main( void )
{
   init_tap_c_();

   cDate emFirst = date_c( 2021, 6, 11 );
   cDate emLast = date_c( 2021, 7, 11 );
   CDaySet* ds = target_day_set_c( date_period_c( emFirst, emLast ) );
   // group
   cDate groupFirst = emFirst;
   cDate groupLast = date_c( 2021, 6, 23 );
   set_date_period_on_day_set_c( ds, date_period_c( groupFirst, groupLast ) );
   // last sixteen
   set_many_on_day_set_c_( ds, date_c( 2021, 6, 26 ),
                               date_c( 2021, 6, 27 ),
                               date_c( 2021, 6, 28 ),
                               date_c( 2021, 6, 29 ) );
   // quarter-final
   set_many_on_day_set_c_( ds, date_c( 2021, 7, 2 ), date_c( 2021, 7, 3 ) );
   // semi-final
   set_many_on_day_set_c_( ds, date_c( 2021, 7, 6 ), date_c( 2021, 7, 7 ) );
   // final
   set_on_day_set_c( ds, date_c( 2021, 7, 11 ) );

   testSlice tests = slice_c_( test,
      t_( ds, "zip", "2021.06.11(13i2z4i2z2i2z2i3z+)" ),
      t_( ds, "", "2021.06:  .... .. ....1 11 11111 11 111.. 11 11.\n"
                  "2021.07:    .1 1. .11.. .1 ..... .. ..... .. ..... .\n" )
   );

   for_each_c_( test const*, t, tests )
   {
      cRecorder* rec = &recorder_c_( 1024 );

      bool res = write_day_set_c( rec, t->set, t->fmt );
      res &= recorded_is_c( rec, t->exp );

      tap_descf_c( res, "test '%s' -> \n%s", t->fmt, turn_into_cstr_c( rec ) );
   }

   release_c( ds );

   return finish_tap_c_();
}