File: Struct.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-- 5,550 bytes parent folder | download | duplicates (2)
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 __Struct_hpp__
#define __Struct_hpp__

#include "BasicArray.hpp"
#include "Types.hpp"

class Array;
class Interpreter;

// A twist on the ever-useful structure array class.
// Now, if the structure is of the handle type (i.e.,
// a pointer), then the data is stored in a struct object
// that is stored on the heap instead of inside the structure
// object itself.  This trick allows multiple variables to 
// point to and to modify the same physical data structure
// effectively subverting the value semantics that variables
// in FreeMat normally have.

class StructArray;

class SARefPtr
{
  StructArray *m_ptr;
  Interpreter *m_eval;
  SARefPtr& operator=(const SARefPtr& copy);
public:
  SARefPtr(StructArray* p, Interpreter *eval);
  ~SARefPtr();
  SARefPtr(const SARefPtr& copy);
  StructArray& operator*();
  StructArray* operator->();
  const StructArray& operator*() const;
  const StructArray* operator->() const;
};

class StructArray {
  StringVector m_fields;
  QVector<BasicArray<Array> > m_data;
  QString m_class;
  NTuple m_dims;
  bool m_handleType;
  uint32 m_refCount;
  SARefPtr m_ptr;
public:
  StructArray() : m_handleType(false), m_refCount(0), m_ptr(0,0) {}
  StructArray(StructArray* ptr, Interpreter* eval) : 
    m_handleType(true), m_ptr(ptr,eval) {}
  void ref();
  void deref();
  uint32 getRefCount() const
  {
    if (isHandleClass())
      return m_ptr->getRefCount();
    return m_refCount;
  }
  bool isHandleClass() const
  {
    return m_handleType;
  }
  bool isUserClass() const 
  {
    if (isHandleClass())
      return m_ptr->isUserClass();
    return !m_class.isEmpty();
  }
  QString className() const 
  {
    if (isHandleClass())
      return m_ptr->className();
    return m_class;
  }
  void setClassName(const QString& className) 
  {
    if (isHandleClass())
      return m_ptr->setClassName(className);
    m_class = className;
  }
  StringVector fieldNames() const 
  {
    if (isHandleClass())
      return m_ptr->fieldNames();
    return m_fields;
  }
  void setFieldNamesAndData(const StringVector& fields,
			    const QVector<BasicArray<Array> > data) 
  {
    if (isHandleClass())
      {
	m_ptr->setFieldNamesAndData(fields,data);
        return;
      }
    m_fields = fields;
    m_data = data;
    updateDims();
  }
  int fieldCount() const 
  {
    if (isHandleClass())
      return m_ptr->fieldCount();
    return m_fields.size();
  }
  QString fieldName(int i) const 
  {
    if (isHandleClass())
      return m_ptr->fieldName(i);
    return m_fields[i];
  }
  index_t bytes() const 
  {
    if (isHandleClass())
      return m_ptr->bytes();
    index_t count = 0;
    for (int i=0;i<m_fields.size();i++) {
      count += m_fields[i].size()*sizeof(QChar);
      count += m_data[i].bytes();
    }
    return count;
  }
  int fieldIndex(QString name) const 
  {
    if (isHandleClass())
      return m_ptr->fieldIndex(name);
    if (m_fields.contains(name)) 
      return m_fields.indexOf(name);
    throw Exception("Fieldname " + name + " not defined");
  }
  bool contains(QString name) const 
  { 
    if (isHandleClass())
      return m_ptr->contains(name);
    return m_fields.contains(name); 
  }
  void insert(QString name, const BasicArray<Array> &t) 
  {
    if (isHandleClass())
      return m_ptr->insert(name,t);
    if (!contains(name)) {
      m_fields += name;
      m_data.push_back(t);
    } else {
      m_data[fieldIndex(name)] = t;
    }
    updateDims();
  }
  void insert(QString name, const Array &t)
  {
    BasicArray<Array> ba(t);
    insert(name,ba);
  }
  NTuple dimensions() const 
  {
    if (isHandleClass())
      return m_ptr->dimensions();
    return m_dims;
  }
  const index_t length() const 
  {
    if (isHandleClass())
      return m_ptr->length();
    return m_dims.count();
  }
  void setDimensions(const NTuple &x) 
  {
    if (isHandleClass())
      return m_ptr->setDimensions(x);
    m_dims = x;
  }
  void updateDims() 
  {
    if (isHandleClass())
      return m_ptr->updateDims();
    if (m_data.size() == 0)
      m_dims = NTuple(0,0);
    else
      m_dims = m_data[0].dimensions();
  }
  BasicArray<Array>& operator[](int i) 
  {
    if (isHandleClass())
      return (*m_ptr)[i];
    return m_data[i];
  }
  const BasicArray<Array>& operator[](int i) const 
  {
    if (isHandleClass())
      return (*m_ptr)[i];
    return m_data[i];
  }
  BasicArray<Array>& operator[](QString name) 
  {
    if (isHandleClass())
      return (*m_ptr)[name];
    if (!m_fields.contains(name)) {
      m_fields += name;
      m_data.push_back(BasicArray<Array>());
    }
    return m_data[fieldIndex(name)];
  }
  const BasicArray<Array>& operator[](QString name) const 
  {
    if (isHandleClass())
      return (*m_ptr)[name];
    return m_data[fieldIndex(name)];
  }
};

#endif