source: orange/source/orange/examplegen.cpp @ 6531:57bdc92cd8e9

Revision 6531:57bdc92cd8e9, 5.6 KB checked in by janezd <janez.demsar@…>, 4 years ago (diff)
  • changed licenses to GPL 3.0
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#include "vars.hpp"
23#include "domain.hpp"
24
25#include "crc.h"
26
27#include "examplegen.ppp"
28
29DEFINE_TOrangeVector_classDescription(PExampleGenerator, "TExampleGeneratorList", true, ORANGE_API)
30
31
32int generatorVersion=0;
33
34TExampleGenerator::TExampleGenerator()
35: domain(),
36  version(-1)
37{}
38
39TExampleGenerator::TExampleGenerator(PDomain dom)
40: domain(dom),
41  version(-1)
42{}
43
44int TExampleGenerator::traverse(visitproc visit, void *arg) const
45{ TRAVERSE(TOrange::traverse);
46
47/* No need for this: iterators have just pointers, not references to generators!
48  if (!myIterators.empty())
49    myIterators.front()->generator.mark(myIterators.size());
50*/
51  const_ITERATE(list<iterator *>, ii, myIterators)
52    if ((*ii)->example==&((*ii)->privateExample))
53      TRAVERSE((*ii)->privateExample.traverse);
54  return 0;
55}
56
57
58int TExampleGenerator::dropReferences()
59{ DROPREFERENCES(TOrange::dropReferences);
60
61  while (myIterators.size())
62    deleteIterator(*myIterators.front());
63  return 0;
64}
65
66
67void TExampleGenerator::copyIterator(const TExampleIterator &, TExampleIterator &)
68{}
69
70void TExampleGenerator::deleteIterator(TExampleIterator &it)
71{ it.example = NULL; 
72  it.data = NULL;
73}
74
75// Returns an iterator pointing past the last example
76TExampleIterator TExampleGenerator::end()
77{ return TExampleIterator(this); }
78
79
80void TExampleGenerator::examplesHaveChanged()
81{ version = ++generatorVersion; }
82
83
84#define NOT_SUPPORTED(method) \
85{ raiseError("method '"#method"' is not supported"); }
86
87void TExampleGenerator::addMetaAttribute(const int &, const TValue &)
88NOT_SUPPORTED(addMetaAttribute)
89
90void TExampleGenerator::copyMetaAttribute(const int &, const int &, TValue &)
91NOT_SUPPORTED(copyMetaAttribute)
92
93void TExampleGenerator::removeMetaAttribute(const int &)
94NOT_SUPPORTED(removeMetaAttribute)
95
96
97int TExampleGenerator::checkSum(const bool includeMetas)
98{ unsigned long crc;
99  INIT_CRC(crc);
100  for(TExampleIterator ei(begin()); ei; ++ei)
101    (*ei).addToCRC(crc, includeMetas);
102  FINISH_CRC(crc);
103  return int(crc & 0x7fffffff);
104}
105
106
107float TExampleGenerator::weightOfExamples(const int &weightID) const
108{
109  raiseError("cannot compute the weight of examples");
110  return 0.0;
111}
112
113
114// Constructs the iterator, setting generator and data fields to the given values.
115TExampleIterator::TExampleIterator(TExampleGenerator *agen, TExample *anexam, void *adata) 
116: generator(agen),
117  example(anexam),
118  data(adata) 
119{ if (generator)
120    generator->myIterators.push_front(this);
121}
122
123
124TExampleIterator::TExampleIterator(const TExample &anexam, TExampleGenerator *agen, void *adata) 
125: generator(agen),
126  example(&privateExample),
127  data(adata),
128  privateExample(anexam)
129{ if (generator)
130    generator->myIterators.push_front(this);
131}
132
133TExampleIterator::TExampleIterator(PDomain domain, TExampleGenerator *agen, void *adata) 
134: generator(agen), example(&privateExample), data(adata), privateExample(domain)
135{ if (generator)
136    generator->myIterators.push_front(this);
137}
138
139TExampleIterator::TExampleIterator(const TExampleIterator &other)
140: generator(other.generator),
141  example(other.example==&other.privateExample ? &privateExample : other.example),
142  data(NULL),
143  privateExample(other.example==&other.privateExample ? other.privateExample : TExample())
144{ if (generator) {
145    generator->copyIterator(other, *this); 
146    generator->myIterators.push_front(this);
147  }
148}
149
150
151int TExampleIterator::traverse(visitproc visit, void *arg) const
152{ /* Iterator has an unwrapped pointer to generator.
153     We would have to call generator->traverse if it would not be wrapped.
154     However, all generators are expected to be wrapped (sooner or later)
155     so this won't cause a permanent memory leak. */
156   return (example==&privateExample) ? example->traverse(visit, arg) : 0;
157}
158
159
160int TExampleIterator::dropReferences()
161{ return (example==&privateExample) ? example->dropReferences() : 0;
162}
163
164
165TExampleIterator::~TExampleIterator()   
166{ 
167  if (generator) {
168    generator->deleteIterator(*this); 
169    generator->myIterators.remove(this);
170  }
171}
172
173
174TExampleIterator &TExampleIterator::operator =(const TExampleIterator &other)
175{ 
176  if (other.example==&other.privateExample) {
177    privateExample = other.privateExample;
178    example = &privateExample;
179  }
180  else {
181    privateExample = TExample();
182    example = other.example;
183  }
184
185  if (generator != other.generator) {
186    if (generator) {
187      generator->deleteIterator(*this);
188      generator->myIterators.remove(this);
189    }
190   
191    generator = other.generator;
192    generator->copyIterator(other, *this);
193    generator->myIterators.push_back(this);
194  }
195
196  return *this;
197}
198
199
200bool TExampleIterator::operator == (const TExampleIterator &other)
201{ return (generator==other.generator) && (generator->sameIterators(*this, other)); }
202
203bool TExampleIterator::operator != (const TExampleIterator &other)
204{ return (generator!=other.generator) || (!generator->sameIterators(*this, other)); }
205
Note: See TracBrowser for help on using the repository browser.