]> gitweb.ps.run Git - toc/blob - antlr4-cpp-runtime-4.9.2-source/runtime/src/support/BitSet.h
add antlr source code and ReadMe
[toc] / antlr4-cpp-runtime-4.9.2-source / runtime / src / support / BitSet.h
1 /* Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
2  * Use of this file is governed by the BSD 3-clause license that
3  * can be found in the LICENSE.txt file in the project root.
4  */
5
6 #pragma once
7
8 #include "antlr4-common.h"
9
10 namespace antlrcpp {
11
12   class ANTLR4CPP_PUBLIC BitSet : public std::bitset<2048> {
13   public:
14     size_t nextSetBit(size_t pos) const {
15       for (size_t i = pos; i < size(); i++){
16         if (test(i)) {
17           return i;
18         }
19       }
20
21       return INVALID_INDEX;
22     }
23
24     // Prints a list of every index for which the bitset contains a bit in true.
25     friend std::wostream& operator << (std::wostream& os, const BitSet& obj)
26     {
27       os << "{";
28       size_t total = obj.count();
29       for (size_t i = 0; i < obj.size(); i++){
30         if (obj.test(i)){
31           os << i;
32           --total;
33           if (total > 1){
34             os << ", ";
35           }
36         }
37       }
38
39       os << "}";
40       return os;
41     }
42
43     static std::string subStringRepresentation(const std::vector<BitSet>::iterator &begin,
44                                                 const std::vector<BitSet>::iterator &end) {
45       std::string result;
46       std::vector<BitSet>::iterator vectorIterator;
47
48       for (vectorIterator = begin; vectorIterator != end; vectorIterator++) {
49         result += vectorIterator->toString();
50       }
51       // Grab the end
52       result += end->toString();
53
54       return result;
55     }
56
57     std::string toString(){
58       std::stringstream stream;
59       stream << "{";
60       bool valueAdded = false;
61       for (size_t i = 0; i < size(); ++i){
62         if (test(i)){
63           if (valueAdded) {
64             stream << ", ";
65           }
66           stream << i;
67           valueAdded = true;
68         }
69       }
70
71       stream << "}";
72       return stream.str();
73     }
74
75   };
76 }