Move io_tests to folly/io/async/test
[hiphop-php.git] / hphp / util / functional.h
blob4338acd48cfa9d9efb1882c3ec80cffdd42bf7a3
1 /*
2 +----------------------------------------------------------------------+
3 | HipHop for PHP |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2010-present Facebook, Inc. (http://www.facebook.com) |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
16 #pragma once
18 #include <cstring>
19 #include <string>
21 #include <folly/portability/String.h>
22 #include <folly/Range.h>
24 #include "hphp/util/bstring.h"
25 #include "hphp/util/hash.h"
27 namespace HPHP {
29 //////////////////////////////////////////////////////////////////////
32 * Small function objects for use with STL containers and things of
33 * that sort.
36 //////////////////////////////////////////////////////////////////////
38 struct cstr_hash {
39 size_t operator()(const char* s) const {
40 return hash_string_cs(s, strlen(s));
44 struct ltstr {
45 bool operator()(const char *s1, const char *s2) const {
46 return strcmp(s1, s2) < 0;
50 struct eqstr {
51 bool operator()(const char* s1, const char* s2) const {
52 return strcmp(s1, s2) == 0;
56 struct stdltstr {
57 bool operator()(const std::string &s1, const std::string &s2) const {
58 return strcmp(s1.c_str(), s2.c_str()) < 0;
62 struct stdltistr {
63 using is_transparent = void;
65 bool operator()(const std::string &s1, const std::string &s2) const {
66 return strcasecmp(s1.c_str(), s2.c_str()) < 0;
68 bool operator()(const std::string &s1, folly::StringPiece s2) const {
69 return bstrcasecmp(s1, s2) < 0;
71 bool operator()(folly::StringPiece s1, const std::string &s2) const {
72 return bstrcasecmp(s1, s2) < 0;
74 bool operator()(const std::string &s1, const char* s2) const {
75 return strcasecmp(s1.c_str(), s2) < 0;
77 bool operator()(const char* s1, const std::string &s2) const {
78 return strcasecmp(s1, s2.c_str()) < 0;
82 struct string_hash {
83 size_t operator()(const std::string &s) const {
84 return hash_string_cs_unsafe(s.c_str(), s.size());
86 size_t hash(const std::string &s) const {
87 return operator()(s);
91 template <typename T>
92 struct integralHashCompare {
93 bool equal(T s1, T s2) const {
94 static_assert(std::is_integral<T>::value, "");
95 return s1 == s2;
97 size_t hash(T s) const {
98 static_assert(sizeof(T) <= sizeof(int64_t), "");
99 return hash_int64(int64_t(s));
103 struct stringHashCompare {
104 bool equal(const std::string& s1, const std::string& s2) const {
105 return s1 == s2;
107 size_t hash(const std::string& s) const {
108 return hash_string_cs_unsafe(s.c_str(), s.size());
112 struct stringiHashCompare {
113 bool equal(const std::string& s1, const std::string& s2) const {
114 return s1.size() == s2.size() &&
115 strncasecmp(s1.data(), s2.data(), s1.size()) == 0;
117 size_t hash(const std::string& s) const {
118 return hash_string_i_unsafe(s.c_str(), s.size());
122 template <typename T, typename U, typename THash, typename UHash>
123 struct pairHashCompare {
124 THash thash;
125 UHash uhash;
127 using PairType = std::pair<T, U>;
129 size_t operator() (const PairType& pair) const {
130 return hash(pair);
133 size_t hash(const PairType& pair) const {
134 return hash_int64_pair(thash.hash(pair.first), uhash.hash(pair.second));
137 bool equal(const PairType& a, const PairType& b) const {
138 return thash.equal(a.first, b.first) && uhash.equal(a.second, b.second);
142 template<typename T>
143 struct int_hash {
144 static_assert(std::is_integral<T>::value);
145 size_t operator() (T v) const {
146 return hash_int64(static_cast<uint64_t>(v));
150 using int64_hash = int_hash<int64_t>;
151 using uint64_hash = int_hash<uint64_t>;
152 using int32_hash = int_hash<int32_t>;
153 using uint32_hash = int_hash<uint32_t>;
155 template<typename T>
156 struct pointer_hash {
157 size_t operator() (const T *const p) const {
158 return hash_int64(intptr_t(p));
160 size_t hash(const T *const p) const {
161 return operator()(p);
163 bool equal(const T *const lhs,
164 const T *const rhs) const {
165 return lhs == rhs;
169 template<typename T>
170 struct smart_pointer_hash {
171 size_t operator() (const T &p) const {
172 return (size_t)hash_int64(intptr_t(p.get()));
174 size_t hash (const T &p) const {
175 return operator()(p);
177 bool equal(const T &lhs, const T &rhs) const {
178 return lhs.get() == rhs.get();
182 //////////////////////////////////////////////////////////////////////
183 // Case-insensitive ones.
185 struct hashi {
186 size_t operator()(const char *s) const {
187 return hash_string_i(s, strlen(s));
190 struct eqstri {
191 bool operator()(const char* s1, const char* s2) const {
192 return strcasecmp(s1, s2) == 0;
196 struct string_hashi {
197 size_t operator()(const std::string &s) const {
198 return hash_string_i_unsafe(s.data(), s.size());
202 template<typename S, typename S2=S>
203 struct stringlike_eqstri {
204 bool operator()(const S &s1, const S2 &s2) const {
205 return s1.size() == s2.size() &&
206 strncasecmp(s1.data(), s2.data(), s1.size()) == 0;
209 typedef stringlike_eqstri<std::string> string_eqstri;
211 struct string_lessi {
212 bool operator()(const std::string &s1, const std::string &s2) const {
213 return strcasecmp(s1.data(), s2.data()) < 0;
217 //////////////////////////////////////////////////////////////////////