BDE 4.14.0 Production release
|
Provide a compacted array of const
user-defined objects.
This component provides a space-efficient value-semantic array, bdlc::CompactedArray
, and an associated iterator, bdlc::CompactedArray::const_iterator
, that provides non-modifiable access to its elements. The interface of this class provides the user with functionality similar to a bsl::vector<T>
. The implementation is designed to reduce dynamic memory usage by (1) removing data duplication at the expense of an additional indirection to obtain the stored objects (using the flyweight design pattern) and (2) requiring operator<
to be defined for the type of the stored objects. The array supports primitive operations (e.g., insertion, look-up, removal), as well as a complete set of value-semantic operations; however, modifiable reference to individual elements is not available. Users can access the (non-modifiable) value of individual elements by calling the indexing operator or via iterators.
This section illustrates intended use of this component.
Suppose we are creating a sequence of daily schedules for an employee. Most Mondays (and Tuesdays, Wednesdays, etc.) will have the same schedule, although some may differ. Instead of storing this data in a bsl::vector<my_DailySchedule>
, we can use bdlc::CompactedArray<my_DailySchedule>
to efficiently store this data.
First, we declare and define a my_DailySchedule
class. This class is not overly relevant to the example and is elided for the sake of brevity:
Then, we create our schedule, which is an array of my_DailySchedule
where the index of each element is the date offset (from an arbitrary epoch measured in days).
Now, we create some daily schedules and append them to the schedule
:
Finally, we verify that the storage is compacted: