File: GetSet.hpp

package info (click to toggle)
freemat 4.2%2Bdfsg1-6
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 142,116 kB
  • sloc: ansic: 126,788; cpp: 62,015; python: 2,080; perl: 1,255; sh: 1,146; yacc: 1,019; lex: 239; makefile: 107
file content (225 lines) | stat: -rw-r--r-- 6,484 bytes parent folder | download | duplicates (4)
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
/*
 * Copyright (c) 2009 Samit Basu
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */
#ifndef __GetSet_hpp__
#define __GetSet_hpp__

#include "IndexArray.hpp"

template <typename T>
void SetSlice(T& arg, const IndexArrayVector& index, const T& data) {
  index_t offset = getSliceIndex(arg.dimensions(),index);
  for (index_t i=1;i<=arg.dimensions()[0];i++)
    arg.set(offset+i,data.get(i));
}

template <typename T, typename S>
void SetSlice(T& arg, const IndexArrayVector& index, const S& data) {
  index_t offset = getSliceIndex(arg.dimensions(),index);
  for (index_t i=1;i<=arg.dimensions()[0];i++)
    arg.set(offset+i,data);  
}

template <typename T>
void Set(T& arg, const IndexArray& index, const T& data) {
  if (index.isEmpty()) return;
  if (data.isEmpty()) {
    arg.del(index);
    return;
  }
  if (IsColonOp(index)) {
    NTuple myDims;
    if (!arg.isEmpty())
      myDims = arg.dimensions();
    else
      myDims = data.dimensions();
    if (myDims.count() != data.length()) 
      throw Exception("assignment A(:) = B requires A and B to be the same size");
    T retvec(data);
    retvec.reshape(myDims);
    arg = retvec;
    return;
  }
  index_t max_ndx = MaxValue(index);
  if (max_ndx > arg.length()) arg.resize(max_ndx);
  if (index.length() != data.length()) 
    throw Exception("Assignment A(I) = B requires I and B to be the same size");
  for (index_t i=1;i<=index.length();i++)
    arg.set(index.get(i),data.get(i));
}


template <typename T>
void Set(T& arg, const IndexArrayVector& index, const T& data) {
  if (index.empty()) return;
  if (data.isEmpty()) {
    arg.del(index);
    return;
  }
  if (!arg.isEmpty() && isSliceIndexCase(arg.dimensions(),index)) {
    SetSlice(arg,index,data);
    return;
  }
  IndexArrayVector ndx;
  NTuple secdims;
  NTuple maxsze;
  if (arg.isEmpty()) {
    // Special case when A(NDX) = B, and A is empty
    if (data.isVector()) {
      bool firstcolon = true;
      for (int i=0;i<index.size();i++)
	if (IsColonOp(index[i])) {
	  if (firstcolon) {
	    ndx.push_back(ExpandColons(index[i],data.length()));
	    firstcolon = false;
	  } else
	    ndx.push_back(ScalarToIndex(1));
	} else {
	  ndx.push_back(index[i]);
	}
    } else {
      // Correction - 
      // In the assignment of the form:
      //  g(2,:,4,:) = fs;
      // we fill in the colons with the first and second dimensional sizes of fs
      int colonDim = 0;
      for (int i=0;i<index.size();i++) {
	if (IsColonOp(index[i]))
	  ndx.push_back(ExpandColons(index[i],data.dimensions()[colonDim++]));
	else
	  ndx.push_back(index[i]);
      }
    }
    for (int i=0;i<ndx.size();i++) {
      secdims[i] = ndx[i].length();
      maxsze[i] = MaxValue(ndx[i]);
    }
  } else {
    for (int i=0;i<index.size();i++) {
      ndx.push_back(ExpandColons(index[i],arg.dimensions()[i]));
      secdims[i] = ndx[i].length();
      maxsze[i] = MaxValue(ndx[i]);
    }
  }
  
  if (secdims.count() != data.length())
    throw Exception("mismatch in size for assignment A(I1,I2,...) = B");
  if (!(maxsze <= arg.dimensions()))
    arg.resize(max(arg.dimensions(),maxsze));
  NTuple pos(1,1);
  index_t j=1;
  while (pos <= secdims) {
    NTuple qp;
    for (int i=0;i<index.size();i++)
      qp[i] = ndx[i].get(pos[i]);
    arg.set(qp,data.get(j++));
    secdims.increment(pos);
  }
}

template <typename T, typename S>
void Set(T& arg, const IndexArrayVector& index, const S& data) {
  if (index.empty()) return;
  if (!arg.isEmpty() && isSliceIndexCase(arg.dimensions(),index)) {
    SetSlice(arg,index,data);
    return;
  }
  IndexArrayVector ndx;
  NTuple secdims;
  NTuple maxsze;
  for (int i=0;i<index.size();i++) {
    if (arg.isEmpty())
      ndx.push_back(ExpandColons(index[i],1));
    else
      ndx.push_back(ExpandColons(index[i],arg.dimensions()[i]));
    secdims[i] = ndx[i].length();
    maxsze[i] = MaxValue(ndx[i]);
  }
  if (!(maxsze <= arg.dimensions()))
    arg.resize(max(arg.dimensions(),maxsze));
  NTuple pos(1,1);
  while (pos <= secdims) {
    NTuple qp;
    for (int i=0;i<index.size();i++)
      qp[i] = ndx[i].get(pos[i]);
    arg.set(qp,data);
    secdims.increment(pos);
  }
}

template <typename T, typename S>
void Set(T& arg, const IndexArray& index, const S& data) {
  if (index.isEmpty()) return;
  if (IsColonOp(index)) {
    if (arg.isEmpty())
      arg.set(1,data);
    else
      for (index_t i=1;i<=arg.length();i++) 
	arg.set(i,data);
    return;
  } 
  index_t max_ndx = MaxValue(index);
  if (max_ndx > arg.length()) arg.resize(max_ndx);
  for (index_t i=1;i<=index.length();i++)
    arg.set(index.get(i),data);  
}

template <typename T>
const T Get(const T& arg, const IndexArray& index) {
  if (index.isEmpty()) return T(index.dimensions());
  if (IsColonOp(index)) {
    T retvec(arg);
    retvec.reshape(NTuple(arg.length(),1));
    return retvec;
  }
  if (arg.isEmpty())
    throw Exception("Cannot index into empty variable,");
  NTuple retdims;
  if (arg.isColumnVector() && index.isRowVector())
    retdims = NTuple(index.length(),1);
  else
    retdims = index.dimensions();
  T retvec(retdims);
  for (index_t i=1;i<=retvec.length();i++)
    retvec.set(i,arg.get(index.get(i)));
  return retvec;
}

template <typename T>
const T Get(const T& arg, const IndexArrayVector& index) {
  if (index.empty()) return T(NTuple(0,0));
  if (isSliceIndexCase(arg.dimensions(),index)) return arg.slice(index);
  IndexArrayVector ndx;
  NTuple outdims;
  for (int i=0;i<index.size();i++) {
    ndx.push_back(ExpandColons(index[i],arg.dimensions()[i]));
    outdims[i] = ndx[i].length();
  }
  T retval(outdims);
  NTuple pos(1,1);
  while (pos <= outdims) {
    NTuple qp;
    for (int i=0;i<index.size();i++) 
      qp[i] = ndx[i].get(pos[i]);
    retval.set(pos,arg.get(qp));
    outdims.increment(pos);
  }
  return retval;
}

#endif