1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #ifndef INCLUDED_O3TL_RANGE_HXX
21 #define INCLUDED_O3TL_RANGE_HXX
24 #include <cstring> // for std::size_t
25 #include <boost/assert.hpp>
31 /** Represents a range of integer or iterator values.
34 Has to be assignable, add- and subtractable. That is:
37 - or a random access iterator.
43 typedef T element_type
; /// Provided for generic programming.
44 typedef range
<T
> self
;
48 T i_inclusiveLowerBorder
,
49 T i_exclusiveUpperBorder
);
54 std::size_t size() const;
59 const self
& i_other
) const;
61 const self
& i_other
) const;
62 /// @return i_other.begin() - this->end()
64 const self
& i_other
) const;
74 make_range(T i1
, T i2
)
76 return range
<T
>(i1
, i2
);
80 inline range
<typename
T::const_iterator
>
81 range_of(const T
& i_container
)
83 return make_range( i_container
.begin(),
89 inline range
<typename
T::iterator
>
90 range_of(T
& io_container
)
92 return make_range( io_container
.begin(),
104 range
<T
>::range( T i_inclusiveLowerBorder
,
105 T i_exclusiveUpperBorder
)
106 : nBegin(i_inclusiveLowerBorder
),
107 nEnd(i_exclusiveUpperBorder
)
109 BOOST_ASSERT( nBegin
<= nEnd
110 && "Invalid parameters for range<> constructor.");
120 range
<T
>::begin() const
127 range
<T
>::end() const
134 range
<T
>::size() const
136 BOOST_ASSERT( nBegin
<= nEnd
137 && "Invalid range limits in range<>::size().");
138 return static_cast<std::size_t>( end() - begin() );
143 range
<T
>::contains(T i_value
) const
145 return begin() <= i_value
151 range
<T
>::contains(const self
& i_other
) const
153 // This is subtle, because this would be wrong:
154 // begin() <= i_other.begin()
155 // && i_other.end() <= end();
156 // An empty range that begins and starts at my end()
157 // must not be contained.
159 return contains(i_other
.begin())
160 && i_other
.end() <= end();
165 range
<T
>::overlaps(const self
& i_other
) const
167 return contains(i_other
.begin())
168 || i_other
.contains(begin());
173 range
<T
>::distance_to(const self
& i_other
) const
175 return i_other
.begin() - end();
183 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */