1 // Ceres Solver - A fast non-linear least squares minimizer
2 // Copyright 2015 Google Inc. All rights reserved.
3 // http://ceres-solver.org/
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
8 // * Redistributions of source code must retain the above copyright notice,
9 // this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright notice,
11 // this list of conditions and the following disclaimer in the documentation
12 // and/or other materials provided with the distribution.
13 // * Neither the name of Google Inc. nor the names of its contributors may be
14 // used to endorse or promote products derived from this software without
15 // specific prior written permission.
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 // POSSIBILITY OF SUCH DAMAGE.
29 // Author: keir@google.com (Keir Mierle)
31 #include "ceres/split.h"
37 #include "ceres/internal/port.h"
45 // If we know how much to allocate for a vector of strings, we can allocate the
46 // vector<string> only once and directly to the right size. This saves in
47 // between 33-66 % of memory space needed for the result, and runs faster in the
50 // The reserve is only implemented for the single character delim.
52 // The implementation for counting is cut-and-pasted from
53 // SplitStringToIteratorUsing. I could have written my own counting iterator,
54 // and use the existing template function, but probably this is more clear and
55 // more sure to get optimized to reasonable code.
56 static int CalculateReserveForVector(const string& full, const char* delim) {
58 if (delim[0] != '\0' && delim[1] == '\0') {
59 // Optimize the common case where delim is a single character.
61 const char* p = full.data();
62 const char* end = p + full.size();
64 if (*p == c) { // This could be optimized with hasless(v,1) trick.
67 while (++p != end && *p != c) {
68 // Skip to the next occurence of the delimiter.
77 template <typename StringType, typename ITR>
79 void SplitStringToIteratorUsing(const StringType& full,
82 // Optimize the common case where delim is a single character.
83 if (delim[0] != '\0' && delim[1] == '\0') {
85 const char* p = full.data();
86 const char* end = p + full.size();
91 const char* start = p;
92 while (++p != end && *p != c) {
93 // Skip to the next occurence of the delimiter.
95 *result++ = StringType(start, p - start);
101 string::size_type begin_index, end_index;
102 begin_index = full.find_first_not_of(delim);
103 while (begin_index != string::npos) {
104 end_index = full.find_first_of(delim, begin_index);
105 if (end_index == string::npos) {
106 *result++ = full.substr(begin_index);
109 *result++ = full.substr(begin_index, (end_index - begin_index));
110 begin_index = full.find_first_not_of(delim, end_index);
114 void SplitStringUsing(const string& full,
116 vector<string>* result) {
117 result->reserve(result->size() + CalculateReserveForVector(full, delim));
118 std::back_insert_iterator<vector<string> > it(*result);
119 SplitStringToIteratorUsing(full, delim, it);
122 } // namespace internal