LogCabin
Core/STLUtil.h
Go to the documentation of this file.
00001 /* Copyright (c) 2011-2012 Stanford University
00002  *
00003  * Permission to use, copy, modify, and distribute this software for any
00004  * purpose with or without fee is hereby granted, provided that the above
00005  * copyright notice and this permission notice appear in all copies.
00006  *
00007  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR(S) DISCLAIM ALL WARRANTIES
00008  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
00009  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL AUTHORS BE LIABLE FOR
00010  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
00011  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
00012  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
00013  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
00014  */
00015 
00016 #include <algorithm>
00017 #include <vector>
00018 
00019 #ifndef LOGCABIN_CORE_STLUTIL_H
00020 #define LOGCABIN_CORE_STLUTIL_H
00021 
00022 namespace LogCabin {
00023 namespace Core {
00024 namespace STLUtil {
00025 
00026 /**
00027  * Sort an R-value in place.
00028  * \param container
00029  *      An R-value to sort.
00030  * \return
00031  *      The sorted input.
00032  */
00033 template<typename Container>
00034 Container
00035 sorted(Container container)
00036 {
00037     std::sort(container.begin(), container.end());
00038     return container;
00039 }
00040 
00041 /**
00042  * Return a copy of the keys of a map.
00043  */
00044 template<typename Map>
00045 std::vector<typename Map::key_type>
00046 getKeys(const Map& map)
00047 {
00048     std::vector<typename Map::key_type> keys;
00049     for (auto it = map.begin(); it != map.end(); ++it)
00050         keys.push_back(it->first);
00051     return keys;
00052 }
00053 
00054 /**
00055  * Return a copy of the values of a map.
00056  */
00057 template<typename Map>
00058 std::vector<typename Map::mapped_type>
00059 getValues(const Map& map)
00060 {
00061     std::vector<typename Map::mapped_type> values;
00062     for (auto it = map.begin(); it != map.end(); ++it)
00063         values.push_back(it->second);
00064     return values;
00065 }
00066 
00067 /**
00068  * Return a copy of the key-value pairs of a map.
00069  */
00070 template<typename Map>
00071 std::vector<std::pair<typename Map::key_type,
00072                       typename Map::mapped_type>>
00073 getItems(const Map& map)
00074 {
00075     std::vector<std::pair<typename Map::key_type,
00076                           typename Map::mapped_type>> items;
00077     for (auto it = map.begin(); it != map.end(); ++it)
00078         items.push_back(*it);
00079     return items;
00080 }
00081 
00082 } // namespace LogCabin::Core::STLUtil
00083 } // namespace LogCabin::Core
00084 } // namespace LogCabin
00085 
00086 #endif /* LOGCABIN_CORE_STLUTIL_H */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines