source: cpp/gdk/multirange.h @ 105

Last change on this file since 105 was 66, checked in by Maciej Komosinski, 14 years ago

set 'eol-style' to 'native'

  • Property svn:eol-style set to native
File size: 2.8 KB
Line 
1// This file is a part of the Framsticks GDK library.
2// Copyright (C) 2002-2011  Szymon Ulatowski.  See LICENSE.txt for details.
3// Refer to http://www.framsticks.com/ for further information.
4
5#ifndef _MULTIRANGE_H_
6#define _MULTIRANGE_H_
7
8#include "list.h"
9
10/**
11   range in integer domain.
12   - (a,a) contains single integer: a
13   - (a,b) contains [b-a+1] integers
14 */
15class IRange
16{
17  public:
18int begin,end;
19IRange():begin(0),end(0) {}
20IRange(int b,int e):begin(b),end(e) {}
21IRange(const IRange& r):begin(r.begin),end(r.end) {}
22int size() const {return end-begin+1;}
23int isEmpty() const {return end<begin;}
24void makeEmpty() {end=begin;}
25void set(int b,int e) {begin=b; end=e;}
26void intersect(const IRange& r);
27void add(const IRange& r);
28/** all empty ranges are equal */
29int operator==(const IRange& r);
30void operator=(const IRange& r) {begin=r.begin; end=r.end;}
31int contains(int x) {return (x>=begin)&&(x<=end);}
32int contains(const IRange& r) {return !r.isEmpty()&&contains(r.begin)&&contains(r.end);}
33void print() const;
34};
35
36/** set of ranges = multiple selection. used in conversion mapping. see @ref convmap */
37class MultiRange
38{
39/** subsequent ranges in array, stored as: {begin1,end1,begin2,end2,...}
40    there are data.size()/2 ranges.
41    all ranges are sorted by 'begin' value.
42    ranges cannot intersect.
43 */
44SListTempl<int> data;
45int getData(int i) const {return (int)data(i);}
46void setData(int i,int x) {data.set(i,x);}
47int getBegin(int i) const {return getData(2*i);}
48int getEnd(int i) const {return getData(2*i+1);}
49void setBegin(int i,int x) {setData(2*i,x);}
50void setEnd(int i,int x) {setData(2*i+1,x);}
51/** find the last range with begin<=x
52 @return -1 if not found */
53int findRange(int x) const;
54void addRange(int i,int b,int e);
55void removeRange(int i);
56void removeRanges(int r1,int r2);
57
58  public:
59MultiRange() {}
60MultiRange(const MultiRange &mr) {data=mr.data;}
61void operator=(const MultiRange &mr) {data=mr.data;}
62MultiRange(const IRange &r) {add(r);}
63MultiRange(int begin,int end) {add(begin,end);}
64MultiRange(int x) {add(x);}
65
66int operator==(const MultiRange &mr) const {return data==mr.data;}
67
68void clear();
69int isEmpty() const;
70
71IRange singleRange() const;
72int rangeCount() const;
73IRange getRange(int i) const;
74
75void add(int x) {add(x,x);}
76void add(const IRange& r) {add(r.begin,r.end);}
77void add(int begin, int end);
78void add(const MultiRange &mr);
79
80void remove(int x) {remove(x,x);}
81void remove(const IRange& r) {remove(r.begin,r.end);}
82void remove(int begin, int end);
83void remove(const MultiRange &mr);
84
85int contains(int x) const;
86int contains(const IRange& r) const;
87int contains(const MultiRange &mr) const;
88
89void intersect(const IRange& r) {intersect(r.begin,r.end);}
90void intersect(int begin, int end);
91void intersect(const MultiRange &mr);
92
93void shift(int delta);
94
95void print() const;
96void print2() const;
97};
98
99#endif
100
101
102
103
104
Note: See TracBrowser for help on using the repository browser.