labplot  2.8.2
About: LabPlot is an application for plotting and analysis of 2D and 3D functions and data. It is a complete rewrite of LabPlot1 and lacks in the first release a lot of features available in the predecessor. On the other hand, the GUI and the usability is more superior.
  Fossies Dox: labplot-2.8.2.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

IntervalAttribute.h
Go to the documentation of this file.
1 /***************************************************************************
2  File : IntervalAttribute.h
3  Project : LabPlot
4  --------------------------------------------------------------------
5 
6  Copyright : (C) 2007 by Knut Franke (knut.franke@gmx.de)
7  Copyright : (C) 2007 by Tilman Benkert (thzs@gmx.net)
8  Description : A class representing an interval-based attribute
9 
10  ***************************************************************************/
11 
12 /***************************************************************************
13  * *
14  * This program is free software; you can redistribute it and/or modify *
15  * it under the terms of the GNU General Public License as published by *
16  * the Free Software Foundation; either version 2 of the License, or *
17  * (at your option) any later version. *
18  * *
19  * This program is distributed in the hope that it will be useful, *
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
22  * GNU General Public License for more details. *
23  * *
24  * You should have received a copy of the GNU General Public License *
25  * along with m_intervals program; if not, write to the Free Software *
26  * Foundation, Inc., 51 Franklin Street, Fifth Floor, *
27  * Boston, MA 02110-1301 USA *
28  * *
29  ***************************************************************************/
30 
31 #ifndef INTERVALATTRIBUTE_H
32 #define INTERVALATTRIBUTE_H
33 
34 #include "Interval.h"
35 #include <QVector>
36 
37 //! A class representing an interval-based attribute
38 template<class T> class IntervalAttribute {
39 public:
40  void setValue(const Interval<int>& i, T value) {
41  // first: subtract the new interval from all others
42  QVector< Interval<int> > temp_list;
43  for (int c = 0; c < m_intervals.size(); c++) {
44  temp_list = Interval<int>::subtract(m_intervals.at(c), i);
45  if (temp_list.isEmpty()) {
46  m_intervals.removeAt(c);
47  m_values.removeAt(c--);
48  } else {
49  m_intervals.replace(c, temp_list.at(0));
50  if (temp_list.size() > 1) {
51  m_intervals.insert(c, temp_list.at(1));
52  m_values.insert(c, m_values.at(c));
53  }
54  }
55  }
56 
57  // second: try to merge the new interval with an old one
58  for (int c = 0; c < m_intervals.size(); c++) {
59  if (m_intervals.at(c).touches(i) && m_values.at(c) == value) {
60  m_intervals.replace(c, Interval<int>::merge(m_intervals.at(c), i));
61  return;
62  }
63  }
64  // if it could not be merged, just append it
65  m_intervals.append(i);
66  m_values.append(value);
67  }
68 
69  // overloaded for convenience
70  void setValue(int row, T value)
71  {
72  setValue(Interval<int>(row, row), value);
73  }
74 
75  T value(int row) const
76  {
77  for(int c=m_intervals.size()-1; c>=0; c--)
78  {
79  if(m_intervals.at(c).contains(row))
80  return m_values.at(c);
81  }
82  return T();
83  }
84 
85  void insertRows(int before, int count)
86  {
87  QVector< Interval<int> > temp_list;
88  // first: split all intervals that contain 'before'
89  for(int c=0; c<m_intervals.size(); c++)
90  {
91  if(m_intervals.at(c).contains(before))
92  {
93  temp_list = Interval<int>::split(m_intervals.at(c), before);
94  m_intervals.replace(c, temp_list.at(0));
95  if(temp_list.size()>1)
96  {
97  m_intervals.insert(c, temp_list.at(1));
98  m_values.insert(c, m_values.at(c));
99  c++;
100  }
101 
102  }
103  }
104  // second: translate all intervals that start at 'before' or later
105  for(int c=0; c<m_intervals.size(); c++)
106  {
107  if(m_intervals.at(c).start() >= before)
108  m_intervals[c].translate(count);
109  }
110 
111  }
112 
113  void removeRows(int first, int count)
114  {
115  QVector< Interval<int> > temp_list;
116  Interval<int> i(first, first+count-1);
117  // first: remove the relevant rows from all intervals
118  for(int c=0; c<m_intervals.size(); c++)
119  {
120  temp_list = Interval<int>::subtract(m_intervals.at(c), i);
121  if(temp_list.isEmpty())
122  {
123  m_intervals.removeAt(c);
124  m_values.removeAt(c--);
125  }
126  else
127  {
128  m_intervals.replace(c, temp_list.at(0));
129  if(temp_list.size()>1)
130  {
131  m_intervals.insert(c, temp_list.at(1));
132  m_values.insert(c, m_values.at(c));
133  c++;
134  }
135  }
136  }
137  // second: translate all intervals that start at 'first+count' or later
138  for(int c=0; c<m_intervals.size(); c++)
139  {
140  if(m_intervals.at(c).start() >= first+count)
141  m_intervals[c].translate(-count);
142  }
143  // third: merge as many intervals as possible
144  QVector<T> values_copy = m_values;
145  QVector< Interval<int> > intervals_copy = m_intervals;
146  m_values.clear();
147  m_intervals.clear();
148  for(int c=0; c<intervals_copy.size(); c++)
149  {
150  i = intervals_copy.at(c);
151  T value = values_copy.at(c);
152  for(int cc=0; cc<m_intervals.size(); cc++)
153  {
154  if( m_intervals.at(cc).touches(i) &&
155  m_values.at(cc) == value )
156  {
157  m_intervals.replace(cc, Interval<int>::merge(m_intervals.at(cc),i));
158  return;
159  }
160  }
161  // if it could not be merged, just append it
162  m_intervals.append(i);
163  m_values.append(value);
164  }
165  }
166 
167  void clear() { m_values.clear(); m_intervals.clear(); }
168 
170  QVector<T> values() const { return m_values; }
171 
172 private:
175 };
176 
177 //! A class representing an interval-based attribute (bool version)
178 template<> class IntervalAttribute<bool>
179 {
180  public:
183 
184  void setValue(const Interval<int>& i, bool value=true)
185  {
186  if(value)
187  {
188  foreach(const Interval<int>& iv, m_intervals)
189  if(iv.contains(i))
190  return;
191 
193  } else { // unset
195  }
196  }
197 
198  void setValue(int row, bool value)
199  {
200  setValue(Interval<int>(row, row), value);
201  }
202 
203  bool isSet(int row) const
204  {
205  foreach(Interval<int> iv, m_intervals)
206  if(iv.contains(row))
207  return true;
208  return false;
209  }
210 
211  bool isSet(const Interval<int>& i) const
212  {
213  foreach(Interval<int> iv, m_intervals)
214  if(iv.contains(i))
215  return true;
216  return false;
217  }
218 
219  void insertRows(int before, int count)
220  {
221  QVector< Interval<int> > temp_list;
222  int c;
223  // first: split all intervals that contain 'before'
224  for(c=0; c<m_intervals.size(); c++)
225  {
226  if(m_intervals.at(c).contains(before))
227  {
228  temp_list = Interval<int>::split(m_intervals.at(c), before);
229  m_intervals.replace(c, temp_list.at(0));
230  if(temp_list.size()>1)
231  m_intervals.insert(c++, temp_list.at(1));
232 
233  }
234  }
235  // second: translate all intervals that start at 'before' or later
236  for(c=0; c<m_intervals.size(); c++)
237  {
238  if(m_intervals.at(c).start() >= before)
239  m_intervals[c].translate(count);
240  }
241 
242  }
243 
244  void removeRows(int first, int count)
245  {
246  int c;
247  // first: remove the relevant rows from all intervals
249  // second: translate all intervals that start at 'first+count' or later
250  for(c=0; c<m_intervals.size(); c++)
251  {
252  if(m_intervals.at(c).start() >= first+count)
253  m_intervals[c].translate(-count);
254  }
255  // third: merge as many intervals as possible
256  for(c=m_intervals.size()-1; c>=0; c--)
257  {
258  Interval<int> iv = m_intervals.takeAt(c);
259  int size_before = m_intervals.size();
261  if(size_before == m_intervals.size()) // merge successful
262  c--;
263  }
264  }
265 
267 
268  void clear() { m_intervals.clear(); }
269 
270  private:
272 };
273 
274 #endif
A class representing an interval-based attribute (bool version)
QVector< Interval< int > > m_intervals
void removeRows(int first, int count)
QVector< Interval< int > > intervals() const
void setValue(int row, bool value)
bool isSet(const Interval< int > &i) const
void setValue(const Interval< int > &i, bool value=true)
void insertRows(int before, int count)
bool isSet(int row) const
A class representing an interval-based attribute.
void removeRows(int first, int count)
T value(int row) const
QVector< T > values() const
void setValue(int row, T value)
QVector< Interval< int > > intervals() const
void setValue(const Interval< int > &i, T value)
QVector< Interval< int > > m_intervals
void insertRows(int before, int count)
static void mergeIntervalIntoList(QVector< Interval< T > > *list, Interval< T > i)
Merge an interval into a list.
Definition: Interval.h:127
bool contains(const Interval< T > &other) const
Definition: Interval.h:54
static QVector< Interval< T > > split(const Interval< T > &i, T before)
Split an interval into two.
Definition: Interval.h:105
static QVector< Interval< T > > subtract(const Interval< T > &src_iv, const Interval< T > &minus_iv)
Subtract an interval from another.
Definition: Interval.h:86
static void subtractIntervalFromList(QVector< Interval< T > > *list, Interval< T > i)
Subtract an interval from all intervals in the list.
Definition: Interval.h:160
Auxiliary class for interval based data.
Definition: Interval.h:212