|
BDE 4.14.0 Production release
|
Provide an extensible, packed array of integral values.
const_iteratorThis component provides a space-efficient value-semantic array class template, bdlc::PackedIntArray, and an associated iterator, bdlc::PackedIntArrayConstIterator, that provides non-modifiable access to its elements. The interface of this class provides the user with functionality similar to a bsl::vector<int>. The implementation is designed to reduce dynamic memory usage by storing its contents differently according to the magnitude of values placed within it. The user need not be concerned with the internal representation of the data. The array supports primitive operations (e.g., insertion, look-up, removal) as well as a complete set of value-semantic operations; however, direct reference to individual elements is not available. Users can access the value of individual elements by calling the indexing operator or via iterators. Note that iterators are not invalidated if an array object reallocates memory.
This section illustrates intended use of this component.
There exist many applications in which the range of int data that a container will hold is not known at design time. This means in order to build a robust component one must default to bsl::vector<int>, which for many applications is excessive in its usage of space.
Suppose we are creating a map of temperatures for every city in the United States for every day. This represents a large body of data, most of which is easily representable in a signed char, and in only rare situations is a short required.
To be able to represent all possible values for all areas and times, including extremes like Death Valley, a traditional implementation would require use of a vector<short> for each day for each area. This is excessive for all but the most extreme values, and therefore wasteful for this map as a whole.
We can use bdlc::PackedIntArray to efficiently store this data.
First, we declare and define a my_Date class. This class is very similar to bdlt::Date, and therefore is elided for the sake of compactness.
Then, we create our temperatureMap, which is a map of dates to a map of zip codes to a PackedIntArray of temperatures. Each PackedIntArray has entries for each temperature from 12 A.M, to 11 P.M for each city in each zip code. Notice that we use a PackedIntArray to hold the data compactly.
Next, we add data to the map (provided by the National Weather Service) for a normal case, and the extreme.
Then, since the size of the data set is known at design time, as well as extreme values for the areas, we can use the reserveCapacity() method to give the container hints about the data to come.
Now we add the data to the respective containers.
Finally, notice that in order to represent these values in a PackedIntArray, it required 24 * sizeof(signed char) bytes (24 on most systems) of dynamic memory for nyc, which represents the normal case for this data. A vector<short> would require 24 * sizeof(short) bytes (48 on most systems) of dynamic memory to represent the same data.