[286] | 1 | // This file is a part of Framsticks SDK. http://www.framsticks.com/ |
---|
| 2 | // Copyright (C) 1999-2015 Maciej Komosinski and Szymon Ulatowski. |
---|
| 3 | // See LICENSE.txt for details. |
---|
[109] | 4 | |
---|
| 5 | #ifndef _MULTIRANGE_H_ |
---|
| 6 | #define _MULTIRANGE_H_ |
---|
| 7 | |
---|
| 8 | #include <frams/util/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 | */ |
---|
| 15 | class IRange |
---|
| 16 | { |
---|
| 17 | public: |
---|
| 18 | int begin,end; |
---|
| 19 | IRange():begin(0),end(0) {} |
---|
| 20 | IRange(int b,int e):begin(b),end(e) {} |
---|
| 21 | IRange(const IRange& r):begin(r.begin),end(r.end) {} |
---|
| 22 | int size() const {return end-begin+1;} |
---|
| 23 | int isEmpty() const {return end<begin;} |
---|
| 24 | void makeEmpty() {end=begin;} |
---|
| 25 | void set(int b,int e) {begin=b; end=e;} |
---|
| 26 | void intersect(const IRange& r); |
---|
| 27 | void add(const IRange& r); |
---|
| 28 | /** all empty ranges are equal */ |
---|
| 29 | int operator==(const IRange& r); |
---|
| 30 | void operator=(const IRange& r) {begin=r.begin; end=r.end;} |
---|
| 31 | int contains(int x) {return (x>=begin)&&(x<=end);} |
---|
| 32 | int contains(const IRange& r) {return !r.isEmpty()&&contains(r.begin)&&contains(r.end);} |
---|
| 33 | void print() const; |
---|
| 34 | }; |
---|
| 35 | |
---|
| 36 | /** set of ranges = multiple selection. used in conversion mapping. see @ref convmap */ |
---|
| 37 | class 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 | */ |
---|
| 44 | SListTempl<int> data; |
---|
| 45 | int getData(int i) const {return (int)data(i);} |
---|
| 46 | void setData(int i,int x) {data.set(i,x);} |
---|
| 47 | int getBegin(int i) const {return getData(2*i);} |
---|
| 48 | int getEnd(int i) const {return getData(2*i+1);} |
---|
| 49 | void setBegin(int i,int x) {setData(2*i,x);} |
---|
| 50 | void setEnd(int i,int x) {setData(2*i+1,x);} |
---|
| 51 | /** find the last range with begin<=x |
---|
| 52 | @return -1 if not found */ |
---|
| 53 | int findRange(int x) const; |
---|
| 54 | void addRange(int i,int b,int e); |
---|
| 55 | void removeRange(int i); |
---|
| 56 | void removeRanges(int r1,int r2); |
---|
| 57 | |
---|
| 58 | public: |
---|
| 59 | MultiRange() {} |
---|
| 60 | MultiRange(const MultiRange &mr) {data=mr.data;} |
---|
| 61 | void operator=(const MultiRange &mr) {data=mr.data;} |
---|
| 62 | MultiRange(const IRange &r) {add(r);} |
---|
| 63 | MultiRange(int begin,int end) {add(begin,end);} |
---|
| 64 | MultiRange(int x) {add(x);} |
---|
| 65 | |
---|
| 66 | int operator==(const MultiRange &mr) const {return data==mr.data;} |
---|
| 67 | |
---|
| 68 | void clear(); |
---|
| 69 | int isEmpty() const; |
---|
| 70 | |
---|
| 71 | IRange singleRange() const; |
---|
| 72 | int rangeCount() const; |
---|
| 73 | IRange getRange(int i) const; |
---|
| 74 | |
---|
| 75 | void add(int x) {add(x,x);} |
---|
| 76 | void add(const IRange& r) {add(r.begin,r.end);} |
---|
| 77 | void add(int begin, int end); |
---|
| 78 | void add(const MultiRange &mr); |
---|
| 79 | |
---|
| 80 | void remove(int x) {remove(x,x);} |
---|
| 81 | void remove(const IRange& r) {remove(r.begin,r.end);} |
---|
| 82 | void remove(int begin, int end); |
---|
| 83 | void remove(const MultiRange &mr); |
---|
| 84 | |
---|
| 85 | int contains(int x) const; |
---|
| 86 | int contains(const IRange& r) const; |
---|
| 87 | int contains(const MultiRange &mr) const; |
---|
| 88 | |
---|
| 89 | void intersect(const IRange& r) {intersect(r.begin,r.end);} |
---|
| 90 | void intersect(int begin, int end); |
---|
| 91 | void intersect(const MultiRange &mr); |
---|
| 92 | |
---|
| 93 | void shift(int delta); |
---|
| 94 | |
---|
| 95 | void print() const; |
---|
| 96 | void print2() const; |
---|
| 97 | }; |
---|
| 98 | |
---|
| 99 | #endif |
---|
| 100 | |
---|
| 101 | |
---|
| 102 | |
---|
| 103 | |
---|
| 104 | |
---|