| OpenSubdiv

Loading...
Searching...
No Matches
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 OPENSUBDIV3_VTR_ARRAY_INTERFACE_H
25#define OPENSUBDIV3_VTR_ARRAY_INTERFACE_H
26
27#include "../version.h"
28
29#include <cassert>
30
31 namespace OpenSubdiv {
32 namespace OPENSUBDIV_VERSION {
33
34 namespace Vtr {
35
36//
37// This class provides a simple array-like interface -- a subset std::vector's interface -- for
38// a sequence of elements stored in contiguous memory. It provides a unified representation for
39// referencing data on the stack, all or a subset of std::vector<>, or anywhere else in memory.
40//
41// Note that its members are head/size rather than begin/end as in std::vector -- we frequently
42// need only the size for many queries, and that is most often what is stored elsewhere in other
43// classes, so we hope to reduce unnecessary address arithmetic constructing the interface and
44// accessing the size. The size type is also specifically 32-bit (rather than size_t) to match
45// internal usage and avoid unnecessary conversion to/from 64-bit.
46//
47// Question:
48// Naming is at issue here... formerly called ArrayInterface until that was shot down it has
49// been simplified to Array but needs to be distanced from std::array as it DOES NOT store its
50// own memory and is simply an interface to memory stored elsewhere.
51//
52 template <typename TYPE>
53 class ConstArray {
54
55 public:
56 typedef TYPE value_type;
57 typedef int size_type;
58
59 typedef TYPE const& const_reference;
60 typedef TYPE const* const_iterator;
61
62 typedef TYPE& reference;
63 typedef TYPE* iterator;
64
65 public:
66
67 ConstArray() : _begin(0), _size(0) { }
68
69 ConstArray(value_type const * ptr, size_type sizeArg) :
70 _begin(ptr), _size(sizeArg) { }
71
72 size_type size() const { return _size; }
73
74 bool empty() const { return _size==0; }
75
76 const_reference operator[](int index) const { return _begin[index]; }
77 const_iterator begin() const { return _begin; }
78 const_iterator end() const { return _begin + _size; }
79
80 size_type FindIndexIn4Tuple(value_type value) const {
81 assert(_size>=4);
82 if (value == _begin[0]) return 0;
83 if (value == _begin[1]) return 1;
84 if (value == _begin[2]) return 2;
85 if (value == _begin[3]) return 3;
86 assert("FindIndexIn4Tuple() did not find expected value!" == 0);
87 return -1;
88 }
89
90 size_type FindIndex(value_type value) const {
91 for (size_type i=0; i<size(); ++i) {
92 if (value==_begin[i]) {
93 return i;
94 }
95 }
96 return -1;
97 }
98
99 protected:
100 value_type const * _begin;
101 size_type _size;
102};
103
104 template <typename TYPE>
105 class Array : public ConstArray<TYPE> {
106
107 public:
108 typedef TYPE value_type;
109 typedef int size_type;
110
111 typedef TYPE const& const_reference;
112
113 typedef TYPE& reference;
114 typedef TYPE* iterator;
115
116 public:
117
118 Array() : ConstArray<TYPE>() { }
119
120 Array(value_type * ptr, size_type sizeArg) : ConstArray<TYPE>(ptr, sizeArg) { }
121
122 public:
123
124 const_reference operator[](int index) const {
125 return ConstArray<TYPE>::_begin[index];
126 }
127
128 reference operator[](int index) {
129 return const_cast<reference>(ConstArray<TYPE>::_begin[index]);
130 }
131
132 iterator begin() {
133 return const_cast<iterator>(ConstArray<TYPE>::_begin);
134 }
135
136 iterator end() {
137 return const_cast<iterator>(ConstArray<TYPE>::_begin +
138 ConstArray<TYPE>::_size);
139 }
140};
141
142} // end namespace Vtr
143
144} // end namespace OPENSUBDIV_VERSION
145 using namespace OPENSUBDIV_VERSION;
146} // end namespace OpenSubdiv
147
148#endif /* OPENSUBDIV3_VTR_ARRAY_INTERFACE_H */
OpenSubdiv
Definition error.h:30
OpenSubdiv::OPENSUBDIV_VERSION::Vtr::ConstArray
Definition array.h:53
OpenSubdiv::OPENSUBDIV_VERSION::Vtr::ConstArray::_begin
value_type const * _begin
Definition array.h:100
OpenSubdiv::OPENSUBDIV_VERSION::Vtr::ConstArray::value_type
TYPE value_type
Definition array.h:56
OpenSubdiv::OPENSUBDIV_VERSION::Vtr::ConstArray::begin
const_iterator begin() const
Definition array.h:77
OpenSubdiv::OPENSUBDIV_VERSION::Vtr::ConstArray::operator[]
const_reference operator[](int index) const
Definition array.h:76
OpenSubdiv::OPENSUBDIV_VERSION::Vtr::ConstArray::const_iterator
TYPE const * const_iterator
Definition array.h:60
OpenSubdiv::OPENSUBDIV_VERSION::Vtr::ConstArray::FindIndexIn4Tuple
size_type FindIndexIn4Tuple(value_type value) const
Definition array.h:80
OpenSubdiv::OPENSUBDIV_VERSION::Vtr::ConstArray::size
size_type size() const
Definition array.h:72
OpenSubdiv::OPENSUBDIV_VERSION::Vtr::ConstArray::empty
bool empty() const
Definition array.h:74
OpenSubdiv::OPENSUBDIV_VERSION::Vtr::ConstArray::ConstArray
ConstArray(value_type const *ptr, size_type sizeArg)
Definition array.h:69
OpenSubdiv::OPENSUBDIV_VERSION::Vtr::ConstArray::reference
TYPE & reference
Definition array.h:62
OpenSubdiv::OPENSUBDIV_VERSION::Vtr::ConstArray::_size
size_type _size
Definition array.h:101
OpenSubdiv::OPENSUBDIV_VERSION::Vtr::ConstArray::FindIndex
size_type FindIndex(value_type value) const
Definition array.h:90
OpenSubdiv::OPENSUBDIV_VERSION::Vtr::ConstArray::size_type
int size_type
Definition array.h:57
OpenSubdiv::OPENSUBDIV_VERSION::Vtr::ConstArray::const_reference
TYPE const & const_reference
Definition array.h:59
OpenSubdiv::OPENSUBDIV_VERSION::Vtr::ConstArray::end
const_iterator end() const
Definition array.h:78
OpenSubdiv::OPENSUBDIV_VERSION::Vtr::ConstArray::iterator
TYPE * iterator
Definition array.h:63
OpenSubdiv::OPENSUBDIV_VERSION::Vtr::ConstArray::ConstArray
ConstArray()
Definition array.h:67
OpenSubdiv::OPENSUBDIV_VERSION::Vtr::Array
Definition array.h:105
OpenSubdiv::OPENSUBDIV_VERSION::Vtr::Array::value_type
TYPE value_type
Definition array.h:108
OpenSubdiv::OPENSUBDIV_VERSION::Vtr::Array::operator[]
const_reference operator[](int index) const
Definition array.h:124
OpenSubdiv::OPENSUBDIV_VERSION::Vtr::Array::reference
TYPE & reference
Definition array.h:113
OpenSubdiv::OPENSUBDIV_VERSION::Vtr::Array::size_type
int size_type
Definition array.h:109
OpenSubdiv::OPENSUBDIV_VERSION::Vtr::Array::const_reference
TYPE const & const_reference
Definition array.h:111
OpenSubdiv::OPENSUBDIV_VERSION::Vtr::Array::end
iterator end()
Definition array.h:136
OpenSubdiv::OPENSUBDIV_VERSION::Vtr::Array::begin
iterator begin()
Definition array.h:132
OpenSubdiv::OPENSUBDIV_VERSION::Vtr::Array::Array
Array(value_type *ptr, size_type sizeArg)
Definition array.h:120
OpenSubdiv::OPENSUBDIV_VERSION::Vtr::Array::iterator
TYPE * iterator
Definition array.h:114
OpenSubdiv::OPENSUBDIV_VERSION::Vtr::Array::operator[]
reference operator[](int index)
Definition array.h:128
OpenSubdiv::OPENSUBDIV_VERSION::Vtr::Array::Array
Array()
Definition array.h:118