add bitset operations and tests
[hiphop-php.git] / hphp / runtime / vm / fixed-string-map.h
blobfaa559735bf21c2c8c9724a556187c59b5697670
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 +----------------------------------------------------------------------+
17 #ifndef incl_HPHP_FIXED_STRING_MAP_H_
18 #define incl_HPHP_FIXED_STRING_MAP_H_
20 #include <cstdint>
21 #include <limits>
22 #include <vector>
24 #include "hphp/runtime/base/string-data.h"
26 #include "hphp/util/functional.h"
27 #include "hphp/util/hash-map-typedefs.h"
29 namespace HPHP {
31 //////////////////////////////////////////////////////////////////////
33 template<class V, bool case_sensitive, class ExtraType = int32_t>
34 struct FixedStringMap {
35 explicit FixedStringMap(int num) : m_table(nullptr) { init(num); }
36 FixedStringMap() : m_mask(0), m_table(nullptr) {}
37 ~FixedStringMap() { clear(); }
39 FixedStringMap(const FixedStringMap&) = delete;
40 const FixedStringMap& operator=(const FixedStringMap&) = delete;
42 void clear();
43 void init(int num, uint32_t numExtraBytes = 0);
44 void add(const StringData* s, const V& v);
45 V* find(const StringData* s) const;
47 void* extraData() { return m_table; }
48 const void* extraData() const { return m_table; }
49 ExtraType& extra() { return m_extra; }
50 const ExtraType& extra() const { return m_extra; }
52 static constexpr ptrdiff_t tableOff() {
53 return offsetof(FixedStringMap, m_table);
55 ExtraType size() const { return m_extra; }
57 private:
58 struct Elm {
59 LowStringPtr sd;
60 V data;
64 * The fields we need here are just m_mask and m_table. This would leave 4
65 * byte padding hole, though, which some users (e.g. IndexedStringMap) might
66 * have a use for, so we expose it as a slot for our users.
68 uint32_t m_mask;
69 ExtraType m_extra; // not ours
70 Elm* m_table;
73 //////////////////////////////////////////////////////////////////////
75 template<class T, class V, bool case_sensitive, class ExtraType = int32_t>
76 struct FixedStringMapBuilder {
77 using EqObject = typename std::conditional<
78 case_sensitive,
79 string_data_same,
80 string_data_isame
81 >::type;
83 using Map = hphp_hash_map<const StringData*, V, string_data_hash, EqObject>;
84 using FSMap = FixedStringMap<V, case_sensitive, ExtraType>;
86 using const_iterator = typename Map::const_iterator;
87 using iterator = typename Map::iterator;
89 iterator find(const StringData* key) { return m_map.find(key); }
90 iterator begin() { return m_map.begin(); }
91 iterator end() { return m_map.end(); }
92 V size() const { return m_list.size(); }
94 const_iterator find(const StringData* key) const {
95 return m_map.find(key);
97 const_iterator begin() const { return m_map.begin(); }
98 const_iterator end() const { return m_map.end(); }
100 T& operator[](V idx);
101 const T& operator[](V idx) const;
104 * Add an object to the position on the end, and allow lookup by `name'.
106 void add(const StringData* name, const T& t);
109 * Add an object that occupies an index but can't be located by name.
111 void addUnnamed(const T& t);
114 * Create a FixedStringMap from the builder.
116 void create(FSMap& map);
118 private:
119 std::vector<T> m_list;
120 Map m_map;
123 //////////////////////////////////////////////////////////////////////
127 #include "hphp/runtime/vm/fixed-string-map-inl.h"
129 #endif