Initial import of binutils 2.22 on the new vendor branch
[dragonfly.git] / contrib / gcc-4.1 / libstdc++-v3 / include / ext / pb_assoc / detail / value_type_adapter / value_type_adapter.hpp
1 // -*- C++ -*-
2
3 // Copyright (C) 2005, 2006 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library.  This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
9 // any later version.
10
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
15
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING.  If not, write to the Free
18 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
19 // USA.
20
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction.  Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License.  This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
29
30 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
31
32 // Permission to use, copy, modify, sell, and distribute this software
33 // is hereby granted without fee, provided that the above copyright
34 // notice appears in all copies, and that both that copyright notice and
35 // this permission notice appear in supporting documentation. None of
36 // the above authors, nor IBM Haifa Research Laboratories, make any
37 // representation about the suitability of this software for any
38 // purpose. It is provided "as is" without express or implied warranty.
39
40 /**
41  * @file value_type_adapter.hpp
42  * Contains an adapter of mapping levels.
43  */
44
45 #ifndef VALUE_TYPE_ADAPTER_HPP
46 #define VALUE_TYPE_ADAPTER_HPP
47
48 #include <ext/pb_assoc/detail/value_type_adapter/ref_pair.hpp>
49 #include <ext/pb_assoc/detail/assoc_cntnr_base.hpp>
50 #include <ext/pb_assoc/detail/value_type_adapter/invalidation_guarantee_selector.hpp>
51 #include <ext/pb_assoc/detail/type_utils.hpp>
52 #include <utility>
53 #include <algorithm>
54 #include <tr1/type_traits>  // for aligned_storage/alignment_of
55
56 namespace pb_assoc
57 {
58
59   namespace detail
60   {
61
62 #define PB_ASSOC_STATIC_ASSERT(UNIQUE, E) \
63         typedef \
64                 pb_assoc::detail::static_assert_dummy_class< \
65                         sizeof(pb_assoc::detail::static_assert<(bool)(E)>)> \
66                         UNIQUE##static_assert_type
67
68 #define PB_ASSOC_CLASS_T_DEC \
69         template< \
70                 typename Key, \
71                 typename Data, \
72                 class DS_Tag, \
73                 class Policy_Tl, \
74                 class Allocator, \
75                 int Mapping_Level>
76
77 #define PB_ASSOC_CLASS_C_DEC \
78         value_type_adapter< \
79                 Key, \
80                 Data, \
81                 DS_Tag, \
82                 Policy_Tl, \
83                 Allocator, \
84                 Mapping_Level>
85
86 #define PB_ASSOC_BASE_C_DEC \
87         cond_type< \
88                 Mapping_Level != 1, \
89                 value_type_adapter< \
90                         Key, \
91                         Data, \
92                         DS_Tag, \
93                         Policy_Tl, \
94                         Allocator, \
95                         Mapping_Level - 1>, \
96                 typename assoc_cntnr_base< \
97                         Key, \
98                         Data, \
99                         DS_Tag, \
100                         Policy_Tl, \
101                         Allocator>::type>::type
102
103     template<typename Key,
104              typename Data,
105              class DS_Tag,
106              class Policy_Tl,
107              class Allocator,
108              int Mapping_Level>
109     struct value_type_adapter : public PB_ASSOC_BASE_C_DEC
110     {
111
112     private:
113       typedef typename PB_ASSOC_BASE_C_DEC my_base;
114
115       typedef typename my_base::data_type my_base_data_type;
116
117       enum
118         {
119           same_alloc_type =
120           is_same_type<
121           typename my_base::allocator::template rebind<
122           char>::other,
123           typename my_base_data_type::allocator::template rebind<
124           char>::other>::value
125         };
126
127       PB_ASSOC_STATIC_ASSERT(wrong_level, Mapping_Level > 0);
128
129       PB_ASSOC_STATIC_ASSERT(must_be_same_alloc, same_alloc_type);
130
131 #include <ext/pb_assoc/detail/value_type_adapter/value_type_traits.hpp>
132 #include <ext/pb_assoc/detail/value_type_adapter/it_value_type_traits.hpp>
133
134       typedef
135       it_value_type_traits_<
136         typename base_it_key_type<
137         my_base,
138         Mapping_Level == 1>::type,
139         typename my_base_data_type::const_key_reference,
140         typename cond_type<
141         is_same_type<
142         typename my_base_data_type::data_type,
143         null_data_type>::value,
144         null_data_type,
145         typename my_base_data_type::data_reference>::type,
146         typename my_base_data_type::reference,
147         typename my_base::allocator>
148       it_value_type_traits_t;
149
150 #include <ext/pb_assoc/detail/value_type_adapter/iterator.hpp>
151
152       typedef
153       value_type_traits_<
154         typename my_base::key_type,
155         typename my_base_data_type::key_type,
156         typename my_base_data_type::data_type,
157         typename my_base::allocator>
158       value_type_traits_t;
159
160       enum
161         {
162           has_data =
163           !is_same_type<
164           typename my_base_data_type::data_type,
165           null_data_type>::value
166         };
167
168     public:
169
170       typedef typename Allocator::size_type size_type;
171
172       typedef typename Allocator::difference_type difference_type;
173
174       typedef typename my_base::allocator allocator;
175
176       typedef typename it_value_type_traits_t::key_type it_key_type;
177
178       typedef
179       std::pair<
180         typename my_base::key_type,
181         typename my_base_data_type::key_type>
182       key_type;
183
184       typedef
185       typename allocator::template rebind<
186         key_type>::other::reference
187       key_reference;
188
189       typedef
190       typename allocator::template rebind<
191         key_type>::other::const_reference
192       const_key_reference;
193
194       typedef
195       typename allocator::template rebind<
196         key_type>::other::pointer
197       key_pointer;
198
199       typedef
200       typename allocator::template rebind<
201         key_type>::other::const_pointer
202       const_key_pointer;
203
204       typedef typename my_base_data_type::data_type data_type;
205
206       typedef
207       typename allocator::template rebind<
208         data_type>::other::reference
209       data_reference;
210
211       typedef
212       typename allocator::template rebind<
213         data_type>::other::const_reference
214       const_data_reference;
215
216       typedef
217       typename allocator::template rebind<
218         data_type>::other::pointer
219       data_pointer;
220
221       typedef
222       typename allocator::template rebind<
223         data_type>::other::const_pointer
224       const_data_pointer;
225
226       typedef typename value_type_traits_t::value_type value_type;
227
228       typedef typename value_type_traits_t::reference reference;
229
230       typedef typename value_type_traits_t::const_reference const_reference;
231
232       typedef typename value_type_traits_t::pointer pointer;
233
234       typedef typename value_type_traits_t::const_pointer const_pointer;
235
236       typedef
237       it_<
238         typename my_base::const_find_iterator,
239         typename my_base_data_type::const_find_iterator,
240         has_data,
241         true>
242       const_find_iterator;
243
244       typedef
245       it_<
246         typename my_base::find_iterator,
247         typename my_base_data_type::find_iterator,
248         has_data,
249         false>
250       find_iterator;
251
252       typedef
253       it_<
254         typename my_base::const_iterator,
255         typename my_base_data_type::const_iterator,
256         has_data,
257         true>
258       const_iterator;
259
260       typedef
261       it_<
262         typename my_base::iterator,
263         typename my_base_data_type::iterator,
264         has_data,
265         false>
266       iterator;
267
268       enum
269         {
270           mapping_level = mapping_level_imp<
271           typename my_base::given_data_type>::value -1
272         };
273
274       // Tmp Ami rebind
275
276       typedef compound_ds_tag ds_category;
277
278       typedef
279       typename cond_type<
280         mapping_level == 1,
281         typename cond_type<
282         has_data,
283         data_enabled_ms_tag,
284         basic_ms_tag>::type,
285         compound_data_enabled_ms_tag>::type
286       ms_category;
287
288       typedef
289       typename cond_type<
290         Mapping_Level == 1,
291         DS_Tag,
292         compound_ds_tag>::type
293       effective_base_ds_tag;
294
295       typedef ds_traits< my_base_data_type> base_data_ds_traits;
296
297       enum
298         {
299           erase_can_throw =
300           base_data_ds_traits::erase_can_throw
301         };
302
303       enum
304         {
305           order_preserving =
306           order_preserving_imp<
307           my_base,
308           effective_base_ds_tag>::value&& 
309           base_data_ds_traits::order_preserving
310         };
311
312       enum
313         {
314           erase_iterators =
315           base_data_ds_traits::erase_iterators
316         };
317
318       typedef
319       typename ig_sel<
320         typename invalidation_guarantee_imp<
321         my_base,
322         effective_base_ds_tag>::type,
323         typename ds_traits<
324         my_base_data_type>::invalidation_guarantee>::type
325       invalidation_guarantee;
326
327       enum
328         {
329           reverse_iteration =
330           reverse_iteration_imp<
331           my_base,
332           effective_base_ds_tag>::value&& 
333           base_data_ds_traits::reverse_iteration
334         };
335
336       enum
337         {
338           split_join = false
339         };
340
341     protected:
342       typedef typename my_base_data_type::data_pointer erase_imp_ret_t;
343
344     private:
345       inline const_key_reference
346       extract_key_imp(const_reference r_val, int_to_type<true>)
347       {
348         return (r_val.first);
349       }
350
351       inline const_key_reference
352       extract_key_imp(const_reference r_val, int_to_type<false>)
353       {
354         return (r_val);
355       }
356
357       inline it_key_type
358       extract_key_imp(typename iterator::const_reference r_val, int_to_type<true>)
359       {
360         return (r_val.first);
361       }
362
363       inline it_key_type
364       extract_key_imp(typename iterator::const_reference r_val, int_to_type<false>)
365       {
366         return (r_val);
367       }
368
369     public:
370
371       inline size_type
372       size() const
373       {
374         return (std::distance(begin(), end()));
375       }
376
377       inline size_type
378       max_size() const
379       {
380         return (my_base::max_size());
381       }
382
383       inline bool
384       empty() const
385       {
386         return (size() == 0);
387       }
388
389       inline static const_key_reference
390       extract_key(const_reference r_val)
391       {
392         return (extract_key_imp(
393                                 r_val,
394                                 int_to_type<has_data>()));
395       }
396
397       inline it_key_type
398       extract_key(typename iterator::const_reference r_val)
399       {
400         return (extract_key_imp(
401                                 r_val,
402                                 int_to_type<has_data>()));
403       }
404
405       inline std::pair<
406         find_iterator,
407         bool>
408       insert(const_reference r_val)
409       {
410         typedef std::pair< typename my_base::find_iterator, bool> base_ins_ret;
411
412         // Tmp Ami
413       }
414
415       inline data_reference
416       operator[](const_key_reference r_key)
417       {
418         return (subscript_imp(r_key));
419       }
420
421       inline const_find_iterator
422       find(const_key_reference r_key) const
423       {
424         typename my_base::const_find_iterator it = my_base::find(r_key.first);
425
426         if (it == my_base::end())
427           return (end());
428
429         typename my_base_data_type::const_find_iterator sec_it =
430           it->second.find(r_key.second);
431
432         if (sec_it == it->second.end())
433           return (end());
434
435         return (const_find_iterator(it, sec_it));
436       }
437
438       inline find_iterator
439       find(const_key_reference r_key)
440       {
441         typename my_base::find_iterator it = my_base::find(r_key.first);
442
443         if (it == my_base::end())
444           return (end());
445
446         typename my_base_data_type::find_iterator sec_it =
447           it->second.find(r_key.second);
448
449         if (sec_it == it->second.end())
450           return (end());
451
452         return (find_iterator(it, my_base::end(), sec_it));
453       }
454
455       inline const_data_reference
456       operator[](const_key_reference r_key) const
457       {
458         return (my_base::operator[](r_key.first).operator[](r_key.second));
459       }
460
461       inline size_type
462       erase(const_key_reference r_key)
463       {
464         typename my_base::find_iterator it =
465           my_base::find(r_key.first);
466
467         if (it == my_base::end())
468           return (0);
469
470         if (it->second.find(r_key.second) == it->second.end())
471           return (0);
472
473         it->second.erase(r_key.second);
474
475         return (1);
476       }
477
478 #include <ext/pb_assoc/detail/value_type_adapter/erase_if_pred.hpp>
479
480       template<class Pred>
481       inline size_type
482       erase_if(Pred prd)
483       {
484         typename my_base::iterator it = my_base::begin();
485
486         typename my_base::iterator end_it = my_base::end();
487
488         size_type ersd = 0;
489
490         // Tmp Ami check erase can throw
491
492         while (it != end_it)
493           {
494             if (it->second.empty() == false)
495               {
496                 erase_if_pred<Pred> p(prd, it);
497
498                 ersd += it->second.erase_if(p);
499               }
500
501             ++it;
502           }
503
504         return (ersd);
505       }
506
507       void
508       clear()
509       {
510         typename my_base::iterator it = my_base::begin();
511
512         typename my_base::iterator end_it = my_base::end();
513
514         while (it != end_it)
515           it->second.clear();
516       }
517
518       inline const_iterator
519       begin() const
520       {
521         typename my_base::const_iterator it = my_base::begin();
522
523         while (it != my_base::end()&&  it->second.size() == 0)
524           ++it;
525
526         if (it == my_base::end())
527           return (end());
528
529         return (const_iterator(it, my_base::end(), it->second.begin()));
530       }
531
532       inline iterator
533       begin()
534       {
535         typename my_base::iterator it = my_base::begin();
536
537         while (it != my_base::end()&&  it->second.size() == 0)
538           ++it;
539
540         if (it == my_base::end())
541           return (end());
542
543         return (iterator(it, my_base::end(), it->second.begin()));
544       }
545
546       inline const_iterator
547       end() const
548       {
549         return (const_iterator(my_base::end(), my_base::end()));
550       }
551
552       inline iterator
553       end()
554       {
555         return (iterator(my_base::end(), my_base::end()));
556       }
557
558     protected:
559
560       virtual
561       ~value_type_adapter()
562       { }
563
564 #define PB_ASSOC_CLASS_NAME value_type_adapter
565
566 #define PB_ASSOC_DIRECT_BASE_C_DEC PB_ASSOC_BASE_C_DEC
567
568 #define PB_ASSOC_DIRECT_BASE_CAST_C_DEC \
569         typename PB_ASSOC_DIRECT_BASE_C_DEC
570
571 #include <ext/pb_assoc/detail/constructors_destructor_fn_imps.hpp>
572
573 #undef PB_ASSOC_CLASS_NAME
574
575 #undef PB_ASSOC_DIRECT_BASE_C_DEC
576
577 #undef PB_ASSOC_DIRECT_BASE_CAST_C_DEC
578
579       data_reference
580       subscript_imp(const_key_reference r_key)
581       {
582         return (my_base::subscript_imp(r_key.first)[r_key.second]);
583       }
584
585     private:
586       value_type_adapter& 
587       operator=(const value_type_adapter& r_other);
588     };
589
590 #undef PB_ASSOC_CLASS_T_DEC
591
592 #undef PB_ASSOC_CLASS_C_DEC
593
594 #undef PB_ASSOC_BASE_C_DEC
595
596   } // namespace detail
597
598 } // namespace pb_assoc
599
600 #endif // #ifndef VALUE_TYPE_ADAPTER_HPP
601