3 /*-------------------------------------------------------------------------
4 * drawElements C++ Base Library
5 * -----------------------------
7 * Copyright 2014 The Android Open Source Project
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
23 * \brief Utilities for STL containers.
24 *//*--------------------------------------------------------------------*/
38 void STLUtil_selfTest (void);
40 //! Test whether `item` is a member of `container`. The type `C` must be an
41 //! AssociativeContainer.
44 inline bool contains (const C& container, const typename C::key_type& item)
46 const typename C::const_iterator it = container.find(item);
47 return (it != container.end());
50 template<typename I, typename K>
51 inline bool contains (const I& begin, const I& end, const K& item)
53 const I it = std::find(begin, end, item);
58 C intersection (const C& s1, const C& s2)
61 std::set_intersection(s1.begin(), s1.end(), s2.begin(), s2.end(),
62 std::insert_iterator<C>(ret, ret.begin()));
67 C set_union (const C& s1, const C& s2)
70 std::set_union(s1.begin(), s1.end(), s2.begin(), s2.end(),
71 std::insert_iterator<C>(ret, ret.begin()));
75 // Utilities for map-like container types
77 //! Return a pointer to the value mapped to `key`, or null if not found.
79 const typename M::mapped_type* tryLookup (const M& map,
80 const typename M::key_type& key)
82 typename M::const_iterator it = map.find(key);
88 //! Return a reference to the value mapped to `key`, or `fallback` if not found.
90 const typename M::mapped_type& lookupDefault (const M& map,
91 const typename M::key_type& key,
92 const typename M::mapped_type& fallback)
94 const typename M::mapped_type* ptr = tryLookup(map, key);
95 return ptr == DE_NULL ? fallback : *ptr;
98 //! Return a reference to the value mapped to `key`, or raise
99 //! `std::out_of_range` if not found.
101 const typename M::mapped_type& lookup (const M& map, const typename M::key_type& key)
103 const typename M::mapped_type* ptr = tryLookup(map, key);
105 throw std::out_of_range("key not found in map");
109 //! Map `key` to `value`. This differs from `map[key] = value` in that there
110 //! is no default construction and assignment involved.
112 bool insert (M& map, const typename M::key_type& key, const typename M::mapped_type& value)
114 typename M::value_type entry(key, value);
115 std::pair<typename M::iterator,bool> ret = map.insert(entry);
119 // Returns the total size in bytes for contiguous-storage containers.
120 template <typename T>
121 size_t dataSize (const T& container)
123 return (container.size() * sizeof(typename T::value_type));
126 // Returns the data pointer or a null pointer if the vector is empty.
127 template <typename T>
128 T* dataOrNull (std::vector<T>& container)
130 return (container.empty() ? nullptr : container.data());
133 // Returns the data pointer or a null pointer if the vector is empty.
134 template <typename T>
135 const T* dataOrNull (const std::vector<T>& container)
137 return (container.empty() ? nullptr : container.data());
140 // Returns the container size() as an uint32_t value.
141 template <typename T>
142 uint32_t sizeU32 (const T& container)
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);
151 #endif // _DESTLUTIL_HPP