Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(143)

Side by Side Diff: ui/events/gesture_detection/bitset_32.h

Issue 128613003: [Tracking Patch] Unified gesture detection (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Cleanup Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef UI_EVENTS_GESTURE_DETECTION_BITSET_32_H_
6 #define UI_EVENTS_GESTURE_DETECTION_BITSET_32_H_
7
8 #include "base/basictypes.h"
9
10 namespace ui {
11
12 // Port of BitSet32 from Android
13 // * platform/system/core/include/utils/BitSet.h
14 // * Change-Id: I9bbf41f9d2d4a2593b0e6d7d8be7e283f985bade
15 // * Please update the Change-Id as upstream Android changes are pulled.
16 struct BitSet32 {
17 uint32_t value;
18
19 inline BitSet32() : value(0) {}
20 explicit inline BitSet32(uint32_t value) : value(value) {}
21
22 // Gets the value associated with a particular bit index.
23 static inline uint32_t value_for_bit(uint32_t n) { return 0x80000000 >> n; }
24
25 // Clears the bit set.
26 inline void clear() { value = 0; }
27
28 // Returns the number of marked bits in the set.
29 inline uint32_t count() const { return popcnt(value); }
30
31 // Returns true if the bit set does not contain any marked bits.
32 inline bool is_empty() const { return !value; }
33
34 // Returns true if the bit set does not contain any unmarked bits.
35 inline bool is_full() const { return value == 0xffffffff; }
36
37 // Returns true if the specified bit is marked.
38 inline bool has_bit(uint32_t n) const { return value & value_for_bit(n); }
39
40 // Marks the specified bit.
41 inline void mark_bit(uint32_t n) { value |= value_for_bit(n); }
42
43 // Clears the specified bit.
44 inline void clear_bit(uint32_t n) { value &= ~value_for_bit(n); }
45
46 // Finds the first marked bit in the set.
47 // Result is undefined if all bits are unmarked.
48 inline uint32_t first_marked_bit() const { return clz(value); }
49
50 // Finds the first unmarked bit in the set.
51 // Result is undefined if all bits are marked.
52 inline uint32_t first_unmarked_bit() const { return clz(~value); }
53
54 // Finds the last marked bit in the set.
55 // Result is undefined if all bits are unmarked.
56 inline uint32_t last_marked_bit() const { return 31 - ctz(value); }
57
58 // Finds the first marked bit in the set and clears it. Returns the bit
59 // index.
60 // Result is undefined if all bits are unmarked.
61 inline uint32_t clear_first_marked_bit() {
62 uint32_t n = first_marked_bit();
63 clear_bit(n);
64 return n;
65 }
66
67 // Finds the first unmarked bit in the set and marks it. Returns the bit
68 // index.
69 // Result is undefined if all bits are marked.
70 inline uint32_t mark_first_unmarked_bit() {
71 uint32_t n = first_unmarked_bit();
72 mark_bit(n);
73 return n;
74 }
75
76 // Finds the last marked bit in the set and clears it. Returns the bit index.
77 // Result is undefined if all bits are unmarked.
78 inline uint32_t clear_last_marked_bit() {
79 uint32_t n = last_marked_bit();
80 clear_bit(n);
81 return n;
82 }
83
84 // Gets the inde of the specified bit in the set, which is the number of
85 // marked bits that appear before the specified bit.
86 inline uint32_t get_index_of_bit(uint32_t n) const {
87 return popcnt(value & ~(0xffffffffUL >> n));
88 }
89
90 inline bool operator==(const BitSet32& other) const {
91 return value == other.value;
92 }
93 inline bool operator!=(const BitSet32& other) const {
94 return value != other.value;
95 }
96
97 private:
98
99 #if defined(COMPILER_GCC) || defined(__clang__)
100 static inline uint32_t popcnt(uint32_t v) { return __builtin_popcount(v); }
101 static inline uint32_t clz(uint32_t v) { return __builtin_clz(v); }
102 static inline uint32_t ctz(uint32_t v) { return __builtin_ctz(v); }
103 #else
104 // http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel
105 static inline uint32_t popcnt(uint32_t v) {
106 v = v - ((v >> 1) & 0x55555555);
107 v = (v & 0x33333333) + ((v >> 2) & 0x33333333);
108 return (((v + (v >> 4)) & 0x0F0F0F0F) * 0x01010101) >> 24;
109 }
110 static inline uint32_t clz(uint32_t v) {
111 v |= (v >> 1);
112 v |= (v >> 2);
113 v |= (v >> 4);
114 v |= (v >> 8);
115 v |= (v >> 16);
116 return 32 - popcnt(v);
117 }
118 static inline uint32_t ctz(uint32_t v) {
119 return popcnt((v & -v) - 1));
120 }
121 #endif
122 };
123
124 } // namespace ui
125
126 #endif // UI_EVENTS_GESTURE_DETECTION_BITSET_32_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698