00001 #ifndef _STR_LinearExtent_H_ // -*-c++-*-
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifdef __GNUG__
00022 # pragma interface
00023 #endif
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045 #include <iosfwd>
00046 #include <climits>
00047
00048 #define _STR_LinearExtent_H_
00049
00050 namespace strmod {
00051 namespace strmod {
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063 class LinearExtent {
00064 public:
00065 typedef unsigned int off_t;
00066 typedef unsigned int length_t;
00067 static const off_t OFFSET_MAX = UINT_MAX;
00068 static const length_t LENGTH_MAX = UINT_MAX;
00069
00070
00071
00072
00073
00074
00075 static const LinearExtent full_extent;
00076
00077
00078 inline LinearExtent();
00079
00080 inline LinearExtent(off_t offset, length_t length);
00081
00082 inline ~LinearExtent();
00083
00084
00085 inline off_t Offset() const;
00086
00087 inline void Offset(off_t new_off);
00088
00089 inline length_t Length() const;
00090
00091 inline void Length(length_t new_length);
00092
00093
00094
00095
00096
00097
00098
00099
00100 inline off_t beginOffset() const;
00101
00102
00103 inline off_t endOffset() const;
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117 inline const LinearExtent intersection(const LinearExtent &other) const;
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127 void LengthenLeft(length_t by);
00128
00129 inline void LengthenRight(length_t by);
00130
00131
00132
00133 void ShortenLeft(length_t by);
00134
00135 inline void ShortenRight(length_t by);
00136
00137
00138 inline void MoveRight(off_t by);
00139
00140 inline void MoveLeft(off_t by);
00141
00142
00143
00144 const LinearExtent SubExtent(const LinearExtent &extent) const;
00145
00146 const LinearExtent &SubExtent_eq(const LinearExtent &extent);
00147
00148 private:
00149 off_t m_offset;
00150 length_t m_length;
00151 };
00152
00153
00154 ::std::ostream &operator <<(::std::ostream &os, const LinearExtent &ext);
00155
00156
00157
00158 inline LinearExtent::LinearExtent() : m_offset(0), m_length(0)
00159 {
00160 }
00161
00162 inline LinearExtent::LinearExtent(off_t offset, length_t length)
00163 : m_offset(offset), m_length(length)
00164 {
00165 }
00166
00167 inline LinearExtent::~LinearExtent()
00168 {
00169 }
00170
00171 inline LinearExtent::off_t LinearExtent::Offset() const
00172 {
00173 return(m_offset);
00174 }
00175
00176 inline void LinearExtent::Offset(LinearExtent::off_t new_off)
00177 {
00178 m_offset = new_off;
00179 }
00180
00181 inline LinearExtent::length_t LinearExtent::Length() const
00182 {
00183 return(m_length);
00184 }
00185
00186 inline void LinearExtent::Length(LinearExtent::length_t new_length)
00187 {
00188 m_length = new_length;
00189 }
00190
00191 inline LinearExtent::off_t LinearExtent::beginOffset() const
00192 {
00193 return(Offset());
00194 }
00195
00196 inline LinearExtent::off_t LinearExtent::endOffset() const
00197 {
00198 return(Offset() + Length());
00199 }
00200
00201 inline const LinearExtent
00202 LinearExtent::intersection(const LinearExtent &other) const
00203 {
00204 off_t intstart = (Offset() > other.Offset()) ? Offset() : other.Offset();
00205 off_t myend = endOffset();
00206 off_t otherend = other.endOffset();
00207 off_t intend = (myend < otherend) ? myend : otherend;
00208 return(LinearExtent(intstart,
00209 (intend <= intstart) ? 0 : (intend - intstart)));
00210 }
00211
00212 inline void LinearExtent::MoveRight(LinearExtent::off_t by)
00213 {
00214 if ((LENGTH_MAX - endOffset()) >= by) {
00215 m_offset += by;
00216 } else {
00217 m_offset = LENGTH_MAX - Length();
00218 }
00219 }
00220
00221 inline void LinearExtent::MoveLeft(LinearExtent::off_t by)
00222 {
00223 if (by < m_offset) {
00224 m_offset -= by;
00225 } else {
00226 m_offset = 0;
00227 }
00228 }
00229
00230 inline void LinearExtent::LengthenRight(length_t by)
00231 {
00232 if ((LENGTH_MAX - m_length) >= by) {
00233 m_length += by;
00234 } else {
00235 m_length = LENGTH_MAX;
00236 }
00237 }
00238
00239 inline void LinearExtent::ShortenRight(length_t by)
00240 {
00241 if (m_length >= by) {
00242 m_length -= by;
00243 } else {
00244 m_length = 0;
00245 }
00246 }
00247
00248 inline bool operator ==(const LinearExtent &a, const LinearExtent &b)
00249 {
00250 return (a.Offset() == b.Offset()) && (a.Length() == b.Length());
00251 }
00252
00253 inline bool operator !=(const LinearExtent &a, const LinearExtent &b)
00254 {
00255 return !(a == b);
00256 }
00257
00258 }
00259 }
00260
00261 #endif