• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdesdk API Reference
  • KDE Home
  • Contact Us
 

okteta

  • sources
  • kde-4.12
  • kdesdk
  • okteta
  • core
numberrange.h
Go to the documentation of this file.
1 /*
2  This file is part of the Okteta Core library, made within the KDE community.
3 
4  Copyright 2003,2007-2009 Friedrich W. H. Kossebau <kossebau@kde.org>
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Lesser General Public
8  License as published by the Free Software Foundation; either
9  version 2.1 of the License, or (at your option) version 3, or any
10  later version accepted by the membership of KDE e.V. (or its
11  successor approved by the membership of KDE e.V.), which shall
12  act as a proxy defined in Section 6 of version 3 of the license.
13 
14  This library is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  Lesser General Public License for more details.
18 
19  You should have received a copy of the GNU Lesser General Public
20  License along with this library. If not, see <http://www.gnu.org/licenses/>.
21 */
22 
23 #ifndef KDE_NUMBERRANGE_H
24 #define KDE_NUMBERRANGE_H
25 
26 // lib
27 #include "range.h"
28 
29 
30 namespace KDE
31 {
32 
36 template<typename N, typename S = N>
37 class NumberRange : public Range<N>
38 {
39  public:
44  static NumberRange fromWidth( N startIndex, S width );
45  static NumberRange fromWidth( S width );
46  public:
51  // FIXME: gcc 3.4.5 on windows gives an internal_compiler_error if ctor/dtor are defined with the keyword inline
52  // thus put the function bodies here
53  NumberRange( N startIndex, N endIndex ) : Range<N>(startIndex,endIndex) {}
54  NumberRange( const NumberRange& other ) : Range<N>(other.start(),other.end()) {}
55  NumberRange() {}
56 
57  ~NumberRange() {}
58 
59  public:
60  NumberRange& operator=( const NumberRange& other );
61 
62  public:
63  bool operator==( const NumberRange& other ) const;
64 
65  public:
66  void setByWidth( N other, S width );
70  void setStartNextBehind( const NumberRange& other );
71  void setStartNextBehind( N index );
75  void setEndNextBefore( const NumberRange& other );
76  void setEndNextBefore( N index );
80  void setStartByWidth( S width );
84  void setEndByWidth( S width );
86  void restrictEndByWidth( S width );
90  void moveToStart( N other );
94  void moveToEnd( N end );
95 
96  void adaptToReplacement( N offset, S removedLength, S insertedLength );
97 
98  NumberRange splitAt( N index );
99  NumberRange splitAtLocal( N index );
100  NumberRange remove( const NumberRange& removeRange );
101  NumberRange removeLocal( const NumberRange& removeRange );
102 
103  bool prepend( const NumberRange& other );
104  bool append( const NumberRange& other );
105 
106  public:
110  S width() const;
111  N nextBeforeStart() const;
112  N nextBehindEnd() const;
113 
114  public:
116  N localIndex( N index ) const;
117  NumberRange localRange( const NumberRange& other ) const;
119  NumberRange subRange( const NumberRange& localRange ) const;
123  N startForInclude( const NumberRange& other ) const;
125  bool isJoinable( const NumberRange& other ) const;
126 };
127 
128 
129 template<typename N,typename S>
130 inline NumberRange<N,S> NumberRange<N,S>::fromWidth( N startIndex, S width ) { return NumberRange(startIndex,startIndex+width-1); }
131 template<typename N,typename S>
132 inline NumberRange<N,S> NumberRange<N,S>::fromWidth( S width ) { return NumberRange<N,S>(0,width-1); }
133 
134 template<typename N,typename S>
135 inline bool NumberRange<N,S>::operator==( const NumberRange<N,S>& other ) const { return Range<N>::operator==(other); }
136 
137 template<typename N,typename S>
138 inline NumberRange<N,S>& NumberRange<N,S>::operator=( const NumberRange<N,S>& other ) { Range<N>::operator=(other); return *this; }
139 
140 template<typename N,typename S>
141 inline S NumberRange<N,S>::width() const { return Range<N>::isValid() ? Range<N>::end()-Range<N>::start()+1 : 0; }
142 template<typename N,typename S>
143 inline N NumberRange<N,S>::nextBeforeStart() const { return Range<N>::start()-1; }
144 template<typename N,typename S>
145 inline N NumberRange<N,S>::nextBehindEnd() const { return Range<N>::end()+1; }
146 
147 template<typename N,typename S>
148 inline void NumberRange<N,S>::setByWidth( N other, S width ) { Range<N>::setStart( other ); Range<N>::setEnd( other+width-1 ); }
149 template<typename N,typename S>
150 inline void NumberRange<N,S>::setStartByWidth( S width ) { Range<N>::setStart( Range<N>::end()-width+1 ); }
151 template<typename N,typename S>
152 inline void NumberRange<N,S>::setEndByWidth( S width ) { Range<N>::setEnd( Range<N>::start()+width-1 ); }
153 template<typename N,typename S>
154 inline void NumberRange<N,S>::setStartNextBehind( const NumberRange<N,S>& other ) { Range<N>::setStart( other.nextBehindEnd() ); }
155 template<typename N,typename S>
156 inline void NumberRange<N,S>::setStartNextBehind( N index ) { Range<N>::setStart( index+1 ); }
157 template<typename N,typename S>
158 inline void NumberRange<N,S>::setEndNextBefore( const NumberRange<N,S>& other ) { Range<N>::setEnd( other.nextBeforeStart() ); }
159 template<typename N,typename S>
160 inline void NumberRange<N,S>::setEndNextBefore( N index ) { Range<N>::setEnd( index-1 ); }
161 template<typename N,typename S>
162 inline void NumberRange<N,S>::restrictEndByWidth( S width ) { Range<N>::restrictEndTo( Range<N>::start()+width-1 ); }
163 
164 template<typename N,typename S>
165 inline void NumberRange<N,S>::moveToStart( N other ) { Range<N>::setEnd( other+width()-1 ); Range<N>::setStart( other ); }
166 template<typename N,typename S>
167 inline void NumberRange<N,S>::moveToEnd( N end ) { Range<N>::setStart( end-width()+1 ); Range<N>::setEnd( end ); }
168 
169 template<typename N,typename S>
170 inline NumberRange<N,S> NumberRange<N,S>::splitAt( N index )
171 {
172  const N secondEnd = Range<N>::end();
173  setEndNextBefore( index );
174  return NumberRange<N,S>( index, secondEnd );
175 }
176 template<typename N,typename S>
177 inline NumberRange<N,S> NumberRange<N,S>::splitAtLocal( N index )
178 {
179  const N secondEnd = Range<N>::end();
180  setEndByWidth( index );
181  return NumberRange<N,S>( nextBehindEnd(), secondEnd );
182 }
183 template<typename N,typename S>
184 inline NumberRange<N,S> NumberRange<N,S>::remove( const NumberRange<N,S>& removeRange )
185 {
186  const N secondEnd = Range<N>::end();
187  setEndNextBefore( removeRange );
188  return NumberRange<N,S>( removeRange.nextBehindEnd(), secondEnd );
189 }
190 template<typename N,typename S>
191 inline NumberRange<N,S> NumberRange<N,S>::removeLocal( const NumberRange<N,S>& removeRange )
192 {
193  const N secondEnd = Range<N>::end();
194  setEndByWidth( removeRange.start() );
195  return NumberRange<N,S>( Range<N>::start()+removeRange.nextBehindEnd(), secondEnd );
196 }
197 
198 template<typename N,typename S>
199 inline N NumberRange<N,S>::localIndex( N index ) const { return index - Range<N>::start(); }
200 template<typename N,typename S>
201 inline NumberRange<N,S> NumberRange<N,S>::localRange( const NumberRange<N,S>& other ) const
202 { return NumberRange<N,S>( other.start()-Range<N>::start(), other.end()-Range<N>::start() ); }
203 template<typename N,typename S>
204 inline NumberRange<N,S> NumberRange<N,S>::subRange( const NumberRange<N,S>& localRange ) const
205 { return NumberRange<N,S>( localRange.start()+Range<N>::start(), localRange.end()+Range<N>::start() ); }
206 
207 template<typename N,typename S>
208 inline N NumberRange<N,S>::startForInclude( const NumberRange<N,S>& other ) const
209 {
210  return Range<N>::startsBehind(other.start()) ? other.start() :
211  Range<N>::endsBefore(other.end()) ? other.end()-width()+1 :
212  Range<N>::start();
213 }
214 template<typename N,typename S>
215 inline bool NumberRange<N,S>::isJoinable( const NumberRange<N,S>& other ) const
216 { return Range<N>::start() <= other.nextBehindEnd() && other.nextBeforeStart() <= Range<N>::end(); }
217 
218 template<typename N,typename S>
219 inline bool NumberRange<N,S>::prepend( const NumberRange<N,S>& other )
220 { const bool mergeable = ( other.nextBehindEnd() == Range<N>::start() ); if( mergeable ) Range<N>::setStart( other.start() ); return mergeable; }
221 template<typename N,typename S>
222 inline bool NumberRange<N,S>::append( const NumberRange<N,S>& other )
223 { const bool mergeable = ( nextBehindEnd() == other.start() ); if( mergeable ) Range<N>::setEnd( other.end() ); return mergeable; }
224 
225 template<typename N,typename S>
226 inline void NumberRange<N,S>::adaptToReplacement( N offset, S removedLength, S insertedLength )
227 {
228  // nothing to adapt or not affected at all??
229  if( ! Range<N>::isValid() || Range<N>::endsBefore(offset-1) )
230  return;
231 
232  // indirectly affected?
233  if( !Range<N>::startsBefore(offset+removedLength) )
234  {
235  Range<N>::moveBy( insertedLength-removedLength );
236  }
237  // changes overlap, oh well
238  else
239  {
240  // only inserted?
241  if( removedLength==0 )
242  {
243  if( Range<N>::startsBefore(offset) && !Range<N>::endsBefore(offset-1) )
244  Range<N>::moveEndBy( insertedLength );
245  }
246  // only removed?
247  else if( insertedLength==0 )
248  {
249  Range<N>::extendStartTo(offset);
250  Range<N>::moveEndBy( -removedLength );
251  Range<N>::extendEndTo(offset-1);
252  // equals "if( End>offset+removedLength ) End -= removedLength; else End = offset-1;"
253  }
254  else
255  {
256  if( Range<N>::startsBehind(offset) )
257  Range<N>::setStart( offset+insertedLength );
258  if( Range<N>::endsBefore(offset+removedLength-1) )
259  Range<N>::setEnd( offset-1 );
260  else
261  Range<N>::moveEndBy( insertedLength-removedLength );
262  }
263  }
264 }
265 
266 }
267 
268 #endif
KDE::NumberRange::splitAt
NumberRange splitAt(N index)
Definition: numberrange.h:170
KDE::Range::extendEndTo
void extendEndTo(T Limit)
extends the end to Limit.
Definition: range.h:76
KDE::NumberRange::setEndNextBefore
void setEndNextBefore(const NumberRange &other)
sets the first index of the range's range one behind the other's end If one of both is invalid or the...
Definition: numberrange.h:158
KDE::Range::moveBy
void moveBy(T D)
moves the range by D.
Definition: range.h:82
KDE::NumberRange::NumberRange
NumberRange()
Definition: numberrange.h:55
KDE::NumberRange::NumberRange
NumberRange(const NumberRange &other)
Definition: numberrange.h:54
KDE::NumberRange::nextBehindEnd
N nextBehindEnd() const
Definition: numberrange.h:145
KDE::NumberRange
describes a range of numbers which have a distance of 1 each
Definition: numberrange.h:37
KDE::NumberRange::localRange
NumberRange localRange(const NumberRange &other) const
Definition: numberrange.h:201
KDE::NumberRange::moveToStart
void moveToStart(N other)
moves the range defined by a new start.
Definition: numberrange.h:165
KDE::Range< N >::start
N start() const
Definition: range.h:86
KDE::Range::setEnd
void setEnd(T E)
sets the last index of the range
Definition: range.h:60
KDE::NumberRange::isJoinable
bool isJoinable(const NumberRange &other) const
Definition: numberrange.h:215
KDE::Range
This template describes a range.
Definition: range.h:38
KDE::NumberRange::setByWidth
void setByWidth(N other, S width)
Definition: numberrange.h:148
KDE::NumberRange::removeLocal
NumberRange removeLocal(const NumberRange &removeRange)
Definition: numberrange.h:191
KDE::NumberRange::width
S width() const
Definition: numberrange.h:141
KDE::NumberRange::NumberRange
NumberRange(N startIndex, N endIndex)
constructs a range
Definition: numberrange.h:53
KDE::NumberRange::subRange
NumberRange subRange(const NumberRange &localRange) const
Definition: numberrange.h:204
range.h
KDE::Range::restrictEndTo
void restrictEndTo(T Limit)
restricts the end to Limit.
Definition: range.h:69
KDE::NumberRange::localIndex
N localIndex(N index) const
Definition: numberrange.h:199
KDE::Range< N >::end
N end() const
Definition: range.h:88
KDE::Range::operator=
Range & operator=(const Range &R)
Definition: range.h:46
KDE::NumberRange::setStartByWidth
void setStartByWidth(S width)
sets the first index of the range's range to be width-1 before the end If the range is invalid the be...
Definition: numberrange.h:150
KDE::NumberRange::adaptToReplacement
void adaptToReplacement(N offset, S removedLength, S insertedLength)
Definition: numberrange.h:226
KDE::NumberRange::fromWidth
static NumberRange fromWidth(N startIndex, S width)
constructs a range by width
Definition: numberrange.h:130
KDE::NumberRange::startForInclude
N startForInclude(const NumberRange &other) const
Definition: numberrange.h:208
KDE::NumberRange::setEndByWidth
void setEndByWidth(S width)
sets the last index of the range's range to be width-1 behind the start If the range is invalid the b...
Definition: numberrange.h:152
KDE::NumberRange::setStartNextBehind
void setStartNextBehind(const NumberRange &other)
sets the first index of the range's range one behind the other's end If one of both is invalid the be...
Definition: numberrange.h:154
KDE::Range::extendStartTo
void extendStartTo(T Limit)
extends the start to Limit.
Definition: range.h:74
KDE::Range::operator==
bool operator==(const Range &R) const
Definition: range.h:49
KDE::NumberRange::splitAtLocal
NumberRange splitAtLocal(N index)
Definition: numberrange.h:177
N
KDE::NumberRange::moveToEnd
void moveToEnd(N end)
moves the range defined by a new end.
Definition: numberrange.h:167
KDE::NumberRange::remove
NumberRange remove(const NumberRange &removeRange)
Definition: numberrange.h:184
KDE::NumberRange::operator==
bool operator==(const NumberRange &other) const
Definition: numberrange.h:135
KDE::NumberRange::prepend
bool prepend(const NumberRange &other)
Definition: numberrange.h:219
KDE::Range::endsBefore
bool endsBefore(T Value) const
returns true if range is before index.
Definition: range.h:103
KDE::NumberRange::operator=
NumberRange & operator=(const NumberRange &other)
Definition: numberrange.h:138
KDE::Range::startsBehind
bool startsBehind(T Value) const
returns true if range is behind index.
Definition: range.h:97
KDE::Range::setStart
void setStart(T S)
sets the first index of the range
Definition: range.h:58
KDE::NumberRange::restrictEndByWidth
void restrictEndByWidth(S width)
restricts the end by width.
Definition: numberrange.h:162
KDE::Range::isValid
bool isValid() const
returns true if the range covers at least one index
Definition: range.h:122
KDE::Range::moveEndBy
void moveEndBy(T D)
moves the end by D.
Definition: range.h:80
KDE::NumberRange::nextBeforeStart
N nextBeforeStart() const
Definition: numberrange.h:143
KDE::NumberRange::append
bool append(const NumberRange &other)
Definition: numberrange.h:222
KDE::NumberRange::~NumberRange
~NumberRange()
Definition: numberrange.h:57
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 23:04:08 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

okteta

Skip menu "okteta"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdesdk API Reference

Skip menu "kdesdk API Reference"
  • kapptemplate
  • kcachegrind
  • kompare
  • lokalize
  • okteta
  • umbrello
  •   umbrello

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal