BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bdlt_dayofweekset

Detailed Description

Outline

Purpose

Provide an ordered set of (unique) bdlt::DayOfWeek::Enum values.

Classes

See also
bdlt_dayofweek

Description

This component implements an efficient value-semantic, ordered set class, bdlt::DayOfWeekSet, for elements of the bdlt::DayOfWeek::Enum enumeration. As there are only seven possible element values, asymptotic performance characterization is not appropriate; all operations implicitly run in constant time and provide the no-throw guarantee.

Supplementary Overloaded Operators

In addition to the standard value-semantic operators =, ==, !=, and <<, the following canonical set of binary and unary (free) operators are defined on bdlt::DayOfWeekSet objects:

set S: { e_MON, e_TUE, e_WED }
set T: { e_MON, e_WED, e_FRI }
Union: S | T { e_MON, e_TUE, e_WED, e_FRI }
Intersection: S & T { e_MON, e_WED }
Exclusive Or: S ^ T { e_TUE, e_FRI }
Subtraction: S - T { e_TUE }
T - S { e_FRI }
Unary Negation: ~S { e_SUN, e_THU, e_FRI, e_SAT }
~T { e_SUN, e_TUE, e_THU, e_SAT }

The corresponding assignment (member) operators |= , &=, ^=, and -= (but not ~=) are also provided.

Usage

This section illustrates intended use of this component.

Example 1: Manipulation and Traversal of Day of Week Sets

A bdlt::DayOfWeekSet is useful for recording recurring appointments, or special days (e.g., weekend days), in a calendar. The following snippets of code illustrate how to create and use a bdlt::DayOfWeek set.

First, we create a couple of commonly useful sets. First we define the bdlt::DayOfWeekSet weekendDays:

bdlt::DayOfWeekSet weekendDays;
Definition bdlt_dayofweekset.h:398

Then, we notice that this set is initially empty.

assert(0 == weekendDays.length());
int length() const
Return the number of elements in this set.
Definition bdlt_dayofweekset.h:869

Next, we add the days that characterize weekends:

assert(1 == weekendDays.length());
assert(2 == weekendDays.length());
void add(DayOfWeek::Enum value)
Add the specified value to this set.
Definition bdlt_dayofweekset.h:790
@ e_SUN
Definition bdlt_dayofweek.h:125
@ e_SAT
Definition bdlt_dayofweek.h:131

Then, we observe that weekendDays now contains precisely the days we expect it to contain:

assert(true == weekendDays.isMember(bdlt::DayOfWeek::e_SUN));
assert(false == weekendDays.isMember(bdlt::DayOfWeek::e_MON));
assert(false == weekendDays.isMember(bdlt::DayOfWeek::e_TUE));
assert(false == weekendDays.isMember(bdlt::DayOfWeek::e_WED));
assert(false == weekendDays.isMember(bdlt::DayOfWeek::e_THU));
assert(false == weekendDays.isMember(bdlt::DayOfWeek::e_FRI));
assert(true == weekendDays.isMember(bdlt::DayOfWeek::e_SAT));
bool isMember(DayOfWeek::Enum value) const
Definition bdlt_dayofweekset.h:862
@ e_FRI
Definition bdlt_dayofweek.h:130
@ e_TUE
Definition bdlt_dayofweek.h:127
@ e_WED
Definition bdlt_dayofweek.h:128
@ e_MON
Definition bdlt_dayofweek.h:126
@ e_THU
Definition bdlt_dayofweek.h:129

Next, we create the complementary bdlt::DayOfWeekSet weekDays directly from weekendDays via a combination of unary negation and copy construction:

bdlt::DayOfWeekSet weekDays(~weekendDays);
assert(5 == weekDays.length());
assert(false == weekDays.isMember(bdlt::DayOfWeek::e_SUN));
assert(true == weekDays.isMember(bdlt::DayOfWeek::e_MON));
assert(true == weekDays.isMember(bdlt::DayOfWeek::e_TUE));
assert(true == weekDays.isMember(bdlt::DayOfWeek::e_WED));
assert(true == weekDays.isMember(bdlt::DayOfWeek::e_THU));
assert(true == weekDays.isMember(bdlt::DayOfWeek::e_FRI));
assert(false == weekDays.isMember(bdlt::DayOfWeek::e_SAT));

Then, to create a set containing all of the days in the week, we do so via unary negation of the default constructed value:

const bdlt::DayOfWeekSet NO_DAYS;
const bdlt::DayOfWeekSet ALL_DAYS(~NO_DAYS);
assert(7 == ALL_DAYS.length());

Next, we observe that neither weekDays nor weekendDays represent the same value as ALL_DAYS, but their union does:

assert(ALL_DAYS != weekendDays);
assert(ALL_DAYS != weekDays);
assert(ALL_DAYS == (weekDays | weekendDays));
assert(ALL_DAYS == (weekDays ^ weekendDays));
assert(weekendDays == ALL_DAYS - weekDays);
assert(weekDays == ALL_DAYS - weekendDays);
assert(weekDays == ALL_DAYS - weekendDays);

Then, we observe that similarly, neither weekDays nor weekendDays represents the same value as NO_DAYS, but their intersection does:

assert(NO_DAYS != weekendDays);
assert(NO_DAYS != weekDays);
assert(NO_DAYS == (weekDays & weekendDays));
assert(weekendDays == weekendDays - weekDays);
assert(weekDays == weekDays - weekendDays);

Next, we create the corresponding set eDays consisting of the only days of the week that have an E in them: TUESDAY and WEDNESDAY:

bdlt::DayOfWeekSet eDays; assert(0 == eDays.length());
eDays.add(bdlt::DayOfWeek::e_TUE); assert(1 == eDays.length());
eDays.add(bdlt::DayOfWeek::e_WED); assert(2 == eDays.length());
assert(false == eDays.isMember(bdlt::DayOfWeek::e_SUN));
assert(false == eDays.isMember(bdlt::DayOfWeek::e_MON));
assert(true == eDays.isMember(bdlt::DayOfWeek::e_TUE));
assert(true == eDays.isMember(bdlt::DayOfWeek::e_WED));
assert(false == eDays.isMember(bdlt::DayOfWeek::e_THU));
assert(false == eDays.isMember(bdlt::DayOfWeek::e_FRI));
assert(false == eDays.isMember(bdlt::DayOfWeek::e_SAT));

Then, we create a set consisting of days that have an n in them: MONDAY, WEDNESDAY, and SUNDAY. We create the corresponding set nDays starting with the value of eDays by first removing TUESDAY, and then adding SUNDAY and MONDAY:

bdlt::DayOfWeekSet nDays(eDays); assert(2 == nDays.length());
nDays.remove(bdlt::DayOfWeek::e_TUE); assert(1 == nDays.length());
nDays.add(bdlt::DayOfWeek::e_SUN); assert(2 == nDays.length());
nDays.add(bdlt::DayOfWeek::e_MON); assert(3 == nDays.length());
assert(true == nDays.isMember(bdlt::DayOfWeek::e_SUN));
assert(true == nDays.isMember(bdlt::DayOfWeek::e_MON));
assert(false == nDays.isMember(bdlt::DayOfWeek::e_TUE));
assert(true == nDays.isMember(bdlt::DayOfWeek::e_WED));
assert(false == nDays.isMember(bdlt::DayOfWeek::e_THU));
assert(false == nDays.isMember(bdlt::DayOfWeek::e_FRI));
assert(false == nDays.isMember(bdlt::DayOfWeek::e_SAT));

Next, we observe that all eDays are weekDays, but that's not true of nDays:

assert(true == weekDays.areMembers(eDays));
assert(false == weekDays.areMembers(nDays));

Now, we observe that iteration order is defined by increasing enumerated bdlt::DayOfWeek::Day value [ SUN .. SAT ]. The following use of the forward (bi-directional) iterator:

for (bdlt::DayOfWeekSet::iterator it = ALL_DAYS.begin();
it != ALL_DAYS.end();
++it) {
bsl::cout << *it << bsl::endl;
}
Definition bdlt_dayofweekset.h:293

produces:

SUN
MON
TUE
WED
THU
FRI
SAT

on standard output.

Finally, we observe that, similarly, the following use of the reverse iterator:

for (bdlt::DayOfWeekSet::reverse_iterator it = weekDays.rbegin();
it != weekDays.rend();
++it) {
bsl::cout << *it << bsl::endl;
}
bsl::reverse_iterator< iterator > reverse_iterator
Standard nested alias for set container's reverse iterator.
Definition bdlt_dayofweekset.h:421

produces:

FRI
THU
WED
TUE
MON