diff options
| author | Patrick Schönberger | 2021-08-14 14:56:12 +0200 |
|---|---|---|
| committer | Patrick Schönberger | 2021-08-14 14:56:12 +0200 |
| commit | c6ad2948bb98d42f8e0883ef82cd14cd2d5eda60 (patch) | |
| tree | 9e83d6d8f61e56f5d3425b8709314d6bdb9315a9 /antlr4-cpp-runtime-4.9.2-source/runtime/src/support/BitSet.h | |
| parent | 9f94b672a5dc32da5ad01742bd4e976315a30d9c (diff) | |
| download | toc-c6ad2948bb98d42f8e0883ef82cd14cd2d5eda60.tar.gz toc-c6ad2948bb98d42f8e0883ef82cd14cd2d5eda60.zip | |
Diffstat (limited to 'antlr4-cpp-runtime-4.9.2-source/runtime/src/support/BitSet.h')
| -rw-r--r-- | antlr4-cpp-runtime-4.9.2-source/runtime/src/support/BitSet.h | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/antlr4-cpp-runtime-4.9.2-source/runtime/src/support/BitSet.h b/antlr4-cpp-runtime-4.9.2-source/runtime/src/support/BitSet.h new file mode 100644 index 0000000..bf849b1 --- /dev/null +++ b/antlr4-cpp-runtime-4.9.2-source/runtime/src/support/BitSet.h @@ -0,0 +1,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(){ + 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(); + } + + }; +} |
