X-Git-Url: http://plrg.eecs.uci.edu/git/?a=blobdiff_plain;f=folly%2Fsorted_vector_types.h;h=b9354b10cbb9bdd7e4ae49e7f4b1336c9dfa8b00;hb=7f813725fa5aacead8c001e15a3b4addbd988899;hp=6d29a101fd9d4e9e68465e130c32beb4652a2b13;hpb=22afce906d7e98d95f8c45c3301072d9fd891d41;p=folly.git diff --git a/folly/sorted_vector_types.h b/folly/sorted_vector_types.h index 6d29a101..b9354b10 100644 --- a/folly/sorted_vector_types.h +++ b/folly/sorted_vector_types.h @@ -1,5 +1,5 @@ /* - * Copyright 2014 Facebook, Inc. + * Copyright 2016 Facebook, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -57,8 +57,7 @@ * std::vector<>, which requires it to be Assignable.) */ -#ifndef FOLLY_SORTED_VECTOR_TYPES_H_ -#define FOLLY_SORTED_VECTOR_TYPES_H_ +#pragma once #include #include @@ -112,36 +111,36 @@ namespace detail { } template - std::pair + typename OurContainer::iterator insert_with_hint(OurContainer& sorted, Vector& cont, typename OurContainer::iterator hint, - typename OurContainer::value_type value, + typename OurContainer::value_type&& value, GrowthPolicy& po) { const typename OurContainer::value_compare& cmp(sorted.value_comp()); if (hint == cont.end() || cmp(value, *hint)) { if (hint == cont.begin()) { po.increase_capacity(cont, cont.begin()); - return std::make_pair(cont.insert(cont.begin(), value), true); + return cont.insert(cont.begin(), std::move(value)); } if (cmp(*(hint - 1), value)) { hint = po.increase_capacity(cont, hint); - return std::make_pair(cont.insert(hint, value), true); + return cont.insert(hint, std::move(value)); } - return sorted.insert(value); + return sorted.insert(std::move(value)).first; } if (cmp(*hint, value)) { if (hint + 1 == cont.end() || cmp(value, *(hint + 1))) { typename OurContainer::iterator it = po.increase_capacity(cont, hint + 1); - return std::make_pair(cont.insert(it, value), true); + return cont.insert(it, std::move(value)); } } // Value and *hint did not compare, so they are equal keys. - return std::make_pair(hint, false); + return hint; } } @@ -241,19 +240,28 @@ public: size_type max_size() const { return m_.cont_.max_size(); } bool empty() const { return m_.cont_.empty(); } void reserve(size_type s) { return m_.cont_.reserve(s); } + void shrink_to_fit() { m_.cont_.shrink_to_fit(); } size_type capacity() const { return m_.cont_.capacity(); } std::pair insert(const value_type& value) { + return insert(value_type(value)); + } + + std::pair insert(value_type&& value) { iterator it = lower_bound(value); if (it == end() || value_comp()(value, *it)) { it = get_growth_policy().increase_capacity(m_.cont_, it); - return std::make_pair(m_.cont_.insert(it, value), true); + return std::make_pair(m_.cont_.insert(it, std::move(value)), true); } return std::make_pair(it, false); } - std::pair insert(iterator hint, const value_type& value) { - return detail::insert_with_hint(*this, m_.cont_, hint, value, + iterator insert(iterator hint, const value_type& value) { + return insert(hint, value_type(value)); + } + + iterator insert(iterator hint, value_type&& value) { + return detail::insert_with_hint(*this, m_.cont_, hint, std::move(value), get_growth_policy()); } @@ -476,19 +484,28 @@ public: size_type max_size() const { return m_.cont_.max_size(); } bool empty() const { return m_.cont_.empty(); } void reserve(size_type s) { return m_.cont_.reserve(s); } + void shrink_to_fit() { m_.cont_.shrink_to_fit(); } size_type capacity() const { return m_.cont_.capacity(); } std::pair insert(const value_type& value) { + return insert(value_type(value)); + } + + std::pair insert(value_type&& value) { iterator it = lower_bound(value.first); if (it == end() || value_comp()(value, *it)) { it = get_growth_policy().increase_capacity(m_.cont_, it); - return std::make_pair(m_.cont_.insert(it, value), true); + return std::make_pair(m_.cont_.insert(it, std::move(value)), true); } return std::make_pair(it, false); } - std::pair insert(iterator hint, const value_type& value) { - return detail::insert_with_hint(*this, m_.cont_, hint, value, + iterator insert(iterator hint, const value_type& value) { + return insert(hint, value_type(value)); + } + + iterator insert(iterator hint, value_type&& value) { + return detail::insert_with_hint(*this, m_.cont_, hint, std::move(value), get_growth_policy()); } @@ -534,6 +551,22 @@ public: return end(); } + mapped_type& at(const key_type& key) { + iterator it = find(key); + if (it != end()) { + return it->second; + } + throw std::out_of_range("sorted_vector_map::at"); + } + + const mapped_type& at(const key_type& key) const { + const_iterator it = find(key); + if (it != end()) { + return it->second; + } + throw std::out_of_range("sorted_vector_map::at"); + } + size_type count(const key_type& key) const { return find(key) == end() ? 0 : 1; } @@ -585,7 +618,7 @@ public: mapped_type& operator[](const key_type& key) { iterator it = lower_bound(key); if (it == end() || key_comp()(key, it->first)) { - return insert(it, value_type(key, mapped_type())).first->second; + return insert(it, value_type(key, mapped_type()))->second; } return it->second; } @@ -620,6 +653,3 @@ inline void swap(sorted_vector_map& a, ////////////////////////////////////////////////////////////////////// } - -#endif -