276
by Razzloss
Copied dcpp/ from 0705-branch |
1 |
/*
|
2 |
* Copyright (C) 2001-2008 Jacek Sieka, arnetheduck on gmail point com
|
|
3 |
*
|
|
4 |
* This program is free software; you can redistribute it and/or modify
|
|
5 |
* it under the terms of the GNU General Public License as published by
|
|
6 |
* the Free Software Foundation; either version 2 of the License, or
|
|
7 |
* (at your option) any later version.
|
|
8 |
*
|
|
9 |
* This program is distributed in the hope that it will be useful,
|
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
12 |
* GNU General Public License for more details.
|
|
13 |
*
|
|
14 |
* You should have received a copy of the GNU General Public License
|
|
15 |
* along with this program; if not, write to the Free Software
|
|
16 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
17 |
*/
|
|
18 |
||
19 |
#ifndef DCPLUSPLUS_DCPP_SEGMENT_H_
|
|
20 |
#define DCPLUSPLUS_DCPP_SEGMENT_H_
|
|
21 |
||
22 |
class Segment { |
|
23 |
public: |
|
24 |
Segment() : start(0), size(-1) { } |
|
25 |
Segment(int64_t start_, int64_t size_) : start(start_), size(size_) { } |
|
26 |
||
27 |
int64_t getStart() const { return start; } |
|
28 |
int64_t getSize() const { return size; } |
|
29 |
int64_t getEnd() const { return getStart() + getSize(); } |
|
30 |
||
31 |
void setSize(int64_t size_) { size = size_; } |
|
32 |
||
33 |
bool overlaps(const Segment& rhs) const { |
|
34 |
int64_t end = getEnd(); |
|
35 |
int64_t rend = rhs.getEnd(); |
|
36 |
return getStart() < rend && rhs.getStart() < end; |
|
37 |
}
|
|
38 |
||
39 |
void trim(const Segment& rhs) { |
|
40 |
if(!overlaps(rhs)) { |
|
41 |
return; |
|
42 |
}
|
|
43 |
||
44 |
if(rhs.getStart() < start) { |
|
45 |
int64_t rend = rhs.getEnd(); |
|
46 |
if(rend > getEnd()) { |
|
47 |
start = size = 0; |
|
48 |
} else { |
|
49 |
size -= rend - start; |
|
50 |
start = rend; |
|
51 |
}
|
|
52 |
return; |
|
53 |
}
|
|
54 |
size = rhs.getStart() - start; |
|
55 |
}
|
|
56 |
||
57 |
bool operator==(const Segment& rhs) const { |
|
58 |
return getStart() == rhs.getStart() && getSize() == rhs.getSize(); |
|
59 |
}
|
|
60 |
bool operator<(const Segment& rhs) const { |
|
61 |
return (getStart() < rhs.getStart()) || (getStart() == rhs.getStart() && getSize() < rhs.getSize()); |
|
62 |
}
|
|
63 |
private: |
|
64 |
int64_t start; |
|
65 |
int64_t size; |
|
66 |
};
|
|
67 |
||
68 |
#endif /*SEGMENT_H_*/ |