rewrite sort/unique function to be more generic
This commit is contained in:
		
							parent
							
								
									fcf9f266ad
								
							
						
					
					
						commit
						25c52b7482
					
				| @ -1,6 +1,6 @@ | ||||
| /*
 | ||||
| 
 | ||||
| Copyright (c) 2013, Project OSRM, Dennis Luxen, others | ||||
| Copyright (c) 2015, Project OSRM, Dennis Luxen, others | ||||
| All rights reserved. | ||||
| 
 | ||||
| Redistribution and use in source and binary forms, with or without modification, | ||||
| @ -25,8 +25,8 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||||
| 
 | ||||
| */ | ||||
| 
 | ||||
| #ifndef CONTAINER_HPP_ | ||||
| #define CONTAINER_HPP_ | ||||
| #ifndef CONTAINER_HPP | ||||
| #define CONTAINER_HPP | ||||
| 
 | ||||
| #include <algorithm> | ||||
| #include <iterator> | ||||
| @ -34,12 +34,27 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||||
| 
 | ||||
| namespace osrm | ||||
| { | ||||
| template <typename T> void sort_unique_resize(std::vector<T> &vector) | ||||
| namespace detail | ||||
| { | ||||
|     std::sort(vector.begin(), vector.end()); | ||||
| // Culled by SFINAE if reserve does not exist or is not accessible
 | ||||
| template <typename T> constexpr auto has_resize_method(T &t) -> decltype(t.resize(0), bool()) | ||||
| { | ||||
|     return true; | ||||
| } | ||||
| 
 | ||||
| // Used as fallback when SFINAE culls the template method
 | ||||
| constexpr bool has_resize_method(...) { return false; } | ||||
| } | ||||
| 
 | ||||
| template <typename Container> void sort_unique_resize(Container &vector) | ||||
| { | ||||
|     std::sort(std::begin(vector), std::end(vector)); | ||||
|     const auto number_of_unique_elements = | ||||
|         std::unique(vector.begin(), vector.end()) - vector.begin(); | ||||
|     vector.resize(number_of_unique_elements); | ||||
|         std::unique(std::begin(vector), std::end(vector)) - std::begin(vector); | ||||
|     if (detail::has_resize_method(vector)) | ||||
|     { | ||||
|         vector.resize(number_of_unique_elements); | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| // template <typename T> inline void sort_unique_resize_shrink_vector(std::vector<T> &vector)
 | ||||
| @ -82,4 +97,4 @@ Function for_each_pair(ContainerT &container, Function function) | ||||
|     return for_each_pair(std::begin(container), std::end(container), function); | ||||
| } | ||||
| } | ||||
| #endif /* CONTAINER_HPP_ */ | ||||
| #endif /* CONTAINER_HPP */ | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user