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