Oracle® Fusion Middleware C++ API Reference for Oracle Coherence
12c (12.2.1.4.0)

E90870-01

coherence/stl/adapter_map.hpp

00001 /*
00002 * adapter_map.hpp
00003 *
00004 * Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
00005 *
00006 * Oracle is a registered trademarks of Oracle Corporation and/or its
00007 * affiliates.
00008 *
00009 * This software is the confidential and proprietary information of Oracle
00010 * Corporation. You shall not disclose such confidential and proprietary
00011 * information and shall use it only in accordance with the terms of the
00012 * license agreement you entered into with Oracle.
00013 *
00014 * This notice may not be removed or altered.
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 // ----- pre-definitions ----------------------------------------------------
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 * @internal
00062 */
00063 template<class K, class V>
00064 class _adapter_map_base
00065     {
00066     // ----- typedefs -------------------------------------------------------
00067 
00068     public:
00069         /**
00070         * @internal
00071         */
00072         typedef K key_type;
00073 
00074         /**
00075         * @internal
00076         */
00077         typedef V data_type;
00078 
00079         /**
00080         * @internal
00081         */
00082         typedef V mapped_type;
00083 
00084         /**
00085         * @internal
00086         */
00087         typedef std::pair<const key_type, mapped_type> value_type;
00088 
00089         /**
00090         * @internal
00091         */
00092         typedef std::allocator<value_type> allocator_type;
00093 
00094         /**
00095         * @internal
00096         */
00097         typedef typename allocator_type::size_type size_type;
00098 
00099         /**
00100         * @internal
00101         */
00102         typedef typename allocator_type::difference_type difference_type;
00103 
00104         /**
00105         * @internal
00106         */
00107         typedef _iterator<K,V> iterator;
00108 
00109         /**
00110         * @internal
00111         */
00112         typedef _const_iterator<K,V> const_iterator;
00113 
00114 
00115     // ----- local class methods --------------------------------------------
00116 
00117     protected:
00118         /**
00119         * @internal
00120         */
00121         iterator _make_iterator(Map::Handle hMap, Iterator::Handle hIter);
00122 
00123         /**
00124         * @internal
00125         */
00126         iterator _make_lazy_iterator(Map::Handle hMap, Object::View vKey);
00127 
00128         /**
00129         * @internal
00130         */
00131         const_iterator _make_const_iterator(Map::View vMap,
00132                                             Iterator::Handle hIter) const;
00133 
00134         /**
00135         * @internal
00136         */
00137         const_iterator _make_lazy_const_iterator(Map::View vMap,
00138                                             Object::View vKey) const;
00139     };
00140 
00141     /**
00142     * @internal
00143     *
00144     * Iterator which represents the end of an iteration.
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 // impl
00179 
00180 
00181 // ----- class: adapter_map -------------------------------------------------
00182 
00183 /**
00184 * adapter_map provides an STL-like "pair associative container" wrapper
00185 * around coherence::util::Map implementations.
00186 *
00187 * An example usage examples may look as follows:
00188 * @code
00189 * typedef adapter_map<String::View, Integer32::View> month_cache;
00190 *
00191 * month_cache months(CacheFactory::getCache("dist-months"));
00192 *
00193 * months["january"]  = Integer32::valueOf(31);
00194 * months["february"] = Integer32::valueOf(28);
00195 * months["march"]    = Integer32::valueOf(31);
00196 * ...
00197 *
00198 * for (month_cache::iterator i = months.begin(), e = months.end(); i != e; ++i)
00199 *     {
00200 *     std::cout << i->first << " = " << i->second << std::endl;
00201 *     }
00202 * @endcode
00203 *
00204 * adapter_map only supports keys and values which are handles, for instance
00205 * Integer32::View. See boxing_map for a variant which supports keys and values
00206 * of non-managed types, including primitives.
00207 *
00208 * @see boxing_map
00209 * @see Map
00210 * @see TypedCollections
00211 */
00212 template<class K, class V>
00213 class adapter_map
00214         : private impl::_adapter_map_base<K,V>
00215     {
00216     // ----- typedefs -------------------------------------------------------
00217 
00218     private:
00219         /**
00220         * Super type
00221         */
00222         typedef impl::_adapter_map_base<K,V> super;
00223 
00224     public:
00225         /**
00226         * This type.
00227         */
00228         typedef adapter_map<K, V> this_type;
00229 
00230         /**
00231         * Key type of the map. Should be handle-to-const type.
00232         */
00233         typedef typename super::key_type key_type;
00234 
00235         /**
00236         * Value type of the map. Should be handle-to-const type.
00237         */
00238         typedef typename super::data_type data_type;
00239 
00240         /**
00241         * Value type of the map. Should be handle-to-const type.
00242         */
00243         typedef typename super::mapped_type mapped_type;
00244 
00245         /**
00246         * Entry type of the map. value_type is
00247         * @code std::pair<const key_type, mapped_type> @endcode.
00248         */
00249         typedef typename super::value_type value_type;
00250 
00251         /**
00252         * Allocator type for this map. allocator_type is
00253         * @code std::allocator<value_type> @endcode.
00254         */
00255         typedef typename super::allocator_type allocator_type;
00256 
00257         /**
00258         * Signed integral type. Identical to the difference type of iterator
00259         * and const_iterator.
00260         */
00261         typedef typename super::difference_type difference_type;
00262 
00263         /**
00264         * Unsigned integral type. size_type can represent any non-negative
00265         * value of difference_type.
00266         */
00267         typedef typename super::size_type size_type;
00268 
00269         /**
00270         * Iterator type of the <i>forward iterator category</i> which
00271         * iterates key-value std::pairs of this map. Convertible to
00272         * const_iterator.
00273         */
00274         typedef typename super::iterator iterator;
00275 
00276         /**
00277         * Const iterator type of the <i>forward iterator category</i> which
00278         * iterates key-value std::pairs of this map.
00279         */
00280         typedef typename super::const_iterator const_iterator;
00281 
00282         /**
00283         * Type of the values returned by @c iterator.operator->
00284         */
00285         typedef typename iterator::pointer pointer;
00286 
00287         /**
00288         * Type of the values returned by @c const_iterator.operator->
00289         */
00290         typedef typename const_iterator::pointer const_pointer;
00291 
00292         /**
00293         * Type of the values returned by @c iterator.operator*
00294         */
00295         typedef typename iterator::reference reference;
00296 
00297         /**
00298         * Type of the values returned by @c const_iterator.operator*
00299         */
00300         typedef typename const_iterator::reference const_reference;
00301 
00302         /**
00303         * Type of the values returned by adapter_map.operator[]()
00304         * operator[]
00305         */
00306         typedef typename iterator::mapped_reference mapped_reference;
00307 
00308 
00309     // ---- hash map-specific -----------------------------------------------
00310 
00311     public:
00312         class hasher;
00313         class key_equal;
00314 
00315 
00316     // ----- constructors ---------------------------------------------------
00317 
00318     public:
00319         /**
00320         * Create new adapter_map from the given Coherence Map.
00321         *
00322         * @param ohMap the Map to be delegated by the adapter_map.
00323         */
00324         adapter_map(Map::Holder ohMap = NULL)
00325             : f_ohMap(System::common(), ohMap)
00326             {
00327             }
00328 
00329         /**
00330         * Create a adapter_map copy. The new adapter_map will reference the
00331         * same Map, as the original adapter_map.
00332         */
00333         adapter_map(const this_type& that)
00334             : f_ohMap(System::common(), that.f_ohMap)
00335             {
00336             }
00337 
00338         /**
00339         * destructor
00340         */
00341         virtual ~adapter_map()
00342             {
00343             }
00344 
00345         /**
00346         * Reassign this adapter_map to reference the same Map as another
00347         * adapter_map.
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         * Reassign this adapter_map to reference another Map.
00357         */
00358         this_type& operator=(Map::Holder ohMap)
00359             {
00360             initialize(f_ohMap, ohMap);
00361             return *this;
00362             }
00363 
00364 
00365     // ---- delegate access -------------------------------------------------
00366 
00367     public:
00368         /**
00369         * Return the coherence::util::Map to which this adapter_map delegates.
00370         *
00371         * @return the associated coherence::util::Map::Handle
00372         *
00373         * @throws UnsupportedOperationException if the adapater_map is
00374         *         delegating to a Map::View, rather then Map::Handle
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         * Return the coherence::util::Map to which this adapter_map delegates.
00389         *
00390         * @return the associated coherence::util::Map::View
00391         */
00392         Map::View delegate() const
00393             {
00394             return f_ohMap;
00395             }
00396 
00397 
00398     // ---- capacity --------------------------------------------------------
00399 
00400     public:
00401         /**
00402         * @return the number of the key-value std::pairs in the map
00403         */
00404         size_type size() const
00405             {
00406             return delegate()->size();
00407             }
00408 
00409         /**
00410         * @return size() of the largest possible map
00411         */
00412         size_type max_size() const
00413             {
00414             return static_cast<size_type>(-1);
00415             }
00416 
00417         /**
00418         * @return true only iff this map is empty
00419         */
00420         bool empty() const
00421             {
00422             return (size() == 0);
00423             }
00424 
00425 
00426     // ----- iterators ------------------------------------------------------
00427 
00428     public:
00429         /**
00430         * @return a const_iterator referring to the first element in the
00431         *         container; if the map is empty, then
00432         *         @code cbegin() == cend() @endcode
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         * @return a const_iterator referring to the first element in the
00443         *         container; if the map is empty, then
00444         *         @code begin() == end() @endcode
00445         */
00446         const_iterator begin() const
00447             {
00448             return cbegin();
00449             }
00450 
00451         /**
00452         * @return an iterator referring to the first element in the
00453         *         container; if the map is empty, then
00454         *         @code begin() == end() @endcode
00455         */
00456         iterator begin()
00457             {
00458             Map::Handle hMap = delegate();
00459             return super::_make_iterator(hMap, hMap->keySet()->iterator());
00460             }
00461 
00462         /**
00463         * @return a const_iterator which is the past-the-end value for the
00464         *         container; if the map is empty, then
00465         *         @code cbegin() == cend() @endcode
00466         */
00467         const_iterator cend() const
00468             {
00469             return super::_make_const_iterator(delegate(),
00470                     impl::EndIterator::getInstance());
00471             }
00472 
00473         /**
00474         * @return a const_iterator which is the past-the-end value for the
00475         *         container; if the map is empty, then
00476         *         @code begin() == end() @endcode
00477         */
00478         const_iterator end() const
00479             {
00480             return cend();
00481             }
00482 
00483         /**
00484         * @return an iterator which is the past-the-end value for the
00485         *         container; if the map is empty, then
00486         *         @code begin() == end()  @endcode
00487         */
00488         iterator end()
00489             {
00490             return super::_make_iterator(delegate(),
00491                     impl::EndIterator::getInstance());
00492             }
00493 
00494 
00495     // ----- element access -------------------------------------------------
00496 
00497     public:
00498         /**
00499         * Return a reference to the value mapped to the key.
00500         *
00501         * @param key a key to search in the map
00502         *
00503         * @return a reference to x.second, where x is the (unique) element
00504         *         whose key is equal to @a key
00505         */
00506         mapped_reference operator[](const key_type& key);
00507 
00508         /**
00509         * Return a reference to the value mapped to the key.
00510         *
00511         * @param key a key to search in the map
00512         *
00513         * @return a reference to x.second, where x is the (unique) element
00514         *         whose key is equal to @a key
00515         */
00516         mapped_type operator[](const key_type& key) const;
00517 
00518 
00519     // ----- modifiers ------------------------------------------------------
00520 
00521     public:
00522         /**
00523         * Insert @a x if and only if there is no element in the map with key
00524         * equal to the key of @a x. The bool component of the returned
00525         * std::pair is true if and only if the insertion takes place, and the
00526         * iterator component of the std::pair points to the element with key
00527         * equal to the key of argument.
00528         *
00529         * All insert methods do not affect the validity of references to map
00530         * elements, but may invalidate all iterators to the map.
00531         *
00532         * @param x the key-value std::pair to insert into this map
00533         *
00534         * @return the std::pair which indicates the insertion status and points to
00535         *         the corresponding map entry
00536         */
00537         std::pair<iterator, bool> insert(const value_type& x);
00538 
00539         /**
00540         * Insert @a x if and only if there is no element in the map with key
00541         * equal to the key of @a x.
00542         *
00543         * All insert methods do not affect the validity of references to map
00544         * elements, but may invalidate all iterators to the map.
00545         *
00546         * @param hint a hint pointing to where the insert should start to
00547         *             search. Implementation is permitted to ignore the hint.
00548         * @param x    the key-value std::pair to insert into this map.
00549         *
00550         * @return iterator pointing to the element with key equal to the key
00551         *         of @a x.
00552         */
00553         iterator insert(iterator /*hint*/, const value_type& x)
00554             {
00555             return insert(x).first;
00556             }
00557 
00558         /**
00559         * Insert each element from the range [@a first, @a last) if and only
00560         * if there is no element with key equal to the key of that element in
00561         * this map.
00562         *
00563         * All insert methods do not affect the validity of references to map
00564         * elements, but may invalidate all iterators to the map.
00565         *
00566         * @note it is required that @a first and @a last are not iterators
00567         *       into this map. Otherwise, behavior of
00568         *       @code insert(first, last) @endcode is undefined.
00569         */
00570         template<class InputIterator>
00571         void insert(InputIterator first, InputIterator last);
00572 
00573         /**
00574         * Erase an element pointed to by @a position.
00575         *
00576         * All erase methods invalidate only iterators and references to the
00577         * erased elements.
00578         *
00579         * @param position an iterator specifying the element to erase
00580         */
00581         void erase(iterator position);
00582 
00583         /**
00584         * Erase all elements in the range <tt>[@a first, last)</tt>.
00585         *
00586         * All erase methods invalidate only iterators and references to the
00587         * erased elements.
00588         */
00589         void erase(iterator first, iterator last);
00590 
00591         /**
00592         * Erase an element in the map with key equal to the argument.
00593         *
00594         * All erase methods invalidate only iterators and references to the
00595         * erased elements.
00596         *
00597         * @param k key whose mapping is to be removed from the map.
00598         *
00599         * @return  the number of erased elements (0 or 1).
00600         */
00601         size_type erase(const key_type& k)
00602             {
00603             return delegate()->keySet()->remove(k) ? 1 : 0;
00604             }
00605 
00606         /**
00607         * Remove all elements from this map.
00608         */
00609         void clear()
00610             {
00611             delegate()->clear();
00612             }
00613 
00614         /**
00615         * Swap this map and argument map.
00616         */
00617         void swap(this_type& x)
00618             {
00619             std::swap(f_ohMap, x.f_ohMap);
00620             }
00621 
00622 
00623     // ----- map operations -------------------------------------------------
00624 
00625     public:
00626 
00627         /**
00628         * @return an iterator pointing to an element with the key equal to
00629         * @a k, or this->end() if such an element is not found.
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         * @return an iterator pointing to an element with the key equal to
00644         * @a k, or this->end() if such an element is not found.
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         * @return the number of element with key equal to @a k (0 or 1).
00659         */
00660         size_type count(const key_type& k) const
00661             {
00662             return delegate()->containsKey(k) ? 1 : 0;
00663             }
00664 
00665         // sorted map-specific methods lower_bound() and upper_bound() are
00666         // not supported
00667 
00668         /**
00669         * @return a std::pair of iterators which locate the element with key @a k.
00670         *         If such element is found then the first component of the
00671         *         returned std::pair points to the element and the second
00672         *         component of the std::pair points to the element immediately
00673         *         following the found one in the iterator's traverse order.
00674         *         Otherwise returns a this->end(), this->;end() std::pair.
00675         *
00676         * In other words, equal_range always returns the range [x, y) which
00677         * contains the found element only.
00678         */
00679         std::pair<iterator,iterator> equal_range(const key_type& k);
00680 
00681         /**
00682         * @return a std::pair of iterators which locate the element with key @a k.
00683         *         If such element is found then the first component of the
00684         *         returned std::pair points to the element and the second
00685         *         component of the std::pair points to the element immediately
00686         *         following the found one in the iterator's traverse order.
00687         *         Otherwise returns an this->end(), this->end() std::pair.
00688         *
00689         * In other words, equal_range always returns the range [x, y) which
00690         * contains the found element only.
00691         */
00692         std::pair<const_iterator,const_iterator> equal_range(const key_type& k)
00693             const;
00694 
00695 
00696     // ----- other methods --------------------------------------------------
00697 
00698     public:
00699         /**
00700         * @return a copy of the Allocator object
00701         */
00702         allocator_type get_allocator() const
00703             {
00704             return allocator_type();
00705             }
00706 
00707 
00708     // ----- observers: unordered map ---------------------------------------
00709 
00710     public:
00711         /**
00712         * @return hash function
00713         */
00714         hasher hash_function() const
00715             {
00716             return hasher();
00717             }
00718 
00719         /**
00720         * @return key equality predicate
00721         */
00722         key_equal key_eq() const
00723             {
00724             return key_equal();
00725             }
00726 
00727 
00728     // ----- data members ---------------------------------------------------
00729 
00730     protected:
00731         /**
00732         * The Coherence Map that is delegated by the adapter_map.
00733         */
00734         FinalHolder<Map> f_ohMap;
00735     };
00736 
00737 //operators:
00738 
00739 // operators ==,!=,<,>,<=,>= are NOT defined for adapter_map
00740 // as adapter_map can be sorted(ordered) or unordered map and
00741 // these operators are not required to be defined for unordered associative containers.
00742 // (see TR1:6.3.1.2 [tr.unord.req.2])
00743 
00744 //specialized algorithms:
00745 
00746 /** @relates adapter_map
00747 * Swaps two argument maps.
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 * Copy the contents of one map to another.
00757 *
00758 * @param mapSrc  the source map
00759 * @param mapDes  the destination map
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 // ----- implementation -----------------------------------------------------
00770 
00771 COH_OPEN_NAMESPACE(impl)
00772 
00773 
00774 // ----- class _array_proxy -------------------------------------------------
00775 
00776 /**
00777 * @internal
00778 */
00779 template<class T>
00780 class _arrow_proxy
00781     {
00782     private:
00783         T value;
00784 
00785     public:
00786         /**
00787         * @internal
00788         */
00789         _arrow_proxy(const T& x)
00790                 : value(x)
00791             {
00792             }
00793 
00794         /**
00795         * @internal
00796         */
00797         const T* operator->() const
00798             {
00799             return &value;
00800             }
00801 
00802         // This function is needed for MWCW and BCC, which won't call
00803         // operator-> again automatically per 13.3.1.2 para 8
00804         /**
00805         * @internal
00806         */
00807         operator const T*() const
00808             {
00809             return &value;
00810             }
00811     };
00812 
00813 /**
00814 * @internal
00815 */
00816 template<class V>
00817 class _mapped_proxy : public V
00818     {
00819     private:
00820         const Map::Handle   m_hMap; // on stack
00821         Object::View        m_vKey; // on stack
00822 
00823     public: //not for user!
00824         /**
00825         * @internal
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         * @internal
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         * @internal
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 * @internal
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 * @internal
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 * @internal
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 * @internal
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 * @internal
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 * @internal
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 * @internal
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 * @internal
00918 */
00919 template<class K, class V>
00920 struct _value_proxy
00921         : public std::pair< K, _mapped_proxy<V> >
00922     {
00923         /**
00924         * @internal
00925         */
00926         typedef std::pair< K, _mapped_proxy<V> > super;
00927 
00928         /**
00929         * @internal
00930         */
00931         _value_proxy()
00932                 : super()
00933             {
00934             }
00935 
00936         /**
00937         * @internal
00938         */
00939         _value_proxy(const K& key, const _mapped_proxy<V>& proxy)
00940                 : super(key, proxy)
00941             {
00942             }
00943     };
00944 
00945 
00946 // ----- iterators ----------------------------------------------------------
00947 
00948 /**
00949 * @internal
00950 */
00951 template<typename difference_type>
00952 class _iterator_base
00953     {
00954     protected:
00955         /**
00956         * The associated Map.
00957         */
00958         FinalView<Map> f_vMap;
00959 
00960         /**
00961         * The delegate iterator. A value of NULL indicates that the iterator
00962         * has been created lazily, and the first call to _next() will advance
00963         * the iterator to m_vKey.
00964         */
00965         MemberHandle<Iterator> m_hIter;
00966 
00967         /**
00968         * The current key associated with the iterator.
00969         */
00970         MemberView<Object> m_vKey;
00971 
00972         /**
00973         * @internal
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         * @internal
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         * @internal
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         * @internal
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 /*lazy*/
01020                         : EndIterator::getInstance();
01021                 }
01022             return *this;
01023             }
01024 
01025         /**
01026         * @internal
01027         */
01028         void _next()
01029             {
01030             Iterator::Handle hIter = m_hIter;
01031             if (NULL == hIter)
01032                 {
01033                 // lazy create, and advance the iterator past the stored key
01034                 // this is potentially expensive, but also quite rare
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         * @internal
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         * @internal
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         * @internal
01090         */
01091         bool operator!=(const _iterator_base& i) const
01092             {
01093             return !(*this == i);
01094             }
01095     };
01096 
01097 /**
01098 * @internal
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         * @internal
01112         */
01113         typedef std::forward_iterator_tag iterator_category;
01114 
01115         /**
01116         * @internal
01117         */
01118         typedef typename _adapter_map_base<K,V>::difference_type difference_type;
01119 
01120         /**
01121         * @internal
01122         */
01123         typedef typename _adapter_map_base<K,V>::value_type value_type;
01124 
01125         /**
01126         * @internal
01127         */
01128         typedef value_type reference;
01129 
01130         /**
01131         * @internal
01132         */
01133         typedef _arrow_proxy<const value_type> pointer;
01134 
01135         /**
01136         * @internal
01137         */
01138         _const_iterator(const _const_iterator& that)
01139             : super(that)
01140             {
01141             }
01142 
01143         /**
01144         * @internal
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         * @internal
01154         */
01155         pointer operator->() const
01156             {
01157             return pointer(operator*());
01158             }
01159 
01160         /**
01161         * @internal
01162         */
01163         _const_iterator& operator++()
01164             {
01165             super::_next();
01166             return *this;
01167             }
01168 
01169         /**
01170         * @internal
01171         */
01172         _const_iterator operator++(int)
01173             {
01174             _const_iterator r = *this;
01175             super::_next();
01176             return r;
01177             }
01178 
01179         /**
01180         * @internal
01181         */
01182         _const_iterator& operator+=(difference_type i)
01183             {
01184             _incr(i);
01185             return *this;
01186             }
01187 
01188         /**
01189         * @internal
01190         */
01191         _const_iterator operator+(difference_type i) const
01192             {
01193             _const_iterator r = *this;
01194             return r += i;
01195             }
01196 
01197         /**
01198         * @internal
01199         */
01200         reference operator[](difference_type i)
01201             {
01202             return *(*this + i);
01203             }
01204 
01205     private:
01206         /**
01207         * @internal
01208         */
01209         _const_iterator(Map::View vMap, Iterator::Handle hIter)
01210                 : super(vMap, hIter)
01211             {
01212             }
01213 
01214         /**
01215         * @internal
01216         */
01217         _const_iterator(Map::View vMap, Object::View vKey)
01218                 : super(vMap, vKey)
01219             {
01220             }
01221 
01222         /**
01223         * @internal
01224         */
01225         reference _make_pair(K key, V value) const
01226             {
01227             return value_type(key, value);
01228             }
01229     };
01230 
01231 /**
01232 * @internal
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         * @internal
01246         */
01247         _iterator(const _iterator& that)
01248             : super(that), f_hMap(System::common(), that.f_hMap, true)
01249             {
01250             }
01251 
01252         /**
01253         * @internal
01254         */
01255         typedef typename super::difference_type difference_type;
01256 
01257         /**
01258         * @internal
01259         */
01260         typedef const _mapped_proxy<V> mapped_reference;
01261 
01262         /**
01263         * @internal
01264         */
01265         typedef const _value_proxy<const K, V> reference;
01266 
01267         /**
01268         * @internal
01269         */
01270         typedef const _arrow_proxy<reference> pointer;
01271 
01272         /**
01273         * @internal
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         * @internal
01283         */
01284         pointer operator->() const
01285             {
01286             return pointer(operator*());
01287             }
01288 
01289         /**
01290         * @internal
01291         */
01292         _iterator& operator++()
01293             {
01294             super::_next();
01295             return *this;
01296             }
01297 
01298         /**
01299         * @internal
01300         */
01301         _iterator operator++(int)
01302             {
01303             _iterator r = *this;
01304             super::_next();
01305             return r;
01306             }
01307 
01308         /**
01309         * @internal
01310         */
01311         _iterator& operator+=(difference_type i)
01312             {
01313             _incr(i);
01314             return *this;
01315             }
01316 
01317         /**
01318         * @internal
01319         */
01320         _iterator operator+(difference_type i) const
01321             {
01322             _iterator r = *this;
01323             return r += i;
01324             }
01325 
01326         /**
01327         * @internal
01328         */
01329         reference operator[](difference_type i)
01330             {
01331             return *(*this + i);
01332             }
01333 
01334     private:
01335         /**
01336         * @internal
01337         */
01338         mutable FinalHandle<Map> f_hMap;
01339 
01340         /**
01341         * @internal
01342         */
01343         _iterator(Map::Handle hMap, Iterator::Handle hIter)
01344             : super(hMap, hIter), f_hMap(System::common(), hMap, true)
01345             {
01346             }
01347 
01348         /**
01349         * @internal
01350         */
01351         _iterator(Map::Handle hMap, Object::View vKey)
01352                 : super(hMap, vKey), f_hMap(System::common(), hMap, true)
01353             {
01354             }
01355 
01356         /**
01357         * @internal
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 * @internal
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 * @internal
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 * @internal
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 * @internal
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 * @internal
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 * @internal
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 * @internal
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 * @internal
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 // impl
01447 
01448 
01449 // ----- Observers -----------------------------------------------------------
01450 
01451 /** @relates adapter_map
01452 * Hasher function.
01453 *
01454 * A function object that takes a single argument of type key_type and
01455 * returns a value of type size32_t.
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         * Determine a hash code corresponding to a given key object.
01464         *
01465         * @param k the handle to the key Object, may be @c NULL.
01466         *
01467         * @return a hash code of the key object, or 0 if @c NULL handle
01468         *         was provided
01469         */
01470         size32_t operator()(const key_type& k) const
01471             {
01472             return Object::hashCode(k);
01473             }
01474     };
01475 
01476 /** @relates adapter_map
01477 * Key equality relation.
01478 *
01479 * Binary predicate that takes two arguments of type key_type.
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         * Compare two key objects for equality.
01488         *
01489         * @param x the handle to the first key Object
01490         * @param y the handle to the second key Object
01491         *
01492         * @return whether two keys are equal
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 // ----- adapter_map methods -----------------------------------------------------
01502 
01503 //element access:
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     // operator[] is not part of the associative container concept, but is
01509     // defined within map implementations. As it is not part of the concept
01510     // we have some leverage with respect to the implementation. Most notably
01511     // we are not required to delegate to the insert method, which can be
01512     // expensive as it requires an iteration scan. Additionally we are not
01513     // required to insert of test, i.e. v = m[k], does not have to set a value
01514     // for k if it did not already exist
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     // see note from non-const version
01525     return cast<V>(delegate()->get(key));
01526     }
01527 
01528 //modifiers:
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         // perform conditional put
01537         if (NULL == (cast<InvocableMap::Handle>(hMap)->
01538                 invoke(p.first, ConditionalPut::create(NotFilter::create(
01539                         PresentFilter::getInstance()), p.second, true))))
01540             {
01541             // insert occurred
01542             return std::pair<iterator, bool>(find(p.first), false);
01543             }
01544         }
01545     else if (!hMap->containsKey(p.first))
01546         {
01547         // key not found, insert element
01548         hMap->put(p.first, p.second);
01549         return std::pair<iterator, bool>(find(p.first), false);
01550         }
01551 
01552     // key already exists in the map
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 //map operations:
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
Copyright © 2000, 2019, Oracle and/or its affiliates. All rights reserved.