1 // Ceres Solver - A fast non-linear least squares minimizer
2 // Copyright 2010, 2011, 2012 Google Inc. All rights reserved.
3 // http://code.google.com/p/ceres-solver/
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
7 //
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.
16 //
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.
28 //
29 // Author: keir@google.com (Keir Mierle)
30
31 #include <string>
32 #include <vector>
33 #include <iterator>
34 #include "ceres/internal/port.h"
35
36 namespace ceres {
37
38 // If we know how much to allocate for a vector of strings, we can allocate the
39 // vector<string> only once and directly to the right size. This saves in
40 // between 33-66 % of memory space needed for the result, and runs faster in the
41 // microbenchmarks.
42 //
43 // The reserve is only implemented for the single character delim.
44 //
45 // The implementation for counting is cut-and-pasted from
46 // SplitStringToIteratorUsing. I could have written my own counting iterator,
47 // and use the existing template function, but probably this is more clear and
48 // more sure to get optimized to reasonable code.
CalculateReserveForVector(const string & full,const char * delim)49 static int CalculateReserveForVector(const string& full, const char* delim) {
50 int count = 0;
51 if (delim[0] != '\0' && delim[1] == '\0') {
52 // Optimize the common case where delim is a single character.
53 char c = delim[0];
54 const char* p = full.data();
55 const char* end = p + full.size();
56 while (p != end) {
57 if (*p == c) { // This could be optimized with hasless(v,1) trick.
58 ++p;
59 } else {
60 while (++p != end && *p != c) {
61 // Skip to the next occurence of the delimiter.
62 }
63 ++count;
64 }
65 }
66 }
67 return count;
68 }
69
70 template <typename StringType, typename ITR>
71 static inline
SplitStringToIteratorUsing(const StringType & full,const char * delim,ITR & result)72 void SplitStringToIteratorUsing(const StringType& full,
73 const char* delim,
74 ITR& result) {
75 // Optimize the common case where delim is a single character.
76 if (delim[0] != '\0' && delim[1] == '\0') {
77 char c = delim[0];
78 const char* p = full.data();
79 const char* end = p + full.size();
80 while (p != end) {
81 if (*p == c) {
82 ++p;
83 } else {
84 const char* start = p;
85 while (++p != end && *p != c) {
86 // Skip to the next occurence of the delimiter.
87 }
88 *result++ = StringType(start, p - start);
89 }
90 }
91 return;
92 }
93
94 string::size_type begin_index, end_index;
95 begin_index = full.find_first_not_of(delim);
96 while (begin_index != string::npos) {
97 end_index = full.find_first_of(delim, begin_index);
98 if (end_index == string::npos) {
99 *result++ = full.substr(begin_index);
100 return;
101 }
102 *result++ = full.substr(begin_index, (end_index - begin_index));
103 begin_index = full.find_first_not_of(delim, end_index);
104 }
105 }
106
SplitStringUsing(const string & full,const char * delim,vector<string> * result)107 void SplitStringUsing(const string& full,
108 const char* delim,
109 vector<string>* result) {
110 result->reserve(result->size() + CalculateReserveForVector(full, delim));
111 back_insert_iterator< vector<string> > it(*result);
112 SplitStringToIteratorUsing(full, delim, it);
113 }
114
115 } // namespace ceres
116