xref: /aosp_15_r20/external/deqp/framework/delibs/decpp/deSTLUtil.hpp (revision 35238bce31c2a825756842865a792f8cf7f89930)
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(), 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(), std::insert_iterator<C>(ret, ret.begin()));
70     return ret;
71 }
72 
73 // Utilities for map-like container types
74 
75 //! Return a pointer to the value mapped to `key`, or null if not found.
76 template <typename M>
tryLookup(const M & map,const typename M::key_type & key)77 const typename M::mapped_type *tryLookup(const M &map, const typename M::key_type &key)
78 {
79     typename M::const_iterator it = map.find(key);
80     if (it == map.end())
81         return DE_NULL;
82     return &it->second;
83 }
84 
85 //! Return a reference to the value mapped to `key`, or `fallback` if not found.
86 template <typename M>
lookupDefault(const M & map,const typename M::key_type & key,const typename M::mapped_type & fallback)87 const typename M::mapped_type &lookupDefault(const M &map, const typename M::key_type &key,
88                                              const typename M::mapped_type &fallback)
89 {
90     const typename M::mapped_type *ptr = tryLookup(map, key);
91     return ptr == DE_NULL ? fallback : *ptr;
92 }
93 
94 //! Return a reference to the value mapped to `key`, or raise
95 //! `std::out_of_range` if not found.
96 template <typename M>
lookup(const M & map,const typename M::key_type & key)97 const typename M::mapped_type &lookup(const M &map, const typename M::key_type &key)
98 {
99     const typename M::mapped_type *ptr = tryLookup(map, key);
100     if (ptr == DE_NULL)
101         throw std::out_of_range("key not found in map");
102     return *ptr;
103 }
104 
105 //! Map `key` to `value`. This differs from `map[key] = value` in that there
106 //! is no default construction and assignment involved.
107 template <typename M>
insert(M & map,const typename M::key_type & key,const typename M::mapped_type & value)108 bool insert(M &map, const typename M::key_type &key, const typename M::mapped_type &value)
109 {
110     typename M::value_type entry(key, value);
111     std::pair<typename M::iterator, bool> ret = map.insert(entry);
112     return ret.second;
113 }
114 
115 // Returns the total size in bytes for contiguous-storage containers.
116 template <typename T>
dataSize(const T & container)117 size_t dataSize(const T &container)
118 {
119     return (container.size() * sizeof(typename T::value_type));
120 }
121 
122 // Returns the data pointer or a null pointer if the vector is empty.
123 template <typename T>
dataOrNull(std::vector<T> & container)124 T *dataOrNull(std::vector<T> &container)
125 {
126     return (container.empty() ? nullptr : container.data());
127 }
128 
129 // Returns the data pointer or a null pointer if the vector is empty.
130 template <typename T>
dataOrNull(const std::vector<T> & container)131 const T *dataOrNull(const std::vector<T> &container)
132 {
133     return (container.empty() ? nullptr : container.data());
134 }
135 
136 // Returns the container size() as an uint32_t value.
137 template <typename T>
sizeU32(const T & container)138 uint32_t sizeU32(const T &container)
139 {
140     const size_t sz = container.size();
141     DE_ASSERT(sz <= static_cast<size_t>(std::numeric_limits<uint32_t>::max()));
142     return static_cast<uint32_t>(sz);
143 }
144 
145 } // namespace de
146 
147 #endif // _DESTLUTIL_HPP
148