OpenSubdiv: opensubdiv/vtr/array.h Source File

| 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>
36 class ConstArray {
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
63 size_type FindIndexIn4Tuple(value_type value) const {
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
73 size_type FindIndex(value_type value) const {
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:
83 value_type const * _begin;
84 size_type _size;
85};
86
87template <typename TYPE>
88 class 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
111 reference operator[](int index) {
112 return const_cast<reference>(ConstArray<TYPE>::_begin[index]);
113 }
114
115 iterator begin() {
116 return const_cast<iterator>(ConstArray<TYPE>::_begin);
117 }
118
119 iterator end() {
120 return const_cast<iterator>(ConstArray<TYPE>::_begin +
121 ConstArray<TYPE>::_size);
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 */
OpenSubdiv
Definition limits.h:15
OpenSubdiv::OPENSUBDIV_VERSION
Definition limits.h:16
OpenSubdiv::OPENSUBDIV_VERSION::Vtr
Definition topologyRefiner.h:23
OpenSubdiv::OPENSUBDIV_VERSION::Vtr::ConstArray< int >::_begin
value_type const * _begin
Definition array.h:83
OpenSubdiv::OPENSUBDIV_VERSION::Vtr::ConstArray< int >::value_type
int value_type
Definition array.h:39
OpenSubdiv::OPENSUBDIV_VERSION::Vtr::ConstArray::begin
const_iterator begin() const
Definition array.h:60
OpenSubdiv::OPENSUBDIV_VERSION::Vtr::ConstArray::operator[]
const_reference operator[](int index) const
Definition array.h:59
OpenSubdiv::OPENSUBDIV_VERSION::Vtr::ConstArray< int >::const_iterator
int const * const_iterator
Definition array.h:43
OpenSubdiv::OPENSUBDIV_VERSION::Vtr::ConstArray::FindIndexIn4Tuple
size_type FindIndexIn4Tuple(value_type value) const
Definition array.h:63
OpenSubdiv::OPENSUBDIV_VERSION::Vtr::ConstArray::size
size_type size() const
Definition array.h:55
OpenSubdiv::OPENSUBDIV_VERSION::Vtr::ConstArray::empty
bool empty() const
Definition array.h:57
OpenSubdiv::OPENSUBDIV_VERSION::Vtr::ConstArray::ConstArray
ConstArray(value_type const *ptr, size_type sizeArg)
Definition array.h:52
OpenSubdiv::OPENSUBDIV_VERSION::Vtr::ConstArray< int >::reference
int & reference
Definition array.h:45
OpenSubdiv::OPENSUBDIV_VERSION::Vtr::ConstArray< int >::_size
size_type _size
Definition array.h:84
OpenSubdiv::OPENSUBDIV_VERSION::Vtr::ConstArray::FindIndex
size_type FindIndex(value_type value) const
Definition array.h:73
OpenSubdiv::OPENSUBDIV_VERSION::Vtr::ConstArray< int >::size_type
int size_type
Definition array.h:40
OpenSubdiv::OPENSUBDIV_VERSION::Vtr::ConstArray< int >::const_reference
int const & const_reference
Definition array.h:42
OpenSubdiv::OPENSUBDIV_VERSION::Vtr::ConstArray::end
const_iterator end() const
Definition array.h:61
OpenSubdiv::OPENSUBDIV_VERSION::Vtr::ConstArray< int >::iterator
int * iterator
Definition array.h:46
OpenSubdiv::OPENSUBDIV_VERSION::Vtr::ConstArray::ConstArray
ConstArray()
Definition array.h:50
OpenSubdiv::OPENSUBDIV_VERSION::Vtr::Array< PatchParam >::value_type
PatchParam value_type
Definition array.h:91
OpenSubdiv::OPENSUBDIV_VERSION::Vtr::Array::operator[]
const_reference operator[](int index) const
Definition array.h:107
OpenSubdiv::OPENSUBDIV_VERSION::Vtr::Array< PatchParam >::reference
PatchParam & reference
Definition array.h:96
OpenSubdiv::OPENSUBDIV_VERSION::Vtr::Array< PatchParam >::size_type
int size_type
Definition array.h:92
OpenSubdiv::OPENSUBDIV_VERSION::Vtr::Array< PatchParam >::const_reference
PatchParam const & const_reference
Definition array.h:94
OpenSubdiv::OPENSUBDIV_VERSION::Vtr::Array::end
iterator end()
Definition array.h:119
OpenSubdiv::OPENSUBDIV_VERSION::Vtr::Array::begin
iterator begin()
Definition array.h:115
OpenSubdiv::OPENSUBDIV_VERSION::Vtr::Array::Array
Array(value_type *ptr, size_type sizeArg)
Definition array.h:103
OpenSubdiv::OPENSUBDIV_VERSION::Vtr::Array< PatchParam >::iterator
PatchParam * iterator
Definition array.h:97
OpenSubdiv::OPENSUBDIV_VERSION::Vtr::Array::operator[]
reference operator[](int index)
Definition array.h:111
OpenSubdiv::OPENSUBDIV_VERSION::Vtr::Array::Array
Array()
Definition array.h:101