All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
array.h
Go to the documentation of this file.
1 //
2 // Copyright 2014 DreamWorks Animation LLC.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "Apache License")
5 // with the following modification; you may not use this file except in
6 // compliance with the Apache License and the following modification to it:
7 // Section 6. Trademarks. is deleted and replaced with:
8 //
9 // 6. Trademarks. This License does not grant permission to use the trade
10 // names, trademarks, service marks, or product names of the Licensor
11 // and its affiliates, except as required to comply with Section 4(c) of
12 // the License and to reproduce the content of the NOTICE file.
13 //
14 // You may obtain a copy of the Apache License at
15 //
16 // http://www.apache.org/licenses/LICENSE-2.0
17 //
18 // Unless required by applicable law or agreed to in writing, software
19 // distributed under the Apache License with the above modification is
20 // distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21 // KIND, either express or implied. See the Apache License for the specific
22 // language governing permissions and limitations under the Apache License.
23 //
24 #ifndef VTR_ARRAY_INTERFACE_H
25 #define VTR_ARRAY_INTERFACE_H
26 
27 #include "../version.h"
28 
29 namespace OpenSubdiv {
30 namespace OPENSUBDIV_VERSION {
31 
32 namespace Vtr {
33 
34 //
35 // This class provides a simple array-like interface -- a subset std::vector's interface -- for
36 // a sequence of elements stored in contiguous memory. It provides a unified representation for
37 // referencing data on the stack, all or a subset of std::vector<>, or anywhere else in memory.
38 //
39 // Note that its members are head/size rather than begin/end as in std::vector -- we frequently
40 // need only the size for many queries, and that is most often what is stored elsewhere in other
41 // classes, so we hope to reduce unnecessary address arithmetic constructing the interface and
42 // accessing the size. The size type is also specifically 32-bit (rather than size_t) to match
43 // internal usage and avoid unnecessary conversion to/from 64-bit.
44 //
45 // Question:
46 // Naming is at issue here... formerly called ArrayInterface until that was shot down it has
47 // been simplified to Array but needs to be distanced from std::array as it DOES NOT store its
48 // own memory and is simply an interface to memory stored elsewhere.
49 //
50 template <typename TYPE>
51 class Array {
52 
53 public:
54  typedef TYPE value_type;
55  typedef int size_type;
56 
57  typedef TYPE const& const_reference;
58  typedef TYPE const* const_iterator;
59 
60  typedef TYPE& reference;
61  typedef TYPE* iterator;
62 
63 public:
64  Array() : _begin(0), _size(0) { }
65  Array(const Array<value_type>& array) : _begin(array._begin),
66  _size(array._size) { }
67  Array(const value_type* ptr, size_type size) : _begin(const_cast<value_type*>(ptr)),
68  _size(size) { }
69  ~Array() { }
70 
71  size_type size() const { return _size; }
72 
73  const_reference operator[](int index) const { return _begin[index]; }
74  const_iterator begin() const { return _begin; }
75  const_iterator end() const { return _begin + _size; }
76 
77  reference operator[](int index) { return _begin[index]; }
78  iterator begin() { return _begin; }
79  iterator end() { return _begin + _size; }
80 
81 protected:
84 };
85 
86 } // end namespace Vtr
87 
88 } // end namespace OPENSUBDIV_VERSION
89 using namespace OPENSUBDIV_VERSION;
90 } // end namespace OpenSubdiv
91 
92 #endif /* VTR_ARRAY_INTERFACE_H */
93 
Array(const Array< value_type > &array)
Definition: array.h:65
reference operator[](int index)
Definition: array.h:77
const_iterator begin() const
Definition: array.h:74
const_iterator end() const
Definition: array.h:75
const_reference operator[](int index) const
Definition: array.h:73
Array(const value_type *ptr, size_type size)
Definition: array.h:67