Debian Codegraph @

BambooTracker/gui/pattern_editor/pattern_position.hpp

raw
1 /*
2 * Copyright (C) 2018-2020 Rerrah
3 *
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use,
8 * copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following
11 * conditions:
12 *
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26 #ifndef PATTERN_POSITION_HPP
27 #define PATTERN_POSITION_HPP
28
29 struct PatternPosition
30 {
31 int trackVisIdx, colInTrack, order, step;
32
33 friend bool operator==(const PatternPosition& a, const PatternPosition& b);
34 friend bool operator!=(const PatternPosition& a, const PatternPosition& b);
35
36 void setCols(int trackVisIdx, int colInTrack);
37 void setRows(int order, int step);
38
39 int compareCols(const PatternPosition& b) const;
40 int compareRows(const PatternPosition& b) const;
41
42 bool isEqualCols(const PatternPosition& b) const;
43 bool isEqualCols(const int trackVisIdx, const int colInTrack) const;
44 bool isEqualRows(const PatternPosition& b) const;
45 bool isEqualRows(const int order, const int step) const;
46
47 static bool inRowRange(const PatternPosition& pos,
48 const PatternPosition& begin, const PatternPosition& last)
49 {
50 return (begin.compareRows(pos) <= 0 && pos.compareRows(last) <= 0);
51 }
52
53 static bool inRowRange(const PatternPosition& srcBegin, const PatternPosition& srcLast,
54 const PatternPosition& tgtBegin, const PatternPosition& tgtLast)
55 {
56 if (PatternPosition::inRowRange(srcBegin, tgtBegin, tgtLast)) return true;
57 else if (PatternPosition::inRowRange(srcLast, tgtBegin, tgtLast)) return true;
58 else return (srcBegin.compareRows(tgtBegin) < 0 && tgtLast.compareRows(srcLast));
59 }
60 };
61
62 inline bool operator==(const PatternPosition& a, const PatternPosition& b)
63 {
64 return ((a.trackVisIdx == b.trackVisIdx) && (a.colInTrack == b.colInTrack)
65 && (a.order == b.order) && (a.step == b.step));
66 }
67
68 inline bool operator!=(const PatternPosition& a, const PatternPosition& b)
69 {
70 return !(a == b);
71 }
72
73 inline void PatternPosition::setCols(int track, int colInTrack)
74 {
75 this->trackVisIdx = track;
76 this->colInTrack = colInTrack;
77 }
78
79 inline void PatternPosition::setRows(int order, int step)
80 {
81 this->order = order;
82 this->step = step;
83 }
84
85 /// Return:
86 /// -2: this->track < b.track
87 /// -1: this->track == b.track && this->colInTrack < b.colInTrack
88 /// 0: this->track == b.track && this->colInTrack == b.colInTrack
89 /// 1: this->track == b.track && this->colInTrack > b.colInTrack
90 /// 2: this->track > b.track
91 inline int PatternPosition::compareCols(const PatternPosition& b) const
92 {
93 if (trackVisIdx < b.trackVisIdx) return -2;
94 else if (trackVisIdx > b.trackVisIdx) return 2;
95 else {
96 if (colInTrack < b.colInTrack) return -1;
97 else if (colInTrack > b.colInTrack) return 1;
98 else return 0;
99 }
100 }
101
102 /// Return:
103 /// -2: this->order < b.order
104 /// -1: this->order == b.order && this->step < b.step
105 /// 0: *this == b
106 /// 1: this->order == b.order && this->step > b.step
107 /// 2: this->order > b.order
108 inline int PatternPosition::compareRows(const PatternPosition& b) const
109 {
110 if (order < b.order) return -2;
111 else if (order > b.order) return 2;
112 else {
113 if (step < b.step) return -1;
114 else if (step > b.step) return 1;
115 else return 0;
116 }
117 }
118
119 inline bool PatternPosition::isEqualCols(const PatternPosition& b) const
120 {
121 return isEqualCols(b.trackVisIdx, b.colInTrack);
122 }
123
124 inline bool PatternPosition::isEqualCols(const int trackVisIdx, const int colInTrack) const
125 {
126 return (this->trackVisIdx == trackVisIdx && this->colInTrack == colInTrack);
127 }
128
129 inline bool PatternPosition::isEqualRows(const PatternPosition& b) const
130 {
131 return isEqualRows(b.order, b.step);
132 }
133
134 inline bool PatternPosition::isEqualRows(const int order, const int step) const
135 {
136 return (this->order == order && this->step == step);
137 }
138
139 #endif // PATTERN_POSITION_HPP