Home | History | Annotate | Download | only in runtime
      1 /*
      2  * Copyright (C) 2012 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #ifndef ART_RUNTIME_SAFE_MAP_H_
     18 #define ART_RUNTIME_SAFE_MAP_H_
     19 
     20 #include <map>
     21 #include <memory>
     22 #include <type_traits>
     23 
     24 #include "base/allocator.h"
     25 #include "base/logging.h"
     26 
     27 namespace art {
     28 
     29 // Equivalent to std::map, but without operator[] and its bug-prone semantics (in particular,
     30 // the implicit insertion of a default-constructed value on failed lookups).
     31 template <typename K, typename V, typename Comparator = std::less<K>,
     32           typename Allocator = TrackingAllocator<std::pair<const K, V>, kAllocatorTagSafeMap>>
     33 class SafeMap {
     34  private:
     35   typedef SafeMap<K, V, Comparator, Allocator> Self;
     36 
     37  public:
     38   typedef typename ::std::map<K, V, Comparator, Allocator>::key_compare key_compare;
     39   typedef typename ::std::map<K, V, Comparator, Allocator>::value_compare value_compare;
     40   typedef typename ::std::map<K, V, Comparator, Allocator>::allocator_type allocator_type;
     41   typedef typename ::std::map<K, V, Comparator, Allocator>::iterator iterator;
     42   typedef typename ::std::map<K, V, Comparator, Allocator>::const_iterator const_iterator;
     43   typedef typename ::std::map<K, V, Comparator, Allocator>::size_type size_type;
     44   typedef typename ::std::map<K, V, Comparator, Allocator>::key_type key_type;
     45   typedef typename ::std::map<K, V, Comparator, Allocator>::value_type value_type;
     46 
     47   SafeMap() = default;
     48   SafeMap(const SafeMap&) = default;
     49   explicit SafeMap(const key_compare& cmp, const allocator_type& allocator = allocator_type())
     50     : map_(cmp, allocator) {
     51   }
     52 
     53   Self& operator=(const Self& rhs) {
     54     map_ = rhs.map_;
     55     return *this;
     56   }
     57 
     58   allocator_type get_allocator() const { return map_.get_allocator(); }
     59   key_compare key_comp() const { return map_.key_comp(); }
     60   value_compare value_comp() const { return map_.value_comp(); }
     61 
     62   iterator begin() { return map_.begin(); }
     63   const_iterator begin() const { return map_.begin(); }
     64   iterator end() { return map_.end(); }
     65   const_iterator end() const { return map_.end(); }
     66 
     67   bool empty() const { return map_.empty(); }
     68   size_type size() const { return map_.size(); }
     69 
     70   void swap(Self& other) { map_.swap(other.map_); }
     71   void clear() { map_.clear(); }
     72   iterator erase(iterator it) { return map_.erase(it); }
     73   size_type erase(const K& k) { return map_.erase(k); }
     74 
     75   iterator find(const K& k) { return map_.find(k); }
     76   const_iterator find(const K& k) const { return map_.find(k); }
     77 
     78   iterator lower_bound(const K& k) { return map_.lower_bound(k); }
     79   const_iterator lower_bound(const K& k) const { return map_.lower_bound(k); }
     80 
     81   size_type count(const K& k) const { return map_.count(k); }
     82 
     83   // Note that unlike std::map's operator[], this doesn't return a reference to the value.
     84   V Get(const K& k) const {
     85     const_iterator it = map_.find(k);
     86     DCHECK(it != map_.end());
     87     return it->second;
     88   }
     89 
     90   // Used to insert a new mapping.
     91   iterator Put(const K& k, const V& v) {
     92     std::pair<iterator, bool> result = map_.emplace(k, v);
     93     DCHECK(result.second);  // Check we didn't accidentally overwrite an existing value.
     94     return result.first;
     95   }
     96   iterator Put(const K& k, V&& v) {
     97     std::pair<iterator, bool> result = map_.emplace(k, std::move(v));
     98     DCHECK(result.second);  // Check we didn't accidentally overwrite an existing value.
     99     return result.first;
    100   }
    101 
    102   // Used to insert a new mapping at a known position for better performance.
    103   iterator PutBefore(const_iterator pos, const K& k, const V& v) {
    104     // Check that we're using the correct position and the key is not in the map.
    105     DCHECK(pos == map_.end() || map_.key_comp()(k, pos->first));
    106     DCHECK(pos == map_.begin() || map_.key_comp()((--const_iterator(pos))->first, k));
    107     return map_.emplace_hint(pos, k, v);
    108   }
    109   iterator PutBefore(const_iterator pos, const K& k, V&& v) {
    110     // Check that we're using the correct position and the key is not in the map.
    111     DCHECK(pos == map_.end() || map_.key_comp()(k, pos->first));
    112     DCHECK(pos == map_.begin() || map_.key_comp()((--const_iterator(pos))->first, k));
    113     return map_.emplace_hint(pos, k, std::move(v));
    114   }
    115 
    116   // Used to insert a new mapping or overwrite an existing mapping. Note that if the value type
    117   // of this container is a pointer, any overwritten pointer will be lost and if this container
    118   // was the owner, you have a leak. Returns iterator pointing to the new or overwritten entry.
    119   iterator Overwrite(const K& k, const V& v) {
    120     std::pair<iterator, bool> result = map_.insert(std::make_pair(k, v));
    121     if (!result.second) {
    122       // Already there - update the value for the existing key
    123       result.first->second = v;
    124     }
    125     return result.first;
    126   }
    127 
    128   template <typename CreateFn>
    129   V GetOrCreate(const K& k, CreateFn create) {
    130     static_assert(std::is_same<V, typename std::result_of<CreateFn()>::type>::value,
    131                   "Argument `create` should return a value of type V.");
    132     auto lb = lower_bound(k);
    133     if (lb != end() && !key_comp()(k, lb->first)) {
    134       return lb->second;
    135     }
    136     auto it = PutBefore(lb, k, create());
    137     return it->second;
    138   }
    139 
    140   bool Equals(const Self& rhs) const {
    141     return map_ == rhs.map_;
    142   }
    143 
    144  private:
    145   ::std::map<K, V, Comparator, Allocator> map_;
    146 };
    147 
    148 template <typename K, typename V, typename Comparator, typename Allocator>
    149 bool operator==(const SafeMap<K, V, Comparator, Allocator>& lhs,
    150                 const SafeMap<K, V, Comparator, Allocator>& rhs) {
    151   return lhs.Equals(rhs);
    152 }
    153 
    154 template <typename K, typename V, typename Comparator, typename Allocator>
    155 bool operator!=(const SafeMap<K, V, Comparator, Allocator>& lhs,
    156                 const SafeMap<K, V, Comparator, Allocator>& rhs) {
    157   return !(lhs == rhs);
    158 }
    159 
    160 template<class Key, class T, AllocatorTag kTag, class Compare = std::less<Key>>
    161 class AllocationTrackingSafeMap : public SafeMap<
    162     Key, T, Compare, TrackingAllocator<std::pair<const Key, T>, kTag>> {
    163 };
    164 
    165 }  // namespace art
    166 
    167 #endif  // ART_RUNTIME_SAFE_MAP_H_
    168