00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #ifndef COH_ADAPTER_MAP_HPP
00017 #define COH_ADAPTER_MAP_HPP
00018
00019 #include "coherence/lang.ns"
00020
00021 #include "coherence/util/Collection.hpp"
00022 #include "coherence/util/Collections.hpp"
00023 #include "coherence/util/HashMap.hpp"
00024 #include "coherence/util/InvocableMap.hpp"
00025 #include "coherence/util/Iterator.hpp"
00026 #include "coherence/util/Map.hpp"
00027 #include "coherence/util/filter/NotFilter.hpp"
00028 #include "coherence/util/filter/PresentFilter.hpp"
00029 #include "coherence/util/processor/ConditionalPut.hpp"
00030 #include "coherence/util/processor/ConditionalPutAll.hpp"
00031
00032 #include <functional>
00033 #include <iterator>
00034 #include <memory>
00035 #include <ostream>
00036
00037 #include <utility>
00038
00039 COH_OPEN_NAMESPACE2(coherence,stl)
00040
00041 using coherence::util::Collection;
00042 using coherence::util::Collections;
00043 using coherence::util::HashMap;
00044 using coherence::util::InvocableMap;
00045 using coherence::util::Iterator;
00046 using coherence::util::Map;
00047 using coherence::util::filter::NotFilter;
00048 using coherence::util::filter::PresentFilter;
00049 using coherence::util::processor::ConditionalPut;
00050 using coherence::util::processor::ConditionalPutAll;
00051
00052
00053
00054 COH_OPEN_NAMESPACE(impl)
00055
00056 template<class K, class V> class _iterator;
00057
00058 template<class K, class V> class _const_iterator;
00059
00060
00061
00062
00063 template<class K, class V>
00064 class _adapter_map_base
00065 {
00066
00067
00068 public:
00069
00070
00071
00072 typedef K key_type;
00073
00074
00075
00076
00077 typedef V data_type;
00078
00079
00080
00081
00082 typedef V mapped_type;
00083
00084
00085
00086
00087 typedef std::pair<const key_type, mapped_type> value_type;
00088
00089
00090
00091
00092 typedef std::allocator<value_type> allocator_type;
00093
00094
00095
00096
00097 typedef typename allocator_type::size_type size_type;
00098
00099
00100
00101
00102 typedef typename allocator_type::difference_type difference_type;
00103
00104
00105
00106
00107 typedef _iterator<K,V> iterator;
00108
00109
00110
00111
00112 typedef _const_iterator<K,V> const_iterator;
00113
00114
00115
00116
00117 protected:
00118
00119
00120
00121 iterator _make_iterator(Map::Handle hMap, Iterator::Handle hIter);
00122
00123
00124
00125
00126 iterator _make_lazy_iterator(Map::Handle hMap, Object::View vKey);
00127
00128
00129
00130
00131 const_iterator _make_const_iterator(Map::View vMap,
00132 Iterator::Handle hIter) const;
00133
00134
00135
00136
00137 const_iterator _make_lazy_const_iterator(Map::View vMap,
00138 Object::View vKey) const;
00139 };
00140
00141
00142
00143
00144
00145
00146 class EndIterator
00147 : public class_spec<EndIterator,
00148 extends<Object>,
00149 implements<Iterator> >
00150 {
00151 friend class factory<EndIterator>;
00152
00153 public:
00154 static Handle getInstance()
00155 {
00156 static FinalHandle<EndIterator> hIter
00157 (System::common(), create());
00158 return hIter;
00159 }
00160
00161 protected:
00162 EndIterator()
00163 {
00164 }
00165
00166 public:
00167 virtual bool hasNext() const
00168 {
00169 return false;
00170 }
00171 virtual Object::Holder next()
00172 {
00173 COH_THROW (NoSuchElementException::create());
00174 }
00175 };
00176 COH_STATIC_INIT(EndIterator::getInstance());
00177
00178 COH_CLOSE_NAMESPACE
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212 template<class K, class V>
00213 class adapter_map
00214 : private impl::_adapter_map_base<K,V>
00215 {
00216
00217
00218 private:
00219
00220
00221
00222 typedef impl::_adapter_map_base<K,V> super;
00223
00224 public:
00225
00226
00227
00228 typedef adapter_map<K, V> this_type;
00229
00230
00231
00232
00233 typedef typename super::key_type key_type;
00234
00235
00236
00237
00238 typedef typename super::data_type data_type;
00239
00240
00241
00242
00243 typedef typename super::mapped_type mapped_type;
00244
00245
00246
00247
00248
00249 typedef typename super::value_type value_type;
00250
00251
00252
00253
00254
00255 typedef typename super::allocator_type allocator_type;
00256
00257
00258
00259
00260
00261 typedef typename super::difference_type difference_type;
00262
00263
00264
00265
00266
00267 typedef typename super::size_type size_type;
00268
00269
00270
00271
00272
00273
00274 typedef typename super::iterator iterator;
00275
00276
00277
00278
00279
00280 typedef typename super::const_iterator const_iterator;
00281
00282
00283
00284
00285 typedef typename iterator::pointer pointer;
00286
00287
00288
00289
00290 typedef typename const_iterator::pointer const_pointer;
00291
00292
00293
00294
00295 typedef typename iterator::reference reference;
00296
00297
00298
00299
00300 typedef typename const_iterator::reference const_reference;
00301
00302
00303
00304
00305
00306 typedef typename iterator::mapped_reference mapped_reference;
00307
00308
00309
00310
00311 public:
00312 class hasher;
00313 class key_equal;
00314
00315
00316
00317
00318 public:
00319
00320
00321
00322
00323
00324 adapter_map(Map::Holder ohMap = NULL)
00325 : f_ohMap(System::common(), ohMap)
00326 {
00327 }
00328
00329
00330
00331
00332
00333 adapter_map(const this_type& that)
00334 : f_ohMap(System::common(), that.f_ohMap)
00335 {
00336 }
00337
00338
00339
00340
00341 virtual ~adapter_map()
00342 {
00343 }
00344
00345
00346
00347
00348
00349 this_type& operator=(const this_type& that)
00350 {
00351 initialize(f_ohMap, (Map::Holder) that.f_ohMap);
00352 return *this;
00353 }
00354
00355
00356
00357
00358 this_type& operator=(Map::Holder ohMap)
00359 {
00360 initialize(f_ohMap, ohMap);
00361 return *this;
00362 }
00363
00364
00365
00366
00367 public:
00368
00369
00370
00371
00372
00373
00374
00375
00376 Map::Handle delegate()
00377 {
00378 Map::Holder ohMap = f_ohMap;
00379 Map::Handle hMap = cast<Map::Handle>(ohMap, false);
00380 if (NULL == hMap && NULL != ohMap)
00381 {
00382 COH_THROW (UnsupportedOperationException::create());
00383 }
00384 return hMap;
00385 }
00386
00387
00388
00389
00390
00391
00392 Map::View delegate() const
00393 {
00394 return f_ohMap;
00395 }
00396
00397
00398
00399
00400 public:
00401
00402
00403
00404 size_type size() const
00405 {
00406 return delegate()->size();
00407 }
00408
00409
00410
00411
00412 size_type max_size() const
00413 {
00414 return static_cast<size_type>(-1);
00415 }
00416
00417
00418
00419
00420 bool empty() const
00421 {
00422 return (size() == 0);
00423 }
00424
00425
00426
00427
00428 public:
00429
00430
00431
00432
00433
00434 const_iterator cbegin() const
00435 {
00436 Map::View vMap = delegate();
00437 return super::_make_const_iterator
00438 (vMap, vMap->keySet()->iterator());
00439 }
00440
00441
00442
00443
00444
00445
00446 const_iterator begin() const
00447 {
00448 return cbegin();
00449 }
00450
00451
00452
00453
00454
00455
00456 iterator begin()
00457 {
00458 Map::Handle hMap = delegate();
00459 return super::_make_iterator(hMap, hMap->keySet()->iterator());
00460 }
00461
00462
00463
00464
00465
00466
00467 const_iterator cend() const
00468 {
00469 return super::_make_const_iterator(delegate(),
00470 impl::EndIterator::getInstance());
00471 }
00472
00473
00474
00475
00476
00477
00478 const_iterator end() const
00479 {
00480 return cend();
00481 }
00482
00483
00484
00485
00486
00487
00488 iterator end()
00489 {
00490 return super::_make_iterator(delegate(),
00491 impl::EndIterator::getInstance());
00492 }
00493
00494
00495
00496
00497 public:
00498
00499
00500
00501
00502
00503
00504
00505
00506 mapped_reference operator[](const key_type& key);
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516 mapped_type operator[](const key_type& key) const;
00517
00518
00519
00520
00521 public:
00522
00523
00524
00525
00526
00527
00528
00529
00530
00531
00532
00533
00534
00535
00536
00537 std::pair<iterator, bool> insert(const value_type& x);
00538
00539
00540
00541
00542
00543
00544
00545
00546
00547
00548
00549
00550
00551
00552
00553 iterator insert(iterator , const value_type& x)
00554 {
00555 return insert(x).first;
00556 }
00557
00558
00559
00560
00561
00562
00563
00564
00565
00566
00567
00568
00569
00570 template<class InputIterator>
00571 void insert(InputIterator first, InputIterator last);
00572
00573
00574
00575
00576
00577
00578
00579
00580
00581 void erase(iterator position);
00582
00583
00584
00585
00586
00587
00588
00589 void erase(iterator first, iterator last);
00590
00591
00592
00593
00594
00595
00596
00597
00598
00599
00600
00601 size_type erase(const key_type& k)
00602 {
00603 return delegate()->keySet()->remove(k) ? 1 : 0;
00604 }
00605
00606
00607
00608
00609 void clear()
00610 {
00611 delegate()->clear();
00612 }
00613
00614
00615
00616
00617 void swap(this_type& x)
00618 {
00619 std::swap(f_ohMap, x.f_ohMap);
00620 }
00621
00622
00623
00624
00625 public:
00626
00627
00628
00629
00630
00631 iterator find(const key_type& k)
00632 {
00633 Map::Handle hMap = delegate();
00634 if (!hMap->containsKey(k))
00635 {
00636 return end();
00637 }
00638
00639 return this->_make_lazy_iterator(hMap, k);
00640 }
00641
00642
00643
00644
00645
00646 const_iterator find(const key_type& k) const
00647 {
00648 Map::View vMap = delegate();
00649 if (!vMap->containsKey(k))
00650 {
00651 return end();
00652 }
00653
00654 return this->_make_lazy_const_iterator(vMap, k);
00655 }
00656
00657
00658
00659
00660 size_type count(const key_type& k) const
00661 {
00662 return delegate()->containsKey(k) ? 1 : 0;
00663 }
00664
00665
00666
00667
00668
00669
00670
00671
00672
00673
00674
00675
00676
00677
00678
00679 std::pair<iterator,iterator> equal_range(const key_type& k);
00680
00681
00682
00683
00684
00685
00686
00687
00688
00689
00690
00691
00692 std::pair<const_iterator,const_iterator> equal_range(const key_type& k)
00693 const;
00694
00695
00696
00697
00698 public:
00699
00700
00701
00702 allocator_type get_allocator() const
00703 {
00704 return allocator_type();
00705 }
00706
00707
00708
00709
00710 public:
00711
00712
00713
00714 hasher hash_function() const
00715 {
00716 return hasher();
00717 }
00718
00719
00720
00721
00722 key_equal key_eq() const
00723 {
00724 return key_equal();
00725 }
00726
00727
00728
00729
00730 protected:
00731
00732
00733
00734 FinalHolder<Map> f_ohMap;
00735 };
00736
00737
00738
00739
00740
00741
00742
00743
00744
00745
00746
00747
00748
00749 template <class K, class V>
00750 inline void swap(adapter_map<K,V>& x, adapter_map<K,V>& y)
00751 {
00752 x.swap(y);
00753 }
00754
00755
00756
00757
00758
00759
00760
00761 template <class MapSrc, class MapDes> void copy_map(const MapSrc& mapSrc, MapDes& mapDes)
00762 {
00763 for (typename MapSrc::const_iterator i = mapSrc.begin(), e = mapSrc.end(); i != e; ++i)
00764 {
00765 mapDes[i->first] = i->second;
00766 }
00767 }
00768
00769
00770
00771 COH_OPEN_NAMESPACE(impl)
00772
00773
00774
00775
00776
00777
00778
00779 template<class T>
00780 class _arrow_proxy
00781 {
00782 private:
00783 T value;
00784
00785 public:
00786
00787
00788
00789 _arrow_proxy(const T& x)
00790 : value(x)
00791 {
00792 }
00793
00794
00795
00796
00797 const T* operator->() const
00798 {
00799 return &value;
00800 }
00801
00802
00803
00804
00805
00806
00807 operator const T*() const
00808 {
00809 return &value;
00810 }
00811 };
00812
00813
00814
00815
00816 template<class V>
00817 class _mapped_proxy : public V
00818 {
00819 private:
00820 const Map::Handle m_hMap;
00821 Object::View m_vKey;
00822
00823 public:
00824
00825
00826
00827 _mapped_proxy(Map::Handle hMap, Object::View vKey, V v)
00828 : V(v), m_hMap(hMap), m_vKey(vKey)
00829 {
00830 COH_ENSURE_PARAM(hMap);
00831 }
00832
00833 public:
00834
00835
00836
00837 const _mapped_proxy& operator=(const V& v) const
00838 {
00839 m_hMap->putAll(Collections::singletonMap(m_vKey, v));
00840 return *this;
00841 }
00842
00843
00844
00845
00846 const _mapped_proxy& operator=(const _mapped_proxy& that) const
00847 {
00848 m_hMap->put(Collections::singletonMap(m_vKey, that.m_vValue));
00849 return *this;
00850 }
00851 };
00852
00853
00854
00855
00856 template<class V>
00857 bool operator==(const _mapped_proxy<V>& x, const _mapped_proxy<V>& y)
00858 {
00859 return V(x) == V(y);
00860 }
00861
00862
00863
00864
00865 template<class V>
00866 bool operator==(const _mapped_proxy<V>& x, const V& y)
00867 {
00868 return V(x) == y;
00869 }
00870
00871
00872
00873
00874 template<class V>
00875 bool operator==(const V& x, const _mapped_proxy<V>& y)
00876 {
00877 return x == V(y);
00878 }
00879
00880
00881
00882
00883 template<class V>
00884 bool operator!=(const _mapped_proxy<V>& x, const _mapped_proxy<V>& y)
00885 {
00886 return !(x == y);
00887 }
00888
00889
00890
00891
00892 template<class V>
00893 bool operator!=(const _mapped_proxy<V>& x, const V& y)
00894 {
00895 return !(x == y);
00896 }
00897
00898
00899
00900
00901 template<class V>
00902 bool operator!=(const V& x, const _mapped_proxy<V>& y)
00903 {
00904 return !(x == y);
00905 }
00906
00907
00908
00909
00910 template <typename Char, typename Traits, class V>
00911 COH_INLINE std::basic_ostream<Char, Traits>& operator<<(std::basic_ostream<Char, Traits>& out, const _mapped_proxy<V>& proxy)
00912 {
00913 return out << V(proxy);
00914 }
00915
00916
00917
00918
00919 template<class K, class V>
00920 struct _value_proxy
00921 : public std::pair< K, _mapped_proxy<V> >
00922 {
00923
00924
00925
00926 typedef std::pair< K, _mapped_proxy<V> > super;
00927
00928
00929
00930
00931 _value_proxy()
00932 : super()
00933 {
00934 }
00935
00936
00937
00938
00939 _value_proxy(const K& key, const _mapped_proxy<V>& proxy)
00940 : super(key, proxy)
00941 {
00942 }
00943 };
00944
00945
00946
00947
00948
00949
00950
00951 template<typename difference_type>
00952 class _iterator_base
00953 {
00954 protected:
00955
00956
00957
00958 FinalView<Map> f_vMap;
00959
00960
00961
00962
00963
00964
00965 MemberHandle<Iterator> m_hIter;
00966
00967
00968
00969
00970 MemberView<Object> m_vKey;
00971
00972
00973
00974
00975 _iterator_base(Map::View vMap, Object::View vKey)
00976 : f_vMap(System::common(), vMap),
00977 m_hIter(System::common(), NULL),
00978 m_vKey(System::common(), vKey)
00979 {
00980 }
00981
00982
00983
00984
00985 _iterator_base(Map::View vMap, Iterator::Handle hIter)
00986 : f_vMap(System::common(), vMap),
00987 m_hIter(System::common(), hIter),
00988 m_vKey(System::common(), hIter->hasNext() ? hIter->next() : NULL)
00989 {
00990 }
00991
00992
00993
00994
00995 _iterator_base(const _iterator_base& that)
00996 : f_vMap(System::common(), that.f_vMap),
00997 m_hIter(System::common(), NULL),
00998 m_vKey(System::common(), that.m_vKey)
00999 {
01000 Iterator::Handle endIterator = EndIterator::getInstance();
01001 if (endIterator == that.m_hIter)
01002 {
01003 m_hIter = endIterator;
01004 }
01005 }
01006
01007
01008
01009
01010 _iterator_base& operator=(const _iterator_base& that)
01011 {
01012 if (this != &that)
01013 {
01014 f_vMap = that.f_vMap;
01015 m_vKey = that.m_vKey;
01016
01017 Iterator::View vIterThat = that.m_hIter;
01018 m_hIter = vIterThat == NULL || vIterThat->hasNext()
01019 ? NULL
01020 : EndIterator::getInstance();
01021 }
01022 return *this;
01023 }
01024
01025
01026
01027
01028 void _next()
01029 {
01030 Iterator::Handle hIter = m_hIter;
01031 if (NULL == hIter)
01032 {
01033
01034
01035 hIter = f_vMap->keySet()->iterator();
01036 Object::View vKey = m_vKey;
01037
01038 while (hIter->hasNext() && !Object::equals(vKey, hIter->next()));
01039 m_hIter = hIter;
01040 }
01041 else if (!hIter->hasNext())
01042 {
01043 m_hIter = EndIterator::getInstance();
01044 m_vKey = NULL;
01045 return;
01046 }
01047
01048
01049 if (hIter->hasNext())
01050 {
01051 m_vKey = hIter->next();
01052 }
01053 else
01054 {
01055 m_vKey = NULL;
01056 }
01057 }
01058
01059
01060
01061
01062 void _incr(difference_type i)
01063 {
01064 COH_ENSURE(i >= 0);
01065 for ( ; i; --i)
01066 {
01067 _next();
01068 }
01069 }
01070
01071 public:
01072
01073
01074
01075 bool operator==(const _iterator_base& that) const
01076 {
01077 if (f_vMap == that.f_vMap)
01078 {
01079 if (m_hIter == EndIterator::getInstance())
01080 {
01081 return that.m_hIter == EndIterator::getInstance();
01082 }
01083 return Object::equals(m_vKey, that.m_vKey);
01084 }
01085 return false;
01086 }
01087
01088
01089
01090
01091 bool operator!=(const _iterator_base& i) const
01092 {
01093 return !(*this == i);
01094 }
01095 };
01096
01097
01098
01099
01100 template<class K, class V>
01101 class _const_iterator
01102 : public _iterator_base<typename _adapter_map_base<K,V>::difference_type>
01103 {
01104 friend class _adapter_map_base<K,V>;
01105 friend class _iterator<K,V>;
01106 typedef _iterator_base<typename _adapter_map_base<K,V>::difference_type>
01107 super;
01108
01109 public:
01110
01111
01112
01113 typedef std::forward_iterator_tag iterator_category;
01114
01115
01116
01117
01118 typedef typename _adapter_map_base<K,V>::difference_type difference_type;
01119
01120
01121
01122
01123 typedef typename _adapter_map_base<K,V>::value_type value_type;
01124
01125
01126
01127
01128 typedef value_type reference;
01129
01130
01131
01132
01133 typedef _arrow_proxy<const value_type> pointer;
01134
01135
01136
01137
01138 _const_iterator(const _const_iterator& that)
01139 : super(that)
01140 {
01141 }
01142
01143
01144
01145
01146 reference operator*() const
01147 {
01148 K key = cast<K>(super::m_vKey);
01149 return _make_pair(key, cast<V>(super::f_vMap->get(key)));
01150 }
01151
01152
01153
01154
01155 pointer operator->() const
01156 {
01157 return pointer(operator*());
01158 }
01159
01160
01161
01162
01163 _const_iterator& operator++()
01164 {
01165 super::_next();
01166 return *this;
01167 }
01168
01169
01170
01171
01172 _const_iterator operator++(int)
01173 {
01174 _const_iterator r = *this;
01175 super::_next();
01176 return r;
01177 }
01178
01179
01180
01181
01182 _const_iterator& operator+=(difference_type i)
01183 {
01184 _incr(i);
01185 return *this;
01186 }
01187
01188
01189
01190
01191 _const_iterator operator+(difference_type i) const
01192 {
01193 _const_iterator r = *this;
01194 return r += i;
01195 }
01196
01197
01198
01199
01200 reference operator[](difference_type i)
01201 {
01202 return *(*this + i);
01203 }
01204
01205 private:
01206
01207
01208
01209 _const_iterator(Map::View vMap, Iterator::Handle hIter)
01210 : super(vMap, hIter)
01211 {
01212 }
01213
01214
01215
01216
01217 _const_iterator(Map::View vMap, Object::View vKey)
01218 : super(vMap, vKey)
01219 {
01220 }
01221
01222
01223
01224
01225 reference _make_pair(K key, V value) const
01226 {
01227 return value_type(key, value);
01228 }
01229 };
01230
01231
01232
01233
01234 template<class K, class V>
01235 class _iterator
01236 : public _const_iterator<K,V>
01237 {
01238 friend class _adapter_map_base<K,V>;
01239
01240 private:
01241 typedef _const_iterator<K,V> super;
01242
01243 public:
01244
01245
01246
01247 _iterator(const _iterator& that)
01248 : super(that), f_hMap(System::common(), that.f_hMap, true)
01249 {
01250 }
01251
01252
01253
01254
01255 typedef typename super::difference_type difference_type;
01256
01257
01258
01259
01260 typedef const _mapped_proxy<V> mapped_reference;
01261
01262
01263
01264
01265 typedef const _value_proxy<const K, V> reference;
01266
01267
01268
01269
01270 typedef const _arrow_proxy<reference> pointer;
01271
01272
01273
01274
01275 reference operator*() const
01276 {
01277 K key = cast<K>(super::m_vKey);
01278 return _make_ref(key, cast<V>(f_hMap->get(key)));
01279 }
01280
01281
01282
01283
01284 pointer operator->() const
01285 {
01286 return pointer(operator*());
01287 }
01288
01289
01290
01291
01292 _iterator& operator++()
01293 {
01294 super::_next();
01295 return *this;
01296 }
01297
01298
01299
01300
01301 _iterator operator++(int)
01302 {
01303 _iterator r = *this;
01304 super::_next();
01305 return r;
01306 }
01307
01308
01309
01310
01311 _iterator& operator+=(difference_type i)
01312 {
01313 _incr(i);
01314 return *this;
01315 }
01316
01317
01318
01319
01320 _iterator operator+(difference_type i) const
01321 {
01322 _iterator r = *this;
01323 return r += i;
01324 }
01325
01326
01327
01328
01329 reference operator[](difference_type i)
01330 {
01331 return *(*this + i);
01332 }
01333
01334 private:
01335
01336
01337
01338 mutable FinalHandle<Map> f_hMap;
01339
01340
01341
01342
01343 _iterator(Map::Handle hMap, Iterator::Handle hIter)
01344 : super(hMap, hIter), f_hMap(System::common(), hMap, true)
01345 {
01346 }
01347
01348
01349
01350
01351 _iterator(Map::Handle hMap, Object::View vKey)
01352 : super(hMap, vKey), f_hMap(System::common(), hMap, true)
01353 {
01354 }
01355
01356
01357
01358
01359 reference _make_ref(K key, V value) const
01360 {
01361 return reference(key, _mapped_proxy<V>(f_hMap, key, value));
01362 }
01363 };
01364
01365
01366
01367
01368 template<class K, class V>
01369 inline _const_iterator<K,V> operator+
01370 (typename _const_iterator<K,V>::difference_type n,
01371 const _const_iterator<K,V>& x)
01372 {
01373 return x + n;
01374 }
01375
01376
01377
01378
01379 template<class K, class V>
01380 bool operator==(const _value_proxy<K,V>& x, const std::pair<K,V>& y)
01381 {
01382 return (x.first == y.first) && (x.second == y.second);
01383 }
01384
01385
01386
01387
01388 template<class K, class V>
01389 bool operator==(const std::pair<K,V>& x, const _value_proxy<K,V>& y)
01390 {
01391 return (x.first == y.first) && (x.second == y.second);
01392 }
01393
01394
01395
01396
01397 template<class K, class V>
01398 inline _iterator<K,V>
01399 operator+(typename _iterator<K,V>::difference_type n, const _iterator<K,V>& x)
01400 {
01401 return x + n;
01402 }
01403
01404
01405
01406
01407 template<class K, class V>
01408 inline typename _adapter_map_base<K,V>::iterator
01409 _adapter_map_base<K,V>::_make_iterator(Map::Handle hMap, Iterator::Handle hIter)
01410 {
01411 return iterator(hMap, hIter);
01412 }
01413
01414
01415
01416
01417 template<class K, class V>
01418 inline typename _adapter_map_base<K,V>::iterator
01419 _adapter_map_base<K,V>::_make_lazy_iterator(Map::Handle hMap, Object::View vKey)
01420 {
01421 return iterator(hMap, vKey);
01422 }
01423
01424
01425
01426
01427 template<class K, class V>
01428 inline typename _adapter_map_base<K,V>::const_iterator
01429 _adapter_map_base<K,V>::_make_const_iterator(Map::View vMap,
01430 Iterator::Handle hIter) const
01431 {
01432 return const_iterator(vMap, hIter);
01433 }
01434
01435
01436
01437
01438 template<class K, class V>
01439 inline typename _adapter_map_base<K,V>::const_iterator
01440 _adapter_map_base<K,V>::_make_lazy_const_iterator(Map::View vMap,
01441 Object::View vKey) const
01442 {
01443 return const_iterator(vMap, vKey);
01444 }
01445
01446 COH_CLOSE_NAMESPACE
01447
01448
01449
01450
01451
01452
01453
01454
01455
01456
01457 template<class K, class V>
01458 class adapter_map<K,V>::hasher
01459 : public std::unary_function<key_type, size32_t>
01460 {
01461 public:
01462
01463
01464
01465
01466
01467
01468
01469
01470 size32_t operator()(const key_type& k) const
01471 {
01472 return Object::hashCode(k);
01473 }
01474 };
01475
01476
01477
01478
01479
01480
01481 template<class K, class V>
01482 class adapter_map<K,V>::key_equal
01483 : public std::binary_function<key_type, key_type, bool>
01484 {
01485 public:
01486
01487
01488
01489
01490
01491
01492
01493
01494 bool operator()(const key_type& x, const key_type& y) const
01495 {
01496 return Object::equals(x, y);
01497 }
01498 };
01499
01500
01501
01502
01503
01504 template<class K, class V>
01505 inline typename adapter_map<K,V>::mapped_reference
01506 adapter_map<K,V>::operator[](const key_type& key)
01507 {
01508
01509
01510
01511
01512
01513
01514
01515
01516 Map::Handle hMap = delegate();
01517 return mapped_reference(hMap, key, cast<V>(hMap->get(key)));
01518 }
01519
01520 template<class K, class V>
01521 inline typename adapter_map<K,V>::mapped_type
01522 adapter_map<K,V>::operator[](const key_type& key) const
01523 {
01524
01525 return cast<V>(delegate()->get(key));
01526 }
01527
01528
01529 template<class K, class V>
01530 inline std::pair<typename adapter_map<K,V>::iterator, bool>
01531 adapter_map<K,V>::insert(const value_type& p)
01532 {
01533 Map::Handle hMap = delegate();
01534 if (instanceof<InvocableMap::Handle>(hMap))
01535 {
01536
01537 if (NULL == (cast<InvocableMap::Handle>(hMap)->
01538 invoke(p.first, ConditionalPut::create(NotFilter::create(
01539 PresentFilter::getInstance()), p.second, true))))
01540 {
01541
01542 return std::pair<iterator, bool>(find(p.first), false);
01543 }
01544 }
01545 else if (!hMap->containsKey(p.first))
01546 {
01547
01548 hMap->put(p.first, p.second);
01549 return std::pair<iterator, bool>(find(p.first), false);
01550 }
01551
01552
01553 return std::pair<iterator, bool>(find(p.first), true);
01554 }
01555
01556 template<class K, class V>
01557 template <class InputIterator>
01558 inline void adapter_map<K,V>::insert(InputIterator first, InputIterator last)
01559 {
01560 Map::Handle hMapDelegate = delegate();
01561 if (instanceof<InvocableMap::Handle>(hMapDelegate))
01562 {
01563 Map::Handle hMapTmp = HashMap::create();
01564 for ( ;first != last; ++first)
01565 {
01566 hMapTmp->put(K(first->first), V(first->second));
01567 }
01568
01569 cast<InvocableMap::Handle>(hMapDelegate)->invokeAll(
01570 (Collection::View) hMapTmp->keySet(),
01571 ConditionalPutAll::create(NotFilter::create(
01572 PresentFilter::getInstance()),
01573 hMapTmp));
01574 }
01575 else
01576 {
01577 for ( ;first != last; ++first)
01578 {
01579 K key = K(first->first);
01580 if (!hMapDelegate->containsKey(key))
01581 {
01582 hMapDelegate->put(key, V(first->second));
01583 }
01584 }
01585 }
01586 }
01587
01588 template<class K, class V>
01589 inline void adapter_map<K,V>::erase(iterator position)
01590 {
01591 erase(position->first);
01592 }
01593
01594 template<class K, class V>
01595 inline void adapter_map<K,V>::erase(iterator first, iterator last)
01596 {
01597 if (first == begin() && last == end())
01598 {
01599 clear();
01600 }
01601 else
01602 {
01603 while (first != last)
01604 {
01605 erase(first++);
01606 }
01607 }
01608 }
01609
01610
01611 template<class K, class V>
01612 inline std::pair< typename adapter_map<K,V>::iterator,
01613 typename adapter_map<K,V>::iterator >
01614 adapter_map<K,V>::equal_range(const key_type& x)
01615 {
01616 iterator a = find(x);
01617 iterator b = a == end() ? end() : a + 1;
01618 return make_pair(a, b);
01619 }
01620
01621 template<class K, class V>
01622 inline std::pair< typename adapter_map<K,V>::const_iterator,
01623 typename adapter_map<K,V>::const_iterator >
01624 adapter_map<K,V>::equal_range(const key_type& x) const
01625 {
01626 const_iterator a = find(x);
01627 const_iterator b = a == end() ? end() : a + 1;
01628 return make_pair(a, b);
01629 }
01630
01631 COH_CLOSE_NAMESPACE2
01632
01633 #endif // COH_ADAPTER_MAP_HPP