1#ifndef SHRINK_TO_FIT_H
2#define SHRINK_TO_FIT_H
3
4/*
5 * Legal Notice
6 *
7 * This document and associated source code (the "Work") is a part of a
8 * benchmark specification maintained by the TPC.
9 *
10 * The TPC reserves all right, title, and interest to the Work as provided
11 * under U.S. and international laws, including without limitation all patent
12 * and trademark rights therein.
13 *
14 * No Warranty
15 *
16 * 1.1 TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW, THE INFORMATION
17 * CONTAINED HEREIN IS PROVIDED "AS IS" AND WITH ALL FAULTS, AND THE
18 * AUTHORS AND DEVELOPERS OF THE WORK HEREBY DISCLAIM ALL OTHER
19 * WARRANTIES AND CONDITIONS, EITHER EXPRESS, IMPLIED OR STATUTORY,
20 * INCLUDING, BUT NOT LIMITED TO, ANY (IF ANY) IMPLIED WARRANTIES,
21 * DUTIES OR CONDITIONS OF MERCHANTABILITY, OF FITNESS FOR A PARTICULAR
22 * PURPOSE, OF ACCURACY OR COMPLETENESS OF RESPONSES, OF RESULTS, OF
23 * WORKMANLIKE EFFORT, OF LACK OF VIRUSES, AND OF LACK OF NEGLIGENCE.
24 * ALSO, THERE IS NO WARRANTY OR CONDITION OF TITLE, QUIET ENJOYMENT,
25 * QUIET POSSESSION, CORRESPONDENCE TO DESCRIPTION OR NON-INFRINGEMENT
26 * WITH REGARD TO THE WORK.
27 * 1.2 IN NO EVENT WILL ANY AUTHOR OR DEVELOPER OF THE WORK BE LIABLE TO
28 * ANY OTHER PARTY FOR ANY DAMAGES, INCLUDING BUT NOT LIMITED TO THE
29 * COST OF PROCURING SUBSTITUTE GOODS OR SERVICES, LOST PROFITS, LOSS
30 * OF USE, LOSS OF DATA, OR ANY INCIDENTAL, CONSEQUENTIAL, DIRECT,
31 * INDIRECT, OR SPECIAL DAMAGES WHETHER UNDER CONTRACT, TORT, WARRANTY,
32 * OR OTHERWISE, ARISING IN ANY WAY OUT OF THIS OR ANY OTHER AGREEMENT
33 * RELATING TO THE WORK, WHETHER OR NOT SUCH AUTHOR OR DEVELOPER HAD
34 * ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES.
35 *
36 * Contributors
37 * - Doug Johnson
38 */
39
40namespace TPCE {
41
42// Utility functions for tightening up STL containers and strings.
43// With C++11 STL containers and strings have a
44// shrink_to_fit method. But since C++11 features
45// are not supported equally well by all compilers we'll use a
46// general trick that works well but just isn't as obvious to the
47// casual reader of the code. This way, for now, everyone executes
48// the smae code path. When C++11 support is more ubiquitous calls
49// to this function can be replaced by calls to the shrink_to_fit
50// method associated with the object.
51
52template <class T> void shrink_to_fit(T &container) {
53 T(container).swap(container);
54}
55
56template <class T> void shrink_to_fit(T &container, const std::string &name) {
57 printf("%s: shrinking from %d to ", name.c_str(), container.capacity());
58 T(container).swap(container);
59 printf("%d.\n", container.capacity());
60}
61
62} // namespace TPCE
63#endif // SHRINK_TO_FIT_H
64