source: orange/source/orange/table.hpp @ 9410:0cea86d25c0b

Revision 9410:0cea86d25c0b, 4.8 KB checked in by janezd <janez.demsar@…>, 2 years ago (diff)

Renamed Domain.classes to Domain.class_vars; now they always exist, but may be empty
Domain's constructor now has a new keyword argument class_vars
ExampleTable now has pickClass for picking a new class and putting the current class attribute to class_vars

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 __TABLE_HPP
23#define __TABLE_HPP
24
25#include "examplegen.hpp"
26#include "random.hpp"
27
28class TFilter;
29
30// Example generator which stores examples in STL's vector.
31class ORANGE_API TExampleTable : public TExampleGenerator {
32public:
33  __REGISTER_CLASS
34
35  TExample **examples;
36  TExample **_Last, **_EndSpace;
37  PRandomGenerator randomGenerator; //P random generator used by randomExample
38  PExampleGenerator lock; //PR (+owner) the real owner of examples
39  bool ownsExamples; //PR (+owns_instances) if false, examples in this table are references to examples in another table
40
41  // Iterates through examples of basevector
42  #define baseITERATE(x) ITERATE(vector<TExample>, x, examples)
43
44  TExampleTable(PDomain, bool owns = true);
45  TExampleTable(PExampleGenerator orig, bool owns = true); // also copies examples
46  TExampleTable(PDomain, PExampleGenerator orig, bool filterMetas = false); // owns = true (cannot change domain of references); copies examples
47  TExampleTable(PExampleGenerator lock, int); // owns = false; pass anything for int; this constructor locks, but does not copy
48  TExampleTable(PExampleGeneratorList tables);
49  ~TExampleTable();
50
51
52public:
53  /* ExampleTable has some vector-like behaviour  */
54        TExample &at(const int &i);
55  const TExample &at(const int &i) const;
56        TExample &back();
57  const TExample &back() const;
58  bool            empty() const;
59        TExample &front();
60  const TExample &front() const;
61        TExample &operator[](const int &i);
62  const TExample &operator[](const int &i) const;
63            void reserve(const int &i);
64            void growTable();
65            void shrinkTable();
66             int size() const;
67            void erase(const int &sti);
68            void erase(const int &sti, const int &eni);
69
70            void push_back(TExample *x);
71            void erase(TExample **ptr);
72            void erase(TExample **fromPtr, TExample **toPtr);
73            void insert(const int &sti, const TExample &ex);
74
75
76  TExample &new_example();
77  void delete_last();
78
79  int traverse(visitproc visit, void *arg) const;
80  int dropReferences();
81
82  virtual TExampleIterator begin();
83  bool randomExample(TExample &);
84
85  virtual void changeDomain(PDomain, bool filterMetas = false);
86  virtual void pickClass(PVariable);
87
88protected:
89  virtual void increaseIterator(TExampleIterator &);
90  virtual void copyIterator(const TExampleIterator &, TExampleIterator &);
91  virtual bool sameIterators(const TExampleIterator &, const TExampleIterator &);
92
93public:
94  bool remove(TExampleIterator &it);
95
96  TValue operator ()(const TExample &);
97
98  virtual int  numberOfExamples();
99  virtual float weightOfExamples(const int &weightID = 0) const;
100
101  virtual void addExample(const TExample &, bool filterMetas = false);
102  virtual void addExample(TExample *);
103  virtual void addExamples(PExampleGenerator orig, bool filterMetas = false);
104
105  virtual bool removeExamples  (TFilter &); 
106  virtual bool removeExample   (TExample &);
107  virtual bool removeCompatible(TExample &);
108  virtual void removeDuplicates(const int &weightID=0);
109  virtual void clear();
110
111  void sort();
112  void sort(vector<int> &sortOrder);
113
114  void sortByPointers();
115 
116  void shuffle();
117 
118  virtual void addMetaAttribute(const int &id, const TValue &value);
119  virtual void copyMetaAttribute(const int &id, const int &source, TValue &defaultVal);
120  virtual void removeMetaAttribute(const int &id);
121
122  virtual int checkSum(const bool includeMetas = false);
123  virtual int checkSum(const bool includeMetas = false) const;
124  virtual bool hasMissing() const;
125  virtual bool hasMissingClass() const;
126};
127
128
129/* Returns example generator which can be referenced.
130   Function simply stores examples into TExampleTable if needed */
131inline PExampleGenerator fixedExamples(PExampleGenerator gen)
132{ return (&*gen->begin()==&*gen->begin()) ? gen : PExampleGenerator(mlnew TExampleTable(gen)); }
133
134/* Stores examples into TExampleTable if they are not into one already */
135inline PExampleGenerator toExampleTable(PExampleGenerator gen)
136{ return (gen.is_derived_from(TExampleTable) ? gen : PExampleGenerator(mlnew TExampleTable(gen))); }
137
138#endif
139
Note: See TracBrowser for help on using the repository browser.