From fcf83dbf9effab3bd98bad2b83b2468b7eb05cfd Mon Sep 17 00:00:00 2001 From: Tim Foley Date: Fri, 9 Jun 2017 11:34:21 -0700 Subject: Initial import of code. --- source/core/int-set.h | 166 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 166 insertions(+) create mode 100644 source/core/int-set.h (limited to 'source/core/int-set.h') diff --git a/source/core/int-set.h b/source/core/int-set.h new file mode 100644 index 000000000..cb8f788f2 --- /dev/null +++ b/source/core/int-set.h @@ -0,0 +1,166 @@ +#ifndef BIT_VECTOR_INT_SET_H +#define BIT_VECTOR_INT_SET_H + +#include "list.h" +#include "slang-math.h" +#include "common.h" +#include "exception.h" + +#include + +namespace CoreLib +{ + namespace Basic + { + class IntSet + { + private: + List buffer; + public: + IntSet() + {} + IntSet(const IntSet & other) + { + buffer = other.buffer; + } + IntSet(IntSet && other) + { + *this = (_Move(other)); + } + IntSet & operator = (IntSet && other) + { + buffer = _Move(other.buffer); + return *this; + } + IntSet & operator = (const IntSet & other) + { + buffer = other.buffer; + return *this; + } + int GetHashCode() + { + int rs = 0; + for (auto val : buffer) + rs ^= val; + return rs; + } + IntSet(int maxVal) + { + SetMax(maxVal); + } + int Size() const + { + return buffer.Count()*32; + } + void SetMax(int val) + { + Resize(val); + Clear(); + } + void SetAll() + { + for (int i = 0; i>5); + if (buffer.Count() > oldBufferSize) + memset(buffer.Buffer()+oldBufferSize, 0, (buffer.Count()-oldBufferSize) * sizeof(int)); + } + void Clear() + { + for (int i = 0; i>5; + if (id < buffer.Count()) + buffer[id] |= (1<<(val&31)); + else + { + int oldSize = buffer.Count(); + buffer.SetSize(id+1); + memset(buffer.Buffer() + oldSize, 0, (buffer.Count()-oldSize)*sizeof(int)); + buffer[id] |= (1<<(val&31)); + } + } + void Remove(int val) + { + if ((val>>5) < buffer.Count()) + buffer[(val>>5)] &= ~(1<<(val&31)); + } + bool Contains(int val) const + { + if ((val>>5) >= buffer.Count()) + return false; + return (buffer[(val>>5)] & (1<<(val&31))) != 0; + } + void UnionWith(const IntSet & set) + { + for (int i = 0; i buffer.Count()) + buffer.AddRange(set.buffer.Buffer()+buffer.Count(), set.buffer.Count()-buffer.Count()); + } + bool operator == (const IntSet & set) + { + if (buffer.Count() != set.buffer.Count()) + return false; + for (int i = 0; i