Path: blob/21.2-virgl/src/gallium/frontends/clover/util/functional.hpp
4572 views
//1// Copyright 2013 Francisco Jerez2//3// Permission is hereby granted, free of charge, to any person obtaining a4// copy of this software and associated documentation files (the "Software"),5// to deal in the Software without restriction, including without limitation6// the rights to use, copy, modify, merge, publish, distribute, sublicense,7// and/or sell copies of the Software, and to permit persons to whom the8// Software is furnished to do so, subject to the following conditions:9//10// The above copyright notice and this permission notice shall be included in11// all copies or substantial portions of the Software.12//13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL16// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR17// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,18// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR19// OTHER DEALINGS IN THE SOFTWARE.20//2122#ifndef CLOVER_UTIL_FUNCTIONAL_HPP23#define CLOVER_UTIL_FUNCTIONAL_HPP2425#include <type_traits>2627namespace clover {28struct identity {29template<typename T>30typename std::remove_reference<T>::type31operator()(T &&x) const {32return x;33}34};3536struct plus {37template<typename T, typename S>38typename std::common_type<T, S>::type39operator()(T x, S y) const {40return x + y;41}42};4344struct minus {45template<typename T, typename S>46typename std::common_type<T, S>::type47operator()(T x, S y) const {48return x - y;49}50};5152struct negate {53template<typename T>54T55operator()(T x) const {56return -x;57}58};5960struct multiplies {61template<typename T, typename S>62typename std::common_type<T, S>::type63operator()(T x, S y) const {64return x * y;65}66};6768struct divides {69template<typename T, typename S>70typename std::common_type<T, S>::type71operator()(T x, S y) const {72return x / y;73}74};7576struct modulus {77template<typename T, typename S>78typename std::common_type<T, S>::type79operator()(T x, S y) const {80return x % y;81}82};8384struct minimum {85template<typename T>86T87operator()(T x) const {88return x;89}9091template<typename T, typename... Ts>92T93operator()(T x, Ts... xs) const {94T y = minimum()(xs...);95return x < y ? x : y;96}97};9899struct maximum {100template<typename T>101T102operator()(T x) const {103return x;104}105106template<typename T, typename... Ts>107T108operator()(T x, Ts... xs) const {109T y = maximum()(xs...);110return x < y ? y : x;111}112};113114struct preincs {115template<typename T>116T &117operator()(T &x) const {118return ++x;119}120};121122struct predecs {123template<typename T>124T &125operator()(T &x) const {126return --x;127}128};129130template<typename T>131class multiplies_by_t {132public:133multiplies_by_t(T x) : x(x) {134}135136template<typename S>137typename std::common_type<T, S>::type138operator()(S y) const {139return x * y;140}141142private:143T x;144};145146template<typename T>147multiplies_by_t<T>148multiplies_by(T x) {149return { x };150}151152template<typename T>153class preincs_by_t {154public:155preincs_by_t(T n) : n(n) {156}157158template<typename S>159S &160operator()(S &x) const {161return x += n;162}163164private:165T n;166};167168template<typename T>169preincs_by_t<T>170preincs_by(T n) {171return { n };172}173174template<typename T>175class predecs_by_t {176public:177predecs_by_t(T n) : n(n) {178}179180template<typename S>181S &182operator()(S &x) const {183return x -= n;184}185186private:187T n;188};189190template<typename T>191predecs_by_t<T>192predecs_by(T n) {193return { n };194}195196struct greater {197template<typename T, typename S>198bool199operator()(T x, S y) const {200return x > y;201}202};203204struct evals {205template<typename T>206auto207operator()(T &&x) const -> decltype(x()) {208return x();209}210};211212struct derefs {213template<typename T>214auto215operator()(T &&x) const -> decltype(*x) {216return *x;217}218};219220struct addresses {221template<typename T>222T *223operator()(T &x) const {224return &x;225}226227template<typename T>228T *229operator()(std::reference_wrapper<T> x) const {230return &x.get();231}232};233234struct begins {235template<typename T>236auto237operator()(T &x) const -> decltype(x.begin()) {238return x.begin();239}240};241242struct ends {243template<typename T>244auto245operator()(T &x) const -> decltype(x.end()) {246return x.end();247}248};249250struct sizes {251template<typename T>252auto253operator()(T &x) const -> decltype(x.size()) {254return x.size();255}256};257258template<typename T>259class advances_by_t {260public:261advances_by_t(T n) : n(n) {262}263264template<typename S>265S266operator()(S &&it) const {267std::advance(it, n);268return std::forward<S>(it);269}270271private:272T n;273};274275template<typename T>276advances_by_t<T>277advances_by(T n) {278return { n };279}280281struct zips {282template<typename... Ts>283std::tuple<Ts...>284operator()(Ts &&... xs) const {285return std::tuple<Ts...>(std::forward<Ts>(xs)...);286}287};288289struct is_zero {290template<typename T>291bool292operator()(const T &x) const {293return x == 0;294}295};296297struct keys {298template<typename P>299auto300operator()(P &&p) const -> decltype(std::get<0>(std::forward<P>(p))) {301return std::get<0>(std::forward<P>(p));302}303};304305struct values {306template<typename P>307auto308operator()(P &&p) const -> decltype(std::get<1>(std::forward<P>(p))) {309return std::get<1>(std::forward<P>(p));310}311};312313template<typename T>314class equals_t {315public:316equals_t(T &&x) : x(x) {}317318template<typename S>319bool320operator()(S &&y) const {321return x == y;322}323324private:325T x;326};327328template<typename T>329equals_t<T>330equals(T &&x) {331return { std::forward<T>(x) };332}333334class name_equals {335public:336name_equals(const std::string &name) : name(name) {337}338339template<typename T>340bool341operator()(const T &x) const {342return std::string(x.name.begin(), x.name.end()) == name;343}344345private:346const std::string &name;347};348349template<typename T>350class key_equals_t {351public:352key_equals_t(T &&x) : x(x) {353}354355template<typename P>356bool357operator()(const P &p) const {358return p.first == x;359}360361private:362T x;363};364365template<typename T>366key_equals_t<T>367key_equals(T &&x) {368return { std::forward<T>(x) };369}370371template<typename T>372class type_equals_t {373public:374type_equals_t(T type) : type(type) {375}376377template<typename S>378bool379operator()(const S &x) const {380return x.type == type;381}382383private:384T type;385};386387template<typename T>388type_equals_t<T>389type_equals(T x) {390return { x };391}392393template<typename T>394class id_type_equals_t {395public:396id_type_equals_t(const uint32_t id, T t) :397id(id), type(t) {398}399400template<typename X>401bool402operator()(const X &x) const {403return id == x.id && type(x);404}405406private:407const uint32_t id;408type_equals_t<T> type;409};410411template<typename T>412id_type_equals_t<T>413id_type_equals(const uint32_t id, T x) {414return { id, x };415}416417struct interval_overlaps {418template<typename T>419bool420operator()(T x0, T x1, T y0, T y1) {421return ((x0 <= y0 && y0 < x1) ||422(y0 <= x0 && x0 < y1));423}424};425}426427#endif428429430