Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame^] | 1 | // 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 | |
| 39 | namespace ceres { |
| 40 | namespace internal { |
| 41 | |
| 42 | using std::string; |
| 43 | using 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. |
| 56 | static 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 | |
| 77 | template <typename StringType, typename ITR> |
| 78 | static inline |
| 79 | void SplitStringToIteratorUsing(const StringType& full, |
| 80 | const char* delim, |
| 81 | ITR& result) { |
| 82 | // Optimize the common case where delim is a single character. |
| 83 | if (delim[0] != '\0' && delim[1] == '\0') { |
| 84 | char c = delim[0]; |
| 85 | const char* p = full.data(); |
| 86 | const char* end = p + full.size(); |
| 87 | while (p != end) { |
| 88 | if (*p == c) { |
| 89 | ++p; |
| 90 | } else { |
| 91 | const char* start = p; |
| 92 | while (++p != end && *p != c) { |
| 93 | // Skip to the next occurence of the delimiter. |
| 94 | } |
| 95 | *result++ = StringType(start, p - start); |
| 96 | } |
| 97 | } |
| 98 | return; |
| 99 | } |
| 100 | |
| 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); |
| 107 | return; |
| 108 | } |
| 109 | *result++ = full.substr(begin_index, (end_index - begin_index)); |
| 110 | begin_index = full.find_first_not_of(delim, end_index); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | void SplitStringUsing(const string& full, |
| 115 | const char* delim, |
| 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); |
| 120 | } |
| 121 | |
| 122 | } // namespace internal |
| 123 | } // namespace ceres |