File size: 6,849 Bytes
68ce2fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-    */
/* ex: set filetype=cpp softtabstop=4 shiftwidth=4 tabstop=4 cindent expandtab: */

/*

  Author(s):	Ofri Sadowsky, Anton Deguet
  Created on: 2004-07-01

  (C) Copyright 2004-2007 Johns Hopkins University (JHU), All Rights
  Reserved.

--- begin cisst license - do not edit ---

This software is provided "as is" under an open source license, with
no warranty.  The complete license can be found in license.txt and
http://www.cisst.org/cisst/license.txt.

--- end cisst license ---
*/

#pragma once
#ifndef _vctDynamicMatrixRefOwner_h
#define _vctDynamicMatrixRefOwner_h

/*!
  \file
  \brief Declaration of vctDynamicMatrixRefOwner
*/

#include <cisstVector/vctForwardDeclarations.h>
#include <cisstVector/vctVarStrideMatrixIterator.h>
#include <cisstVector/vctDynamicMatrixOwner.h>

/*!
  This templated class stores a pointer, a size, and a stride, and
  allows element access, but does not provide any other operations,
  and does not own the data */
template<class _elementType>
class vctDynamicMatrixRefOwner
{
public:
    /* define most types from vctContainerTraits */
    VCT_CONTAINER_TRAITS_TYPEDEFS(_elementType);
    enum {DIMENSION = 2};
    VCT_NARRAY_TRAITS_TYPEDEFS(DIMENSION);

    typedef vctDynamicMatrixRefOwner<value_type> ThisType;

    /* iterators are container specific */
    typedef vctVarStrideMatrixConstIterator<value_type> const_iterator;
    typedef vctVarStrideMatrixConstIterator<value_type> const_reverse_iterator;
    typedef vctVarStrideMatrixIterator<value_type> iterator;
    typedef vctVarStrideMatrixIterator<value_type> reverse_iterator;


    vctDynamicMatrixRefOwner():
        SizesMember(0, 0),
        StridesMember(1, 1),
        Data(0)
    {
        this->UpdateCachedData();
    }

    vctDynamicMatrixRefOwner(size_type rows, size_type cols,
                             stride_type rowStride, stride_type colStride,
                             pointer data):
        SizesMember(nsize_type(rows, cols)),
        StridesMember(nstride_type(rowStride, colStride)),
        Data(data)
    {
        this->UpdateCachedData();
    }

    void SetRef(size_type rows, size_type cols,
                pointer dataPointer,
                bool storageOrder) {
        this->SetRef(rows, cols,
                     storageOrder == VCT_ROW_MAJOR ? cols : 1,
                     storageOrder == VCT_ROW_MAJOR ? 1 : rows,
                     dataPointer);
    }

    void SetRef(const nsize_type & sizes,
                pointer dataPointer,
                bool storageOrder) {
        this->SetRef(sizes,
                     storageOrder == VCT_ROW_MAJOR ? sizes.Element(1) : 1,
                     storageOrder == VCT_ROW_MAJOR ? 1 : sizes.Element(0),
                     dataPointer);
    }

    void SetRef(size_type rows, size_type cols,
                stride_type rowStride, stride_type colStride,
                pointer data) {
        this->SetRef(nsize_type(rows, cols), nstride_type(rowStride, colStride), data);
    }

    void SetRef(const nsize_type & sizes,
                stride_type rowStride, stride_type colStride,
                pointer data) {
        this->SetRef(sizes, nstride_type(rowStride, colStride), data);
    }

    void SetRef(const nsize_type & sizes, const nstride_type & strides, pointer data) {
        SizesMember.Assign(sizes);
        StridesMember.Assign(strides);
        Data = data;
        this->UpdateCachedData();
    }

    size_type size(void) const {
        return SizesMember.ProductOfElements();
    }

    const nsize_type & sizes(void) const {
        return SizesMember;
    }

    size_type rows(void) const {
        return SizesMember.Element(0);
    }

    size_type cols(void) const {
        return SizesMember.Element(1);
    }

    const nstride_type & strides(void) const {
        return StridesMember;
    }

    difference_type row_stride(void) const {
        return StridesMember.Element(0);
    }

    difference_type col_stride(void) const {
        return StridesMember.Element(1);
    }

    pointer Pointer(index_type rowIndex, index_type colIndex) {
        return Data +  rowIndex * row_stride() + colIndex * col_stride();
    }

    pointer Pointer(void) {
        return Data;
    }

    const_pointer Pointer(index_type rowIndex, index_type colIndex) const {
        return Data +  rowIndex * row_stride() + colIndex * col_stride();
    }

    const_pointer Pointer(void) const {
        return Data;
    }

    const_iterator begin(void) const {
        return const_iterator(Data, col_stride(), cols(), row_stride());
    }

    const_iterator end(void) const {
        return const_iterator(Data, col_stride(), cols(), row_stride())
            + rows() * cols();
    }

    iterator begin(void) {
        return iterator(Data, col_stride(), cols(), row_stride());
    }

    iterator end(void) {
        return iterator(Data, col_stride(), cols(), row_stride())
            + rows() * cols();
    }

    const_reverse_iterator rbegin(void) const {
        return const_reverse_iterator(Data + row_stride() * (rows() - 1) + col_stride() * (cols() - 1),
                                      -col_stride(), cols(), -row_stride());
    }

    const_reverse_iterator rend(void) const {
        return const_reverse_iterator(Data - row_stride() + col_stride() * (cols() - 1),
                                      -col_stride(), cols(), -row_stride());
    }

    reverse_iterator rbegin(void) {
        return reverse_iterator(Data + row_stride() * (rows() - 1) + col_stride() * (cols() - 1),
                                -col_stride(), cols(), -row_stride());
    }

    reverse_iterator rend(void) {
        return reverse_iterator(Data - row_stride() + col_stride() * (cols() - 1),
                                -col_stride(), cols(), -row_stride());
    }

    inline bool IsColMajor(void) const {
        return (this->row_stride() <= this->col_stride());
    }

    inline bool IsRowMajor(void) const {
        return (this->col_stride() <= this->row_stride());
    }

    inline bool IsCompact(void) const {
        return IsCompactMember;
    }

    inline bool StorageOrder(void) const {
        return this->IsRowMajor();
    }

 protected:
    nsize_type SizesMember;
    nstride_type StridesMember;
    bool IsCompactMember;
    value_type* Data;

    inline void UpdateCachedData(void) {
        IsCompactMember = (((this->row_stride() == 1) && (this->col_stride() == static_cast<stride_type>(this->rows())))
                           || ((this->col_stride() == 1) && (this->row_stride() == static_cast<stride_type>(this->cols()))));
    }

private:
    // copy constructor private to prevent any call
    vctDynamicMatrixRefOwner(const ThisType & CMN_UNUSED(other)) {};

};


#endif // _vctDynamicMatrixRefOwner_h