Provide an ordered set of (unique) bdlt::DayOfWeek::Enum
values.
More...
Namespaces |
namespace | bdlt |
Detailed Description
- Outline
-
-
- Purpose:
- Provide an ordered set of (unique)
bdlt::DayOfWeek::Enum
values.
-
- Classes:
-
- See also:
- Component 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
: Then, we notice that this set is initially empty. assert(0 == weekendDays.length());
Next, we add the days that characterize weekends: Then, we observe that weekendDays
now contains precisely the days we expect it to contain: Next, we create the complementary bdlt::DayOfWeekSet
weekDays
directly from weekendDays
via a combination of unary negation and copy construction: Then, to create a set containing all of the days in the week, we do so via unary negation of the default constructed value: 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
: 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
: 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: produces: SUN
MON
TUE
WED
THU
FRI
SAT
on standard output.
- Finally, we observe that, similarly, the following use of the reverse iterator: produces: