source: orange/source/orange/table.hpp @ 11048:782c9d850e72

Revision 11048:782c9d850e72, 4.9 KB checked in by Ales Erjavec <ales.erjavec@…>, 16 months ago (diff)

Fixed 'TExampleTable.clone' method (erroneous copy constructor for TExampleTable).

The 'clone' method calls the C++ copy constructor to copy the table,
but the default copy constructor fails to properly copy the instances
(both tables end up claiming ownership of the same memory space).

Fixed by implementing a copy constructor to make a deep copy of
the instances.

Fixes #1260

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