Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame^] | 1 | // Ceres Solver - A fast non-linear least squares minimizer |
| 2 | // Copyright 2018 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: jodebo_beck@gmx.de (Johannes Beck) |
| 30 | // |
| 31 | // This class mimics std::integer_sequence. That is the reason to follow the |
| 32 | // naming convention of the stl and not the google one. Once Ceres switches |
| 33 | // to c++ 14 this class can be removed. |
| 34 | |
| 35 | #ifndef CERES_PUBLIC_INTERNAL_INTEGER_SEQUENCE_H_ |
| 36 | #define CERES_PUBLIC_INTERNAL_INTEGER_SEQUENCE_H_ |
| 37 | |
| 38 | #if __cplusplus >= 201402L |
| 39 | // We have at least c++ 14 support. Use integer_sequence from the standard. |
| 40 | // Sometimes the STL implementation uses a compiler intrinsic to generate |
| 41 | // the sequences which will speed up compilation. |
| 42 | #include <utility> |
| 43 | |
| 44 | namespace ceres { |
| 45 | namespace internal { |
| 46 | template <typename T, T... Ns> |
| 47 | using integer_sequence = std::integer_sequence<T, Ns...>; |
| 48 | |
| 49 | template <typename T, T N> |
| 50 | using make_integer_sequence = std::make_integer_sequence<T, N>; |
| 51 | |
| 52 | } // namespace internal |
| 53 | } // namespace ceres |
| 54 | #else |
| 55 | |
| 56 | namespace ceres { |
| 57 | namespace internal { |
| 58 | |
| 59 | template <typename T, T... Ns> |
| 60 | struct integer_sequence { |
| 61 | using value_type = T; |
| 62 | }; |
| 63 | |
| 64 | // Implementation of make_integer_sequence. |
| 65 | // |
| 66 | // Recursively instantiate make_integer_sequence_impl until Ns |
| 67 | // contains the sequence 0, 1, ..., Total-1. |
| 68 | // |
| 69 | // Example for Total = 4: |
| 70 | // T CurIdx, Total, Ns... |
| 71 | // make_integer_sequence_impl<int, 0, 4 > |
| 72 | // make_integer_sequence_impl<int, 1, 4, 0 > |
| 73 | // make_integer_sequence_impl<int, 2, 4, 0, 1 > |
| 74 | // make_integer_sequence_impl<int, 3, 4, 0, 1, 2 > |
| 75 | // make_integer_sequence_impl<int, 4, 4, 0, 1, 2, 3> |
| 76 | // ^^^^^^^^^^ |
| 77 | // resulting sequence. |
| 78 | // |
| 79 | // The implemented algorithm has linear complexity for simplicity. A O(log(N)) |
| 80 | // implementation can be found e.g. here: |
| 81 | // https://stackoverflow.com/questions/17424477/implementation-c14-make-integer-sequence |
| 82 | template <typename T, T CurIdx, T Total, T... Ns> |
| 83 | struct make_integer_sequence_impl { |
| 84 | using type = typename make_integer_sequence_impl<T, CurIdx + 1, Total, Ns..., |
| 85 | CurIdx>::type; |
| 86 | }; |
| 87 | |
| 88 | // End of 'recursion' when CurIdx reaches Total. All indices 0, 1, ..., N-1 are |
| 89 | // contained in Ns. The final integer_sequence is created here. |
| 90 | template <typename T, T Total, T... Ns> |
| 91 | struct make_integer_sequence_impl<T, Total, Total, Ns...> { |
| 92 | using type = integer_sequence<T, Ns...>; |
| 93 | }; |
| 94 | |
| 95 | // A helper alias template to simplify creation of integer_sequence with 0, 1, |
| 96 | // ..., N-1 as Ns: |
| 97 | template <typename T, T N> |
| 98 | using make_integer_sequence = |
| 99 | typename make_integer_sequence_impl<T, 0, N>::type; |
| 100 | |
| 101 | } // namespace internal |
| 102 | } // namespace ceres |
| 103 | |
| 104 | #endif |
| 105 | |
| 106 | #endif // CERES_PUBLIC_INTERNAL_INTEGER_SEQUENCE_H_ |