fixed_array.h
Go to the documentation of this file.
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: rennie@google.com (Jeffrey Rennie)
30 // Author: sanjay@google.com (Sanjay Ghemawat) -- renamed to FixedArray
31 
32 #ifndef CERES_PUBLIC_INTERNAL_FIXED_ARRAY_H_
33 #define CERES_PUBLIC_INTERNAL_FIXED_ARRAY_H_
34 
35 #include <cstddef>
39 
40 namespace ceres {
41 namespace internal {
42 
43 // A FixedArray<T> represents a non-resizable array of T where the
44 // length of the array does not need to be a compile time constant.
45 //
46 // FixedArray allocates small arrays inline, and large arrays on
47 // the heap. It is a good replacement for non-standard and deprecated
48 // uses of alloca() and variable length arrays (a GCC extension).
49 //
50 // FixedArray keeps performance fast for small arrays, because it
51 // avoids heap operations. It also helps reduce the chances of
52 // accidentally overflowing your stack if large input is passed to
53 // your function.
54 //
55 // Also, FixedArray is useful for writing portable code. Not all
56 // compilers support arrays of dynamic size.
57 
58 // Most users should not specify an inline_elements argument and let
59 // FixedArray<> automatically determine the number of elements
60 // to store inline based on sizeof(T).
61 //
62 // If inline_elements is specified, the FixedArray<> implementation
63 // will store arrays of length <= inline_elements inline.
64 //
65 // Finally note that unlike vector<T> FixedArray<T> will not zero-initialize
66 // simple types like int, double, bool, etc.
67 //
68 // Non-POD types will be default-initialized just like regular vectors or
69 // arrays.
70 
71 #if defined(_WIN64)
72  typedef __int64 ssize_t;
73 #elif defined(_WIN32)
74  typedef __int32 ssize_t;
75 #endif
76 
77 template <typename T, ssize_t inline_elements = -1>
78 class FixedArray {
79  public:
80  // For playing nicely with stl:
81  typedef T value_type;
82  typedef T* iterator;
83  typedef T const* const_iterator;
84  typedef T& reference;
85  typedef T const& const_reference;
86  typedef T* pointer;
87  typedef std::ptrdiff_t difference_type;
88  typedef size_t size_type;
89 
90  // REQUIRES: n >= 0
91  // Creates an array object that can store "n" elements.
92  //
93  // FixedArray<T> will not zero-initialiaze POD (simple) types like int,
94  // double, bool, etc.
95  // Non-POD types will be default-initialized just like regular vectors or
96  // arrays.
97  explicit FixedArray(size_type n);
98 
99  // Releases any resources.
100  ~FixedArray();
101 
102  // Returns the length of the array.
103  inline size_type size() const { return size_; }
104 
105  // Returns the memory size of the array in bytes.
106  inline size_t memsize() const { return size_ * sizeof(T); }
107 
108  // Returns a pointer to the underlying element array.
109  inline const T* get() const { return &array_[0].element; }
110  inline T* get() { return &array_[0].element; }
111 
112  // REQUIRES: 0 <= i < size()
113  // Returns a reference to the "i"th element.
114  inline T& operator[](size_type i) {
115  assert(i < size_);
116  return array_[i].element;
117  }
118 
119  // REQUIRES: 0 <= i < size()
120  // Returns a reference to the "i"th element.
121  inline const T& operator[](size_type i) const {
122  assert(i < size_);
123  return array_[i].element;
124  }
125 
126  inline iterator begin() { return &array_[0].element; }
127  inline iterator end() { return &array_[size_].element; }
128 
129  inline const_iterator begin() const { return &array_[0].element; }
130  inline const_iterator end() const { return &array_[size_].element; }
131 
132  private:
133  // Container to hold elements of type T. This is necessary to handle
134  // the case where T is a a (C-style) array. The size of InnerContainer
135  // and T must be the same, otherwise callers' assumptions about use
136  // of this code will be broken.
137  struct InnerContainer {
139  };
140 
141  // How many elements should we store inline?
142  // a. If not specified, use a default of 256 bytes (256 bytes
143  // seems small enough to not cause stack overflow or unnecessary
144  // stack pollution, while still allowing stack allocation for
145  // reasonably long character arrays.
146  // b. Never use 0 length arrays (not ISO C++)
147  static const size_type S1 = ((inline_elements < 0)
148  ? (256/sizeof(T)) : inline_elements);
149  static const size_type S2 = (S1 <= 0) ? 1 : S1;
150  static const size_type kInlineElements = S2;
151 
152  size_type const size_;
154 
155  // Allocate some space, not an array of elements of type T, so that we can
156  // skip calling the T constructors and destructors for space we never use.
158 };
159 
160 // Implementation details follow
161 
162 template <class T, ssize_t S>
164  : size_(n),
165  array_((n <= kInlineElements
166  ? reinterpret_cast<InnerContainer*>(inline_space_)
167  : new InnerContainer[n])) {
168  // Construct only the elements actually used.
169  if (array_ == reinterpret_cast<InnerContainer*>(inline_space_)) {
170  for (size_t i = 0; i != size_; ++i) {
171  inline_space_[i].Init();
172  }
173  }
174 }
175 
176 template <class T, ssize_t S>
178  if (array_ != reinterpret_cast<InnerContainer*>(inline_space_)) {
179  delete[] array_;
180  } else {
181  for (size_t i = 0; i != size_; ++i) {
182  inline_space_[i].Destroy();
183  }
184  }
185 }
186 
187 } // namespace internal
188 } // namespace ceres
189 
190 #endif // CERES_PUBLIC_INTERNAL_FIXED_ARRAY_H_
const_iterator end() const
Definition: fixed_array.h:130
size_type size() const
Definition: fixed_array.h:103
InnerContainer *const array_
Definition: fixed_array.h:153
Symmetric< 2 > S2
Definition: testGroup.cpp:80
int n
static const size_type S2
Definition: fixed_array.h:149
const T & operator[](size_type i) const
Definition: fixed_array.h:121
Eigen::Triplet< double > T
std::ptrdiff_t difference_type
Definition: fixed_array.h:87
T & operator[](size_type i)
Definition: fixed_array.h:114
static const size_type S1
Definition: fixed_array.h:147
const_iterator begin() const
Definition: fixed_array.h:129
ManualConstructor< InnerContainer > inline_space_[kInlineElements]
Definition: fixed_array.h:157
static const size_type kInlineElements
Definition: fixed_array.h:150


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:42:04