1*1c78a5d6SAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
3*1c78a5d6SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
4*1c78a5d6SAndrew Rist * or more contributor license agreements. See the NOTICE file
5*1c78a5d6SAndrew Rist * distributed with this work for additional information
6*1c78a5d6SAndrew Rist * regarding copyright ownership. The ASF licenses this file
7*1c78a5d6SAndrew Rist * to you under the Apache License, Version 2.0 (the
8*1c78a5d6SAndrew Rist * "License"); you may not use this file except in compliance
9*1c78a5d6SAndrew Rist * with the License. You may obtain a copy of the License at
10cdf0e10cSrcweir *
11*1c78a5d6SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir *
13*1c78a5d6SAndrew Rist * Unless required by applicable law or agreed to in writing,
14*1c78a5d6SAndrew Rist * software distributed under the License is distributed on an
15*1c78a5d6SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*1c78a5d6SAndrew Rist * KIND, either express or implied. See the License for the
17*1c78a5d6SAndrew Rist * specific language governing permissions and limitations
18*1c78a5d6SAndrew Rist * under the License.
19cdf0e10cSrcweir *
20*1c78a5d6SAndrew Rist *************************************************************/
21*1c78a5d6SAndrew Rist
22*1c78a5d6SAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweir #ifndef ARY_SORTEDIDS_HXX
25cdf0e10cSrcweir #define ARY_SORTEDIDS_HXX
26cdf0e10cSrcweir
27cdf0e10cSrcweir
28cdf0e10cSrcweir // USED SERVICES
29cdf0e10cSrcweir #include <algorithm>
30cdf0e10cSrcweir #include <cosv/tpl/range.hxx>
31cdf0e10cSrcweir
32cdf0e10cSrcweir
33cdf0e10cSrcweir
34cdf0e10cSrcweir
35cdf0e10cSrcweir namespace ary
36cdf0e10cSrcweir {
37cdf0e10cSrcweir
38cdf0e10cSrcweir
39cdf0e10cSrcweir /** Implementation of a set of children to an entity in the Autodoc
40cdf0e10cSrcweir repository. The children are sorted.
41cdf0e10cSrcweir
42cdf0e10cSrcweir @tpl COMPARE
43cdf0e10cSrcweir Needs to provide types:
44cdf0e10cSrcweir entity_base_type
45cdf0e10cSrcweir id_type
46cdf0e10cSrcweir key_type
47cdf0e10cSrcweir
48cdf0e10cSrcweir and functions:
49cdf0e10cSrcweir static entity_base_type &
50cdf0e10cSrcweir EntityOf_(
51cdf0e10cSrcweir id_type i_id );
52cdf0e10cSrcweir static const key_type &
53cdf0e10cSrcweir KeyOf_(
54cdf0e10cSrcweir const entity_type & i_entity );
55cdf0e10cSrcweir static bool Lesser_(
56cdf0e10cSrcweir const key_type & i_1,
57cdf0e10cSrcweir const key_type & i_2 );
58cdf0e10cSrcweir */
59cdf0e10cSrcweir template<class COMPARE>
60cdf0e10cSrcweir class SortedIds
61cdf0e10cSrcweir {
62cdf0e10cSrcweir public:
63cdf0e10cSrcweir typedef typename COMPARE::id_type element_t;
64cdf0e10cSrcweir typedef typename COMPARE::key_type key_t;
65cdf0e10cSrcweir typedef std::vector<element_t> data_t;
66cdf0e10cSrcweir typedef typename data_t::const_iterator const_iterator;
67cdf0e10cSrcweir typedef typename data_t::iterator iterator;
68cdf0e10cSrcweir typedef csv::range<const_iterator> search_result_t;
69cdf0e10cSrcweir
70cdf0e10cSrcweir // LIFECYCLE
71cdf0e10cSrcweir explicit SortedIds(
72cdf0e10cSrcweir std::size_t i_reserve = 0 );
73cdf0e10cSrcweir ~SortedIds();
74cdf0e10cSrcweir
75cdf0e10cSrcweir // OPERATIONS
76cdf0e10cSrcweir void Add(
77cdf0e10cSrcweir element_t i_elem );
78cdf0e10cSrcweir // INQUIRY
79cdf0e10cSrcweir const_iterator Begin() const;
80cdf0e10cSrcweir const_iterator End() const;
81cdf0e10cSrcweir
82cdf0e10cSrcweir element_t Search(
83cdf0e10cSrcweir const key_t & i_key ) const;
84cdf0e10cSrcweir search_result_t SearchAll(
85cdf0e10cSrcweir const key_t & i_key ) const;
86cdf0e10cSrcweir const_iterator LowerBound(
87cdf0e10cSrcweir const key_t & i_key ) const;
88cdf0e10cSrcweir
89cdf0e10cSrcweir private:
90cdf0e10cSrcweir typedef typename COMPARE::entity_base_type entity_t;
91cdf0e10cSrcweir
92cdf0e10cSrcweir // Locals
93cdf0e10cSrcweir iterator LowerBound(
94cdf0e10cSrcweir const key_t & i_key );
95cdf0e10cSrcweir
96cdf0e10cSrcweir static const key_t &
97cdf0e10cSrcweir KeyOf_(
98cdf0e10cSrcweir element_t i_child );
99cdf0e10cSrcweir template <class ITER>
100cdf0e10cSrcweir static ITER impl_LowerBound_(
101cdf0e10cSrcweir ITER i_begin,
102cdf0e10cSrcweir ITER i_end,
103cdf0e10cSrcweir const key_t & i_key );
104cdf0e10cSrcweir
105cdf0e10cSrcweir // DATA
106cdf0e10cSrcweir data_t aData;
107cdf0e10cSrcweir };
108cdf0e10cSrcweir
109cdf0e10cSrcweir
110cdf0e10cSrcweir
111cdf0e10cSrcweir
112cdf0e10cSrcweir // IMPLEMENTATION
113cdf0e10cSrcweir template<class COMPARE>
114cdf0e10cSrcweir inline const typename SortedIds<COMPARE>::key_t &
KeyOf_(element_t i_child)115cdf0e10cSrcweir SortedIds<COMPARE>::KeyOf_(element_t i_child)
116cdf0e10cSrcweir {
117cdf0e10cSrcweir return COMPARE::KeyOf_(COMPARE::EntityOf_(i_child));
118cdf0e10cSrcweir }
119cdf0e10cSrcweir
120cdf0e10cSrcweir template<class COMPARE>
SortedIds(std::size_t i_reserve)121cdf0e10cSrcweir SortedIds<COMPARE>::SortedIds(std::size_t i_reserve)
122cdf0e10cSrcweir : aData()
123cdf0e10cSrcweir {
124cdf0e10cSrcweir if (i_reserve > 0)
125cdf0e10cSrcweir aData.reserve(i_reserve);
126cdf0e10cSrcweir }
127cdf0e10cSrcweir
128cdf0e10cSrcweir template<class COMPARE>
~SortedIds()129cdf0e10cSrcweir SortedIds<COMPARE>::~SortedIds()
130cdf0e10cSrcweir {
131cdf0e10cSrcweir }
132cdf0e10cSrcweir
133cdf0e10cSrcweir template<class COMPARE>
134cdf0e10cSrcweir void
Add(element_t i_elem)135cdf0e10cSrcweir SortedIds<COMPARE>::Add(element_t i_elem)
136cdf0e10cSrcweir {
137cdf0e10cSrcweir aData.insert( LowerBound( KeyOf_(i_elem) ),
138cdf0e10cSrcweir i_elem );
139cdf0e10cSrcweir }
140cdf0e10cSrcweir
141cdf0e10cSrcweir template<class COMPARE>
142cdf0e10cSrcweir inline typename SortedIds<COMPARE>::const_iterator
Begin() const143cdf0e10cSrcweir SortedIds<COMPARE>::Begin() const
144cdf0e10cSrcweir {
145cdf0e10cSrcweir return aData.begin();
146cdf0e10cSrcweir }
147cdf0e10cSrcweir
148cdf0e10cSrcweir template<class COMPARE>
149cdf0e10cSrcweir inline typename SortedIds<COMPARE>::const_iterator
End() const150cdf0e10cSrcweir SortedIds<COMPARE>::End() const
151cdf0e10cSrcweir {
152cdf0e10cSrcweir return aData.end();
153cdf0e10cSrcweir }
154cdf0e10cSrcweir
155cdf0e10cSrcweir template<class COMPARE>
156cdf0e10cSrcweir typename SortedIds<COMPARE>::element_t
Search(const key_t & i_key) const157cdf0e10cSrcweir SortedIds<COMPARE>::Search(const key_t & i_key) const
158cdf0e10cSrcweir {
159cdf0e10cSrcweir const_iterator
160cdf0e10cSrcweir ret = LowerBound(i_key);
161cdf0e10cSrcweir return ret != aData.end() AND NOT COMPARE::Lesser_(i_key, KeyOf_(*ret))
162cdf0e10cSrcweir ? *ret
163cdf0e10cSrcweir : element_t(0);
164cdf0e10cSrcweir }
165cdf0e10cSrcweir
166cdf0e10cSrcweir template<class COMPARE>
167cdf0e10cSrcweir typename SortedIds<COMPARE>::search_result_t
SearchAll(const key_t & i_key) const168cdf0e10cSrcweir SortedIds<COMPARE>::SearchAll(const key_t & i_key) const
169cdf0e10cSrcweir {
170cdf0e10cSrcweir const_iterator
171cdf0e10cSrcweir r1 = LowerBound(i_key);
172cdf0e10cSrcweir const_iterator
173cdf0e10cSrcweir r2 = r1;
174cdf0e10cSrcweir while ( r2 != aData.end()
175cdf0e10cSrcweir AND NOT COMPARE::Lesser_(i_key, KeyOf_(*r2)) )
176cdf0e10cSrcweir {
177cdf0e10cSrcweir ++r2;
178cdf0e10cSrcweir }
179cdf0e10cSrcweir
180cdf0e10cSrcweir return csv::make_range(r1,r2);
181cdf0e10cSrcweir }
182cdf0e10cSrcweir
183cdf0e10cSrcweir template<class COMPARE>
184cdf0e10cSrcweir inline typename SortedIds<COMPARE>::const_iterator
LowerBound(const key_t & i_key) const185cdf0e10cSrcweir SortedIds<COMPARE>::LowerBound(const key_t & i_key) const
186cdf0e10cSrcweir {
187cdf0e10cSrcweir return impl_LowerBound_( aData.begin(),
188cdf0e10cSrcweir aData.end(),
189cdf0e10cSrcweir i_key );
190cdf0e10cSrcweir }
191cdf0e10cSrcweir
192cdf0e10cSrcweir template<class COMPARE>
193cdf0e10cSrcweir inline typename SortedIds<COMPARE>::iterator
LowerBound(const key_t & i_key)194cdf0e10cSrcweir SortedIds<COMPARE>::LowerBound(const key_t & i_key)
195cdf0e10cSrcweir {
196cdf0e10cSrcweir return impl_LowerBound_( aData.begin(),
197cdf0e10cSrcweir aData.end(),
198cdf0e10cSrcweir i_key );
199cdf0e10cSrcweir }
200cdf0e10cSrcweir
201cdf0e10cSrcweir template<class COMPARE>
202cdf0e10cSrcweir template <class ITER>
203cdf0e10cSrcweir ITER
impl_LowerBound_(ITER i_begin,ITER i_end,const key_t & i_key)204cdf0e10cSrcweir SortedIds<COMPARE>::impl_LowerBound_( ITER i_begin,
205cdf0e10cSrcweir ITER i_end,
206cdf0e10cSrcweir const key_t & i_key )
207cdf0e10cSrcweir {
208cdf0e10cSrcweir ITER i1 = i_begin;
209cdf0e10cSrcweir ITER i2 = i_end;
210cdf0e10cSrcweir
211cdf0e10cSrcweir for ( ITER it = i1 + (i2-i1)/2;
212cdf0e10cSrcweir i1 != i2;
213cdf0e10cSrcweir it = i1 + (i2-i1)/2 )
214cdf0e10cSrcweir {
215cdf0e10cSrcweir if ( COMPARE::Lesser_(KeyOf_(*it), i_key) )
216cdf0e10cSrcweir {
217cdf0e10cSrcweir i1 = it;
218cdf0e10cSrcweir ++i1;
219cdf0e10cSrcweir }
220cdf0e10cSrcweir else
221cdf0e10cSrcweir {
222cdf0e10cSrcweir i2 = it;
223cdf0e10cSrcweir }
224cdf0e10cSrcweir } // end for
225cdf0e10cSrcweir
226cdf0e10cSrcweir return i1;
227cdf0e10cSrcweir }
228cdf0e10cSrcweir
229cdf0e10cSrcweir
230cdf0e10cSrcweir
231cdf0e10cSrcweir
232cdf0e10cSrcweir } // namespace ary
233cdf0e10cSrcweir #endif
234