blob: 804f4412debbd7407f956a3ecf3a493244ff060a [file] [log] [blame]
Austin Schuh70cc9552019-01-21 19:46:48 -08001// Ceres Solver - A fast non-linear least squares minimizer
2// Copyright 2015 Google Inc. All rights reserved.
3// http://ceres-solver.org/
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 "ceres/split.h"
32
33#include <iterator>
34#include <string>
35#include <vector>
36
37#include "ceres/internal/port.h"
38
39namespace ceres {
40namespace internal {
41
42using std::string;
43using std::vector;
44
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
48// microbenchmarks.
49//
50// The reserve is only implemented for the single character delim.
51//
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.
56static int CalculateReserveForVector(const string& full, const char* delim) {
57 int count = 0;
58 if (delim[0] != '\0' && delim[1] == '\0') {
59 // Optimize the common case where delim is a single character.
60 char c = delim[0];
61 const char* p = full.data();
62 const char* end = p + full.size();
63 while (p != end) {
64 if (*p == c) { // This could be optimized with hasless(v,1) trick.
65 ++p;
66 } else {
67 while (++p != end && *p != c) {
68 // Skip to the next occurence of the delimiter.
69 }
70 ++count;
71 }
72 }
73 }
74 return count;
75}
76
77template <typename StringType, typename ITR>
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080078static inline void SplitStringToIteratorUsing(const StringType& full,
79 const char* delim,
80 ITR& result) {
Austin Schuh70cc9552019-01-21 19:46:48 -080081 // Optimize the common case where delim is a single character.
82 if (delim[0] != '\0' && delim[1] == '\0') {
83 char c = delim[0];
84 const char* p = full.data();
85 const char* end = p + full.size();
86 while (p != end) {
87 if (*p == c) {
88 ++p;
89 } else {
90 const char* start = p;
91 while (++p != end && *p != c) {
92 // Skip to the next occurence of the delimiter.
93 }
94 *result++ = StringType(start, p - start);
95 }
96 }
97 return;
98 }
99
100 string::size_type begin_index, end_index;
101 begin_index = full.find_first_not_of(delim);
102 while (begin_index != string::npos) {
103 end_index = full.find_first_of(delim, begin_index);
104 if (end_index == string::npos) {
105 *result++ = full.substr(begin_index);
106 return;
107 }
108 *result++ = full.substr(begin_index, (end_index - begin_index));
109 begin_index = full.find_first_not_of(delim, end_index);
110 }
111}
112
113void SplitStringUsing(const string& full,
114 const char* delim,
115 vector<string>* result) {
116 result->reserve(result->size() + CalculateReserveForVector(full, delim));
117 std::back_insert_iterator<vector<string>> it(*result);
118 SplitStringToIteratorUsing(full, delim, it);
119}
120
121} // namespace internal
122} // namespace ceres