~alinuxninja/nginx-edge/trunk

« back to all changes in this revision

Viewing changes to debian/modules/ngx_pagespeed/psol/include/pagespeed/kernel/base/enum_set.h

  • Committer: Vivian
  • Date: 2015-12-04 18:20:11 UTC
  • Revision ID: git-v1:a36f2bc32e884f7473b3a47040e5411306144d7d
* Do not extract psol.tar.gz

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright 2013 Google Inc.
3
 
 *
4
 
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 
 * you may not use this file except in compliance with the License.
6
 
 * You may obtain a copy of the License at
7
 
 *
8
 
 *      http://www.apache.org/licenses/LICENSE-2.0
9
 
 *
10
 
 * Unless required by applicable law or agreed to in writing, software
11
 
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 
 * See the License for the specific language governing permissions and
14
 
 * limitations under the License.
15
 
 */
16
 
 
17
 
// Author: jmarantz@google.com (Joshua Marantz)
18
 
 
19
 
#ifndef PAGESPEED_KERNEL_BASE_ENUM_SET_H_
20
 
#define PAGESPEED_KERNEL_BASE_ENUM_SET_H_
21
 
 
22
 
#include <bitset>
23
 
#include <cstddef>
24
 
 
25
 
namespace net_instaweb {
26
 
 
27
 
// Represents a set of values -- implemented via a bitset.
28
 
template<typename EnumType, size_t NumEnums> class EnumSet {
29
 
 public:
30
 
  bool IsSet(EnumType value) const {
31
 
    return bits_.test(static_cast<size_t>(value));
32
 
  }
33
 
 
34
 
  // Inserts a new value, returning true if a change was made.
35
 
  bool Insert(EnumType value) {
36
 
    bool result = !IsSet(value);
37
 
    insert(value);
38
 
    return result;
39
 
  }
40
 
 
41
 
  // Inserts a value; no return value.
42
 
  //
43
 
  // TODO(jmarantz): change call-sites to Insert and remove this one.
44
 
  void insert(EnumType value) {
45
 
    bits_.set(static_cast<size_t>(value));
46
 
  }
47
 
 
48
 
  // Returns true if a change was made.
49
 
  bool Erase(EnumType value) {
50
 
    bool result = IsSet(value);
51
 
    bits_.reset(static_cast<size_t>(value));
52
 
    return result;
53
 
  }
54
 
 
55
 
  // Mergess src into this, returning whether this resulted in a change.
56
 
  bool Merge(const EnumSet& src) {
57
 
    // We save the current version of the set in order to see whether
58
 
    // the merge resulted in a change.  Note that copying and comparing
59
 
    // the bits is very cheap; probably cheaper than calling count().
60
 
    EnumSet save(*this);
61
 
    bits_ |= src.bits_;
62
 
    return bits_ != save.bits_;
63
 
  }
64
 
 
65
 
  // Merges the entries *not* set in src into this, returning whether this
66
 
  // resulted in a change.
67
 
  bool MergeInverted(const EnumSet& src) {
68
 
    EnumSet save(*this);
69
 
    bits_ |= ~src.bits_;
70
 
    return bits_ != save.bits_;
71
 
  }
72
 
 
73
 
  void EraseSet(const EnumSet& src) {
74
 
    bits_ &= ~src.bits_;
75
 
  }
76
 
 
77
 
  // Sets all the entries to true.
78
 
  void SetAll() {
79
 
    bits_.set();
80
 
  }
81
 
 
82
 
  // Standard STL-like methods.
83
 
  void clear() { bits_.reset(); }
84
 
  size_t size() const { return bits_.count(); }
85
 
  bool empty() const { return bits_.none(); }
86
 
 
87
 
  // This overload is required for use in EXPECT_EQ in tests.
88
 
  bool operator==(const EnumSet& that) const {
89
 
    return bits_ == that.bits_;
90
 
  }
91
 
 
92
 
 private:
93
 
  typedef std::bitset<NumEnums> BitSet;
94
 
  BitSet bits_;
95
 
 
96
 
  // Implicit copy and assign will work perfectly and are required.
97
 
};
98
 
 
99
 
}  // namespace net_instaweb
100
 
 
101
 
#endif  // PAGESPEED_KERNEL_BASE_ENUM_SET_H_