aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/antlr4_cpp_runtime/src/support/BitSet.h
blob: bb30364be089ff8c656bb3ba4f143ad946913e3d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/* Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
 * Use of this file is governed by the BSD 3-clause license that
 * can be found in the LICENSE.txt file in the project root.
 */

#pragma once

#include "antlr4-common.h"

namespace antlrcpp {

  class ANTLR4CPP_PUBLIC BitSet : public std::bitset<2048> {
  public:
    size_t nextSetBit(size_t pos) const {
      for (size_t i = pos; i < size(); i++){
        if (test(i)) {
          return i;
        }
      }

      return INVALID_INDEX;
    }

    // Prints a list of every index for which the bitset contains a bit in true.
    friend std::wostream& operator << (std::wostream& os, const BitSet& obj)
    {
      os << "{";
      size_t total = obj.count();
      for (size_t i = 0; i < obj.size(); i++){
        if (obj.test(i)){
          os << i;
          --total;
          if (total > 1){
            os << ", ";
          }
        }
      }

      os << "}";
      return os;
    }

    static std::string subStringRepresentation(const std::vector<BitSet>::iterator &begin,
                                                const std::vector<BitSet>::iterator &end) {
      std::string result;
      std::vector<BitSet>::iterator vectorIterator;

      for (vectorIterator = begin; vectorIterator != end; vectorIterator++) {
        result += vectorIterator->toString();
      }
      // Grab the end
      result += end->toString();

      return result;
    }

    std::string toString() const {
      std::stringstream stream;
      stream << "{";
      bool valueAdded = false;
      for (size_t i = 0; i < size(); ++i){
        if (test(i)){
          if (valueAdded) {
            stream << ", ";
          }
          stream << i;
          valueAdded = true;
        }
      }

      stream << "}";
      return stream.str();
    }

  };
}