• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #ifndef _DESTLUTIL_HPP
2 #define _DESTLUTIL_HPP
3 /*-------------------------------------------------------------------------
4  * drawElements C++ Base Library
5  * -----------------------------
6  *
7  * Copyright 2014 The Android Open Source Project
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  *//*!
22  * \file
23  * \brief Utilities for STL containers.
24  *//*--------------------------------------------------------------------*/
25 
26 #include "deDefs.hpp"
27 
28 #include <algorithm>
29 #include <stdexcept>
30 #include <utility>
31 #include <iterator>
32 #include <vector>
33 
34 namespace de
35 {
36 
37 void STLUtil_selfTest (void);
38 
39 //! Test whether `item` is a member of `container`. The type `C` must be an
40 //! AssociativeContainer.
41 
42 template<typename C>
contains(const C & container,const typename C::key_type & item)43 inline bool contains (const C& container, const typename C::key_type& item)
44 {
45 	const typename C::const_iterator it = container.find(item);
46 	return (it != container.end());
47 }
48 
49 template<typename I, typename K>
contains(const I & begin,const I & end,const K & item)50 inline bool contains (const I& begin, const I& end, const K& item)
51 {
52 	const I it = std::find(begin, end, item);
53 	return (it != end);
54 }
55 
56 template<typename C>
intersection(const C & s1,const C & s2)57 C intersection (const C& s1, const C& s2)
58 {
59 	C ret;
60 	std::set_intersection(s1.begin(), s1.end(), s2.begin(), s2.end(),
61 						  std::insert_iterator<C>(ret, ret.begin()));
62 	return ret;
63 }
64 
65 template<typename C>
set_union(const C & s1,const C & s2)66 C set_union (const C& s1, const C& s2)
67 {
68 	C ret;
69 	std::set_union(s1.begin(), s1.end(), s2.begin(), s2.end(),
70 				   std::insert_iterator<C>(ret, ret.begin()));
71 	return ret;
72 }
73 
74 // Utilities for map-like container types
75 
76 //! Return a pointer to the value mapped to `key`, or null if not found.
77 template <typename M>
tryLookup(const M & map,const typename M::key_type & key)78 const typename M::mapped_type* tryLookup (const M& map,
79 										  const typename M::key_type& key)
80 {
81 	typename M::const_iterator it = map.find(key);
82 	if (it == map.end())
83 		return DE_NULL;
84 	return &it->second;
85 }
86 
87 //! Return a reference to the value mapped to `key`, or `fallback` if not found.
88 template<typename M>
lookupDefault(const M & map,const typename M::key_type & key,const typename M::mapped_type & fallback)89 const typename M::mapped_type& lookupDefault (const M&							map,
90 											  const typename M::key_type&		key,
91 											  const typename M::mapped_type&	fallback)
92 {
93 	const typename M::mapped_type* ptr = tryLookup(map, key);
94 	return ptr == DE_NULL ? fallback : *ptr;
95 }
96 
97 //! Return a reference to the value mapped to `key`, or raise
98 //! `std::out_of_range` if not found.
99 template<typename M>
lookup(const M & map,const typename M::key_type & key)100 const typename M::mapped_type& lookup (const M& map, const typename M::key_type& key)
101 {
102 	const typename M::mapped_type* ptr = tryLookup(map, key);
103 	if (ptr == DE_NULL)
104 		throw std::out_of_range("key not found in map");
105 	return *ptr;
106 }
107 
108 //! Map `key` to `value`. This differs from `map[key] = value` in that there
109 //! is no default construction and assignment involved.
110 template<typename M>
insert(M & map,const typename M::key_type & key,const typename M::mapped_type & value)111 bool insert (M& map, const typename M::key_type& key, const typename M::mapped_type& value)
112 {
113 	typename M::value_type entry(key, value);
114 	std::pair<typename M::iterator,bool> ret = map.insert(entry);
115 	return ret.second;
116 }
117 
118 // Returns the total size in bytes for contiguous-storage containers.
119 template <typename T>
dataSize(const T & container)120 size_t dataSize (const T& container)
121 {
122 	return (container.size() * sizeof(typename T::value_type));
123 }
124 
125 // Returns const pointer to data stored in vector or NULL if vector is empty
126 template <typename T>
dataSafe(const std::vector<T> & container)127 const T* dataSafe (const std::vector<T>& container)
128 {
129 	return container.empty() ? static_cast<const T*>(DE_NULL) : container.data();
130 }
131 
132 // Returns pointer to data stored in vector or NULL if vector is empty
133 template <typename T>
dataSafe(std::vector<T> & container)134 T* dataSafe (std::vector<T>& container)
135 {
136 	return container.empty() ? static_cast<T*>(DE_NULL) : container.data();
137 }
138 
139 // Returns the data pointer or a null pointer if the vector is empty.
140 template <typename T>
dataOrNull(std::vector<T> & container)141 T* dataOrNull (std::vector<T>& container)
142 {
143 	return (container.empty() ? nullptr : container.data());
144 }
145 
146 // Returns the data pointer or a null pointer if the vector is empty.
147 template <typename T>
dataOrNull(const std::vector<T> & container)148 const T* dataOrNull (const std::vector<T>& container)
149 {
150 	return (container.empty() ? nullptr : container.data());
151 }
152 
153 } // de
154 
155 #endif // _DESTLUTIL_HPP
156