1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: sortedids.hxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #ifndef ARY_SORTEDIDS_HXX
32 #define ARY_SORTEDIDS_HXX
37 #include <cosv/tpl/range.hxx>
46 /** Implementation of a set of children to an entity in the Autodoc
47 repository. The children are sorted.
50 Needs to provide types:
56 static entity_base_type &
59 static const key_type &
61 const entity_type & i_entity );
64 const key_type & i_2 );
66 template<class COMPARE
>
70 typedef typename
COMPARE::id_type element_t
;
71 typedef typename
COMPARE::key_type key_t
;
72 typedef std::vector
<element_t
> data_t
;
73 typedef typename
data_t::const_iterator const_iterator
;
74 typedef typename
data_t::iterator iterator
;
75 typedef csv::range
<const_iterator
> search_result_t
;
79 std::size_t i_reserve
= 0 );
86 const_iterator
Begin() const;
87 const_iterator
End() const;
90 const key_t
& i_key
) const;
91 search_result_t
SearchAll(
92 const key_t
& i_key
) const;
93 const_iterator
LowerBound(
94 const key_t
& i_key
) const;
97 typedef typename
COMPARE::entity_base_type entity_t
;
101 const key_t
& i_key
);
106 template <class ITER
>
107 static ITER
impl_LowerBound_(
110 const key_t
& i_key
);
120 template<class COMPARE
>
121 inline const typename SortedIds
<COMPARE
>::key_t
&
122 SortedIds
<COMPARE
>::KeyOf_(element_t i_child
)
124 return COMPARE::KeyOf_(COMPARE::EntityOf_(i_child
));
127 template<class COMPARE
>
128 SortedIds
<COMPARE
>::SortedIds(std::size_t i_reserve
)
132 aData
.reserve(i_reserve
);
135 template<class COMPARE
>
136 SortedIds
<COMPARE
>::~SortedIds()
140 template<class COMPARE
>
142 SortedIds
<COMPARE
>::Add(element_t i_elem
)
144 aData
.insert( LowerBound( KeyOf_(i_elem
) ),
148 template<class COMPARE
>
149 inline typename SortedIds
<COMPARE
>::const_iterator
150 SortedIds
<COMPARE
>::Begin() const
152 return aData
.begin();
155 template<class COMPARE
>
156 inline typename SortedIds
<COMPARE
>::const_iterator
157 SortedIds
<COMPARE
>::End() const
162 template<class COMPARE
>
163 typename SortedIds
<COMPARE
>::element_t
164 SortedIds
<COMPARE
>::Search(const key_t
& i_key
) const
167 ret
= LowerBound(i_key
);
168 return ret
!= aData
.end() AND NOT
COMPARE::Lesser_(i_key
, KeyOf_(*ret
))
173 template<class COMPARE
>
174 typename SortedIds
<COMPARE
>::search_result_t
175 SortedIds
<COMPARE
>::SearchAll(const key_t
& i_key
) const
178 r1
= LowerBound(i_key
);
181 while ( r2
!= aData
.end()
182 AND NOT
COMPARE::Lesser_(i_key
, KeyOf_(*r2
)) )
187 return csv::make_range(r1
,r2
);
190 template<class COMPARE
>
191 inline typename SortedIds
<COMPARE
>::const_iterator
192 SortedIds
<COMPARE
>::LowerBound(const key_t
& i_key
) const
194 return impl_LowerBound_( aData
.begin(),
199 template<class COMPARE
>
200 inline typename SortedIds
<COMPARE
>::iterator
201 SortedIds
<COMPARE
>::LowerBound(const key_t
& i_key
)
203 return impl_LowerBound_( aData
.begin(),
208 template<class COMPARE
>
209 template <class ITER
>
211 SortedIds
<COMPARE
>::impl_LowerBound_( ITER i_begin
,
213 const key_t
& i_key
)
218 for ( ITER it
= i1
+ (i2
-i1
)/2;
220 it
= i1
+ (i2
-i1
)/2 )
222 if ( COMPARE::Lesser_(KeyOf_(*it
), i_key
) )