source: orange/source/orange/examplegen.hpp @ 10960:2bb772691bd8

Revision 10960:2bb772691bd8, 6.3 KB checked in by Ales Erjavec <ales.erjavec@…>, 21 months ago (diff)

Fixes for clang compiler support.

Added 'TOrangeVector::classDescription()' specialization to
'DEFINE_TOrangeVector_classDescription' macro and removed its default implementation
in the template class (static 'st_classDescription' member must be initialized/specialized before
its use).

A similar problem arises in the included '.ppp' files where st_classDescription is used. So I
moved all '.ppp. includes after the "DEFINE_*"s.

Removed unnecessary/duplicate VWRAPPER/WRAPPER uses.

Added 'this->...' qualifier in template classes (that inherit a from a template class) for
accessing base class methods, otherwise clang does not find them (as per C++ standard).

Moved the TVarList and TAttributed*List definitions/implementations from 'orvector' to 'vars'.
Fixed includes in other files using the two headers accordingly.

Fixed some casts to int with more appropriate casts to size_t/uintptr_t.

Added explicit casts of constants to template class in include/stat.hpp
statistics functions.

(references #1219).

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 __EXAMPLE_GEN_HPP
23#define __EXAMPLE_GEN_HPP
24
25#include <list>
26#include "root.hpp"
27#include "domain.hpp"
28
29using namespace std;
30
31const int
32  NOEX_DONT_KNOW=-1,   
33  /* number of examples is small enough to be explored */                       
34  NOEX_TRACTABLE=-2, 
35  /* there's a lot of examples; randomExample is recommended if it exists */   
36  NOEX_FINITE=-3,
37  /* iterators might not be useful since they would generate an infinite number of examples */
38  NOEX_INFINITE=-4;
39
40
41
42class ORANGE_API TExampleIterator;
43
44extern int generatorVersion;
45
46/*  A base class for objects that 'generate' examples, which can be traversed using iterators.
47    Iterators that point to the first example and beyond the last example are returned by
48    begin() and end() methods. We recommend to use them with copy constructors, for example
49    for(TExampleTable::iterator i(table.begin()); i!=table.end(); i++)
50    and not
51    for(TExampleTable::iterator i=table.begin(); ...
52    The second form can be inefficient or even cause problems under some compilers, if
53    iterator handling methods are not written well. */
54
55class ORANGE_API TExampleGenerator : public TOrange {
56public:
57  __REGISTER_ABSTRACT_CLASS
58
59  PDomain domain; //P domain
60  int version; //PR unique version identifier
61
62  typedef TExampleIterator iterator;
63
64  #ifdef _MSC_VER
65    #pragma warning(push)
66    #pragma warning(disable: 4251)
67  #endif
68
69  list<iterator *> myIterators;
70
71  #ifdef _MSC_VER
72    #pragma warning(pop)
73  #endif
74
75  TExampleGenerator();
76  TExampleGenerator(PDomain dom);
77
78  int traverse(visitproc visit, void *arg) const;
79  int dropReferences();
80
81  virtual TExampleIterator begin() =0;
82  virtual TExampleIterator end();
83
84  virtual bool randomExample(TExample &) =0;
85
86  /*  An abstract method which returns the number or estimate of number of examples.
87      That is, when exact number is not known, it can return NOEX_TRACTABLE (if number of examples is
88      known to be 'small', for instance when examples are read from the file), NOEX_FINITE (when number
89      of examples is finite but can be large -- for instance, when the example space is completely covered),
90      NOEX_INFINITE (when the number is known to be infinite) or NOEX_DONT_KNOW when nothing can be said about it. */
91  virtual int numberOfExamples() =0;
92
93  virtual float weightOfExamples(const int &weightID = 0) const;
94
95  /*  Iterators handling methods should mostly be defined in derived classes. Methods
96      increaseIterator and sameIterators are abstract, while deleteIterator does nothing and
97      'copyIterator' throws an error saying that 'Iterators of this type cannot be copied.  */
98  virtual void increaseIterator(TExampleIterator &)=0;
99  virtual bool sameIterators(const TExampleIterator &, const TExampleIterator &)=0;
100  virtual void deleteIterator(TExampleIterator &);
101  virtual void copyIterator(const TExampleIterator &source, TExampleIterator &dest);
102
103  void examplesHaveChanged();
104
105  virtual void addMetaAttribute(const int &id, const TValue &value);
106  virtual void copyMetaAttribute(const int &id, const int &source, TValue &defaultVal);
107  virtual void removeMetaAttribute(const int &id);
108
109  virtual int checkSum(const bool includeMetas=false);
110};
111
112
113WRAPPER(ExampleGenerator);
114
115#define TExampleGeneratorList TOrangeVector<PExampleGenerator>
116VWRAPPER(ExampleGeneratorList)
117
118
119#include "examples.hpp"
120
121/*  TExampleIterator is a pointer-like object, with similar functionality as iterators in STL.
122    It can be dereferenced to get an example, increased (to point to the next examples), and compared
123    with other examples (to see if they point to the same example).
124    It is used to access (by iterating through) the examples in a generator. */
125class TExampleIterator {
126public:
127  // A pointer to the corresponding generator. Although public, the use this field should be avoided, if possible.
128  TExampleGenerator *generator;
129  /*  An example that iterator points to; this can be a pointer to a copy stored in generator or to privateExample.
130      The only situation in which this would point to a specially allocated copy in memory would be when it needs to point
131      to an instance of a class derived from TExample (such as TExampleForMissing).
132      If NULL, iterator points beyond the last example (i.e. equals end()). */
133  TExample *example;
134  // Used by the generator to store the additional data it needs
135  void *data;
136
137  TExample privateExample;
138
139  // Constructs the iterator, setting generator and data fields to the given values.
140  TExampleIterator(TExampleGenerator *agen=NULL, TExample *anexam=NULL, void *adata =NULL);
141  TExampleIterator(const TExample &anexam, TExampleGenerator *agen=NULL, void *adata =NULL);
142  TExampleIterator(PDomain domain, TExampleGenerator *agen=NULL, void *adata =NULL);
143  TExampleIterator(const TExampleIterator &other);
144  int traverse(visitproc visit, void *arg) const;
145  int dropReferences();
146  ~TExampleIterator();
147
148  TExampleIterator &operator =(const TExampleIterator &other);
149
150  bool operator == (const TExampleIterator &other);
151  bool operator != (const TExampleIterator &other);
152
153  inline TExampleIterator &TExampleIterator::operator ++ ()
154  { if (!example)
155      raiseErrorWho("exampleIterator", "out of range");
156    generator->increaseIterator(*this);
157    return *this;
158  }
159
160  inline TExample &operator *() const
161  { return *example; }
162
163  inline operator bool() const
164  { return example!=NULL; }
165};
166
167
168/* A macro for iterating through the examples of generator */
169#define EITERATE(it,co) for(TExampleIterator it((co).begin()); it; ++it)
170#define PEITERATE(it,co) for(TExampleIterator it((co)->begin()); it; ++it)
171
172#endif
Note: See TracBrowser for help on using the repository browser.