source: orange/source/orange/examples.hpp @ 9401:412768944677

Revision 9401:412768944677, 4.5 KB checked in by janezd <janez.demsar@…>, 2 years ago (diff)

Added multiple classes (Ticket #1012)

Line 
1/*
2    This file is part of Orange.
3   
4    Copyright 1996-2010 Faculty of Computer and Information Science, University of Ljubljana
5    Contact: janez.demsar@fri.uni-lj.si
6
7    Orange is free software: you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation, either version 3 of the License, or
10    (at your option) any later version.
11
12    Orange is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with Orange.  If not, see <http://www.gnu.org/licenses/>.
19*/
20
21
22#ifndef __EXAMPLES_HPP
23#define __EXAMPLES_HPP
24
25#include "meta.hpp"
26#include "orvector.hpp"
27WRAPPER(Domain)
28WRAPPER(Example)
29
30#define TExampleList TOrangeVector<PExample>
31VWRAPPER(ExampleList)
32
33extern long exampleId;
34ORANGE_API long getExampleId();
35
36// A vector of attribute and class values
37class ORANGE_API TExample : public TOrange {
38public:
39  __REGISTER_CLASS
40
41  PDomain domain; //PR Example's domain
42  TValue *values, *values_end, *classes_end;
43  TMetaValues meta;
44  string *name;
45  int id; //P
46
47  TExample();
48  TExample(PDomain dom, bool initMetas = true);
49  TExample(PDomain dom, const TExample &orig, bool copyMetas = true);
50  TExample(const TExample &orig, bool copyMetas = true);
51  TExample(PDomain dom, PExampleList);
52  virtual ~TExample();
53
54private:
55  void insertVal(TValue &srcval, PVariable var, const long &metaID, vector<bool> &defined);
56
57public:
58  int traverse(visitproc visit, void *arg) const;
59  int dropReferences();
60
61  virtual TExample &operator =(const TExample &orig);
62
63  typedef TValue *iterator;
64  typedef const TValue *const_iterator;
65
66  inline TValue *begin()
67  { return values; }
68
69  inline TValue *begin() const
70  { return values; }
71
72  inline TValue *end()
73  { return values_end; }
74
75  inline TValue *end() const
76  { return values_end; }
77
78
79  TValue &operator[] (const int &i)
80  { return i>=0 ? values[i] : getMeta(i); }
81
82  const TValue &operator[] (const int &i) const
83  { return i>=0 ? values[i] : getMetaIfExists(i); }
84
85  // this is duplicated from above, but can be used on non-const examples
86  const TValue &getValue(const int &i)
87  { return i>=0 ? values[i] : getMetaIfExists(i); }
88
89  TValue &operator[] (PVariable &var);
90  const TValue &operator[] (PVariable &var) const;
91  TValue &operator[] (const string &name);
92  const TValue &operator[] (const string &name) const;
93
94  // this one is similar to the above operator, but calls computeValue if needed
95  // (if [] returned it, the computed value would be a reference to a temporary)
96  TValue getValue(PVariable &var) const;
97
98  TValue &getClass()
99  { return values_end[-1]; }
100
101  TValue &getClass() const
102  { return values_end[-1]; }
103
104  void setClass(const TValue &val)
105  { values_end[-1] = val; }
106
107
108  const TValue &missingMeta(const int &i) const;
109
110  // these two require that meta is defined
111  const TValue &getMeta(const int &i) const
112  { 
113    return meta[i];
114  }
115
116  TValue &getMeta(const int &i)
117  { 
118    return meta[i];
119  }
120
121  // this one will set it to undefined if it is optional and undefined
122/*  TValue &getMetaIfExists(const int &i)
123  {
124    TValue &val = meta.getValueIfExists(i);
125    return &val != &missingMetaValue ? val : missingMeta(i);
126  }
127*/
128
129  const TValue &getMetaIfExists(const int &i) const
130  { 
131    const TValue &val = meta.getValueIfExists(i);
132    return &val != &missingMetaValue ? val : missingMeta(i);
133  }
134
135
136
137
138  bool hasMeta(const int &i) const
139  { return meta.exists(i); }
140
141  void setMeta(const int &i, const TValue &val)
142  { meta.setValue(i, val); }
143
144  void removeMeta(const int &i)
145  { meta.removeValue(i); }
146
147  void removeMetaIfExists(const int &i)
148  { meta.removeValueIfExists(i); }
149
150  void removeMetas()
151  { meta.clear(); }
152
153  bool operator < (const TExample &) const;
154  bool operator == (const TExample &) const;
155  int  compare(const TExample &other, const bool ignoreClass = false) const;
156  bool compatible(const TExample &other, const bool ignoreClass = false) const;
157
158  void addToCRC(unsigned long &crc, const bool includeMetas = false) const;
159  int sumValues(const bool includeMetas = false) const;
160 
161  bool hasMissing() const
162  {
163    for(TValue const *vi = values, *ve = classes_end; vi!=ve; vi++)
164      if (vi->isSpecial())
165        return true;
166    return false;
167  }
168 
169  bool missingClass() const
170  { return values_end[-1].isSpecial(); }
171};
172
173
174WRAPPER(Example)
175
176#endif
177
Note: See TracBrowser for help on using the repository browser.