• 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 #include <limits>
34 
35 namespace de
36 {
37 
38 void STLUtil_selfTest (void);
39 
40 //! Test whether `item` is a member of `container`. The type `C` must be an
41 //! AssociativeContainer.
42 
43 template<typename C>
contains(const C & container,const typename C::key_type & item)44 inline bool contains (const C& container, const typename C::key_type& item)
45 {
46 	const typename C::const_iterator it = container.find(item);
47 	return (it != container.end());
48 }
49 
50 template<typename I, typename K>
contains(const I & begin,const I & end,const K & item)51 inline bool contains (const I& begin, const I& end, const K& item)
52 {
53 	const I it = std::find(begin, end, item);
54 	return (it != end);
55 }
56 
57 template<typename C>
intersection(const C & s1,const C & s2)58 C intersection (const C& s1, const C& s2)
59 {
60 	C ret;
61 	std::set_intersection(s1.begin(), s1.end(), s2.begin(), s2.end(),
62 						  std::insert_iterator<C>(ret, ret.begin()));
63 	return ret;
64 }
65 
66 template<typename C>
set_union(const C & s1,const C & s2)67 C set_union (const C& s1, const C& s2)
68 {
69 	C ret;
70 	std::set_union(s1.begin(), s1.end(), s2.begin(), s2.end(),
71 				   std::insert_iterator<C>(ret, ret.begin()));
72 	return ret;
73 }
74 
75 // Utilities for map-like container types
76 
77 //! Return a pointer to the value mapped to `key`, or null if not found.
78 template <typename M>
tryLookup(const M & map,const typename M::key_type & key)79 const typename M::mapped_type* tryLookup (const M& map,
80 										  const typename M::key_type& key)
81 {
82 	typename M::const_iterator it = map.find(key);
83 	if (it == map.end())
84 		return DE_NULL;
85 	return &it->second;
86 }
87 
88 //! Return a reference to the value mapped to `key`, or `fallback` if not found.
89 template<typename M>
lookupDefault(const M & map,const typename M::key_type & key,const typename M::mapped_type & fallback)90 const typename M::mapped_type& lookupDefault (const M&							map,
91 											  const typename M::key_type&		key,
92 											  const typename M::mapped_type&	fallback)
93 {
94 	const typename M::mapped_type* ptr = tryLookup(map, key);
95 	return ptr == DE_NULL ? fallback : *ptr;
96 }
97 
98 //! Return a reference to the value mapped to `key`, or raise
99 //! `std::out_of_range` if not found.
100 template<typename M>
lookup(const M & map,const typename M::key_type & key)101 const typename M::mapped_type& lookup (const M& map, const typename M::key_type& key)
102 {
103 	const typename M::mapped_type* ptr = tryLookup(map, key);
104 	if (ptr == DE_NULL)
105 		throw std::out_of_range("key not found in map");
106 	return *ptr;
107 }
108 
109 //! Map `key` to `value`. This differs from `map[key] = value` in that there
110 //! is no default construction and assignment involved.
111 template<typename M>
insert(M & map,const typename M::key_type & key,const typename M::mapped_type & value)112 bool insert (M& map, const typename M::key_type& key, const typename M::mapped_type& value)
113 {
114 	typename M::value_type entry(key, value);
115 	std::pair<typename M::iterator,bool> ret = map.insert(entry);
116 	return ret.second;
117 }
118 
119 // Returns the total size in bytes for contiguous-storage containers.
120 template <typename T>
dataSize(const T & container)121 size_t dataSize (const T& container)
122 {
123 	return (container.size() * sizeof(typename T::value_type));
124 }
125 
126 // Returns the data pointer or a null pointer if the vector is empty.
127 template <typename T>
dataOrNull(std::vector<T> & container)128 T* dataOrNull (std::vector<T>& container)
129 {
130 	return (container.empty() ? nullptr : container.data());
131 }
132 
133 // Returns the data pointer or a null pointer if the vector is empty.
134 template <typename T>
dataOrNull(const std::vector<T> & container)135 const T* dataOrNull (const std::vector<T>& container)
136 {
137 	return (container.empty() ? nullptr : container.data());
138 }
139 
140 // Returns the container size() as an uint32_t value.
141 template <typename T>
sizeU32(const T & container)142 uint32_t sizeU32 (const T& container)
143 {
144 	const size_t sz = container.size();
145 	DE_ASSERT(sz <= static_cast<size_t>(std::numeric_limits<uint32_t>::max()));
146 	return static_cast<uint32_t>(sz);
147 }
148 
149 } // de
150 
151 #endif // _DESTLUTIL_HPP
152