13c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#ifndef _DESTLUTIL_HPP 23c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#define _DESTLUTIL_HPP 33c827367444ee418f129b2c238299f49d3264554Jarkko Poyry/*------------------------------------------------------------------------- 43c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * drawElements C++ Base Library 53c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * ----------------------------- 63c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * 73c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * Copyright 2014 The Android Open Source Project 83c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * 93c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * Licensed under the Apache License, Version 2.0 (the "License"); 103c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * you may not use this file except in compliance with the License. 113c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * You may obtain a copy of the License at 123c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * 133c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * http://www.apache.org/licenses/LICENSE-2.0 143c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * 153c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * Unless required by applicable law or agreed to in writing, software 163c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * distributed under the License is distributed on an "AS IS" BASIS, 173c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 183c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * See the License for the specific language governing permissions and 193c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * limitations under the License. 203c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * 213c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *//*! 223c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * \file 233c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * \brief Utilities for STL containers. 243c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *//*--------------------------------------------------------------------*/ 253c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 263c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "deDefs.hpp" 273c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 283c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include <algorithm> 293c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include <stdexcept> 303c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include <utility> 313c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include <iterator> 323c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 333c827367444ee418f129b2c238299f49d3264554Jarkko Poyrynamespace de 343c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{ 353c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 363c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid STLUtil_selfTest (void); 373c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 383c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//! Test whether `item` is a member of `container`. The type `C` must be an 393c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//! AssociativeContainer. 403c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 413c827367444ee418f129b2c238299f49d3264554Jarkko Poyrytemplate<typename C> 423c827367444ee418f129b2c238299f49d3264554Jarkko Poyryinline bool contains (const C& container, const typename C::key_type& item) 433c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{ 443c827367444ee418f129b2c238299f49d3264554Jarkko Poyry const typename C::const_iterator it = container.find(item); 453c827367444ee418f129b2c238299f49d3264554Jarkko Poyry return (it != container.end()); 463c827367444ee418f129b2c238299f49d3264554Jarkko Poyry} 473c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 483c827367444ee418f129b2c238299f49d3264554Jarkko Poyrytemplate<typename I, typename K> 493c827367444ee418f129b2c238299f49d3264554Jarkko Poyryinline bool contains (const I& begin, const I& end, const K& item) 503c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{ 513c827367444ee418f129b2c238299f49d3264554Jarkko Poyry const I it = std::find(begin, end, item); 523c827367444ee418f129b2c238299f49d3264554Jarkko Poyry return (it != end); 533c827367444ee418f129b2c238299f49d3264554Jarkko Poyry} 543c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 553c827367444ee418f129b2c238299f49d3264554Jarkko Poyrytemplate<typename C> 563c827367444ee418f129b2c238299f49d3264554Jarkko PoyryC intersection (const C& s1, const C& s2) 573c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{ 583c827367444ee418f129b2c238299f49d3264554Jarkko Poyry C ret; 593c827367444ee418f129b2c238299f49d3264554Jarkko Poyry std::set_intersection(s1.begin(), s1.end(), s2.begin(), s2.end(), 603c827367444ee418f129b2c238299f49d3264554Jarkko Poyry std::insert_iterator<C>(ret, ret.begin())); 613c827367444ee418f129b2c238299f49d3264554Jarkko Poyry return ret; 623c827367444ee418f129b2c238299f49d3264554Jarkko Poyry} 633c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 643c827367444ee418f129b2c238299f49d3264554Jarkko Poyrytemplate<typename C> 653c827367444ee418f129b2c238299f49d3264554Jarkko PoyryC set_union (const C& s1, const C& s2) 663c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{ 673c827367444ee418f129b2c238299f49d3264554Jarkko Poyry C ret; 683c827367444ee418f129b2c238299f49d3264554Jarkko Poyry std::set_union(s1.begin(), s1.end(), s2.begin(), s2.end(), 693c827367444ee418f129b2c238299f49d3264554Jarkko Poyry std::insert_iterator<C>(ret, ret.begin())); 703c827367444ee418f129b2c238299f49d3264554Jarkko Poyry return ret; 713c827367444ee418f129b2c238299f49d3264554Jarkko Poyry} 723c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 733c827367444ee418f129b2c238299f49d3264554Jarkko Poyry// Utilities for map-like container types 743c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 753c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//! Return a pointer to the value mapped to `key`, or null if not found. 763c827367444ee418f129b2c238299f49d3264554Jarkko Poyrytemplate <typename M> 773c827367444ee418f129b2c238299f49d3264554Jarkko Poyryconst typename M::mapped_type* tryLookup (const M& map, 783c827367444ee418f129b2c238299f49d3264554Jarkko Poyry const typename M::key_type& key) 793c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{ 803c827367444ee418f129b2c238299f49d3264554Jarkko Poyry typename M::const_iterator it = map.find(key); 813c827367444ee418f129b2c238299f49d3264554Jarkko Poyry if (it == map.end()) 823c827367444ee418f129b2c238299f49d3264554Jarkko Poyry return DE_NULL; 833c827367444ee418f129b2c238299f49d3264554Jarkko Poyry return &it->second; 843c827367444ee418f129b2c238299f49d3264554Jarkko Poyry} 853c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 863c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//! Return a reference to the value mapped to `key`, or `fallback` if not found. 873c827367444ee418f129b2c238299f49d3264554Jarkko Poyrytemplate<typename M> 883c827367444ee418f129b2c238299f49d3264554Jarkko Poyryconst typename M::mapped_type& lookupDefault (const M& map, 893c827367444ee418f129b2c238299f49d3264554Jarkko Poyry const typename M::key_type& key, 903c827367444ee418f129b2c238299f49d3264554Jarkko Poyry const typename M::mapped_type& fallback) 913c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{ 923c827367444ee418f129b2c238299f49d3264554Jarkko Poyry const typename M::mapped_type* ptr = tryLookup(map, key); 933c827367444ee418f129b2c238299f49d3264554Jarkko Poyry return ptr == DE_NULL ? fallback : *ptr; 943c827367444ee418f129b2c238299f49d3264554Jarkko Poyry} 953c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 963c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//! Return a reference to the value mapped to `key`, or raise 973c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//! `std::out_of_range` if not found. 983c827367444ee418f129b2c238299f49d3264554Jarkko Poyrytemplate<typename M> 993c827367444ee418f129b2c238299f49d3264554Jarkko Poyryconst typename M::mapped_type& lookup (const M& map, const typename M::key_type& key) 1003c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{ 1013c827367444ee418f129b2c238299f49d3264554Jarkko Poyry const typename M::mapped_type* ptr = tryLookup(map, key); 1023c827367444ee418f129b2c238299f49d3264554Jarkko Poyry if (ptr == DE_NULL) 1033c827367444ee418f129b2c238299f49d3264554Jarkko Poyry throw std::out_of_range("key not found in map"); 1043c827367444ee418f129b2c238299f49d3264554Jarkko Poyry return *ptr; 1053c827367444ee418f129b2c238299f49d3264554Jarkko Poyry} 1063c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 1073c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//! Map `key` to `value`. This differs from `map[key] = value` in that there 1083c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//! is no default construction and assignment involved. 1093c827367444ee418f129b2c238299f49d3264554Jarkko Poyrytemplate<typename M> 1103c827367444ee418f129b2c238299f49d3264554Jarkko Poyrybool insert (M& map, const typename M::key_type& key, const typename M::mapped_type& value) 1113c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{ 1123c827367444ee418f129b2c238299f49d3264554Jarkko Poyry typename M::value_type entry(key, value); 1133c827367444ee418f129b2c238299f49d3264554Jarkko Poyry std::pair<typename M::iterator,bool> ret = map.insert(entry); 1143c827367444ee418f129b2c238299f49d3264554Jarkko Poyry return ret.second; 1153c827367444ee418f129b2c238299f49d3264554Jarkko Poyry} 1163c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 1173c827367444ee418f129b2c238299f49d3264554Jarkko Poyry} // de 1183c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 1193c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#endif // _DESTLUTIL_HPP 120