diff options
| author | jsmall-nvidia <jsmall@nvidia.com> | 2019-05-09 14:08:30 -0400 |
|---|---|---|
| committer | Tim Foley <tfoleyNV@users.noreply.github.com> | 2019-05-09 11:08:29 -0700 |
| commit | 30eee05f3f809e3950c8d8463ecdd9807c943692 (patch) | |
| tree | 29f7e87847cea14468e8cda79abece91494fdaa2 /source/core/int-set.h | |
| parent | 88a3f6476c37f3245de6d607d8055879f8892ee4 (diff) | |
IntSet -> UIntSet (#961)
* * Fix warning in vk-swap-chain around use of Index. Rename _indexOf to _indexOfFormat.
* Rename IntSet to UIntSet and put in own files slang-uint-set.h.cpp. Use UInt as the held type.
* On UintSet setMax -> resizeAndClear. Doing so revealed bug in add.
* Closer following of conventions - use kPrefix for constants (even though held in 'enum')
* Small fixes/improvements
* * Add some documentation to UIntSet methods
* Use memset to set/reset bits
* Fix some tabbing.
Rename oldBufferSize -> oldCount
* Fix tabs.
Diffstat (limited to 'source/core/int-set.h')
| -rw-r--r-- | source/core/int-set.h | 209 |
1 files changed, 0 insertions, 209 deletions
diff --git a/source/core/int-set.h b/source/core/int-set.h deleted file mode 100644 index 6b6243552..000000000 --- a/source/core/int-set.h +++ /dev/null @@ -1,209 +0,0 @@ -#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 <memory.h> - -namespace Slang -{ - /* The set works by storing as a bit per integer */ - class IntSet - { - private: - typedef uint32_t Element; ///< Type that holds the bits - enum - { - ELEMENT_SHIFT = 5, ///< How many bits to shift to get Element index from an index - ELEMENT_SIZE = sizeof(Element) * 8, ///< The number of bits in an element - ELEMENT_MASK = ELEMENT_SIZE - 1, ///< Mask to get shift from an index - }; - - // Make sure they are correct for the Element type - SLANG_COMPILE_TIME_ASSERT( (1 << ELEMENT_SHIFT) == ELEMENT_SIZE); - - List<Element> m_buffer; - - public: - IntSet() - {} - IntSet(const IntSet& other) - { - m_buffer = other.m_buffer; - } - IntSet(IntSet && other) - { - *this = (_Move(other)); - } - IntSet& operator=(IntSet&& other) - { - m_buffer = _Move(other.m_buffer); - return *this; - } - IntSet& operator=(const IntSet& other) - { - m_buffer = other.m_buffer; - return *this; - } - int GetHashCode() - { - int rs = 0; - for (auto val : m_buffer) - rs ^= val; - return rs; - } - IntSet(Int maxVal) - { - setMax(maxVal); - } - Int getCount() const - { - return Int(m_buffer.getCount()) * ELEMENT_SIZE; - } - void setMax(Int val) - { - resize(val); - clear(); - } - void setAll() - { - for (Index i = 0; i < m_buffer.getCount(); i++) - m_buffer[i] = ~Element(0); - } - void resize(Int size) - { - const Index oldBufferSize = m_buffer.getCount(); - const Index newCount = Index((size + ELEMENT_MASK) >> ELEMENT_SHIFT); - m_buffer.setCount(newCount); - - if (newCount > oldBufferSize) - memset(m_buffer.getBuffer() + oldBufferSize, 0, (newCount - oldBufferSize) * sizeof(Element)); - } - void clear() - { - for (Index i = 0; i < m_buffer.getCount(); i++) - m_buffer[i] = 0; - } - void clearAndDeallocate() - { - m_buffer.clearAndDeallocate(); - } - void add(Int val) - { - Index id = Index(val >> 5); - if (id < m_buffer.getCount()) - m_buffer[id] |= Element(1) << (val & ELEMENT_MASK); - else - { - Index oldCount = m_buffer.getCount(); - m_buffer.setCount(id + 1); - memset(m_buffer.getBuffer() + oldCount, 0, (m_buffer.getCount() - oldCount) * sizeof(Element)); - m_buffer[id] |= Element(1) << (val & ELEMENT_MASK); - } - } - void remove(Int val) - { - const Index idx = Index(val >> ELEMENT_SHIFT); - if (idx < m_buffer.getCount()) - m_buffer[idx] &= ~(Element(1) << (val & ELEMENT_MASK)); - } - bool contains(Int val) const - { - const Index idx = Index(val >> ELEMENT_SHIFT); - if (idx >= m_buffer.getCount()) - return false; - return (m_buffer[idx] & (Element(1) << (val & ELEMENT_MASK))) != 0; - } - void unionWith(const IntSet& set) - { - const Index minCount = Math::Min(set.m_buffer.getCount(), m_buffer.getCount()); - for (Index i = 0; i < minCount; i++) - { - m_buffer[i] |= set.m_buffer[i]; - } - if (set.m_buffer.getCount() > m_buffer.getCount()) - m_buffer.addRange(set.m_buffer.getBuffer()+m_buffer.getCount(), set.m_buffer.getCount()-m_buffer.getCount()); - } - bool operator==(const IntSet& set) const - { - Index minCount = Math::Min(set.m_buffer.getCount(), m_buffer.getCount()); - if (::memcmp(m_buffer.getBuffer(), set.m_buffer.getBuffer(), minCount * sizeof(Element)) != 0) - { - return false; - } - return m_buffer.getCount() == set.m_buffer.getCount() || (_areRemainingZeros(m_buffer, minCount) && _areRemainingZeros(set.m_buffer, minCount)); - } - bool operator!=(const IntSet& set) const - { - return !(*this == set); - } - void intersectWith(const IntSet& set) - { - if (set.m_buffer.getCount() < m_buffer.getCount()) - memset(m_buffer.getBuffer() + set.m_buffer.getCount(), 0, (m_buffer.getCount() - set.m_buffer.getCount()) * sizeof(Element)); - - const Index minCount = Math::Min(set.m_buffer.getCount(), m_buffer.getCount()); - for (Index i = 0; i < minCount; i++) - { - m_buffer[i] &= set.m_buffer[i]; - } - } - static void calcUnion(IntSet& outRs, const IntSet& set1, const IntSet& set2) - { - outRs.m_buffer.setCount(Math::Max(set1.m_buffer.getCount(), set2.m_buffer.getCount())); - outRs.clear(); - for (Index i = 0; i < set1.m_buffer.getCount(); i++) - outRs.m_buffer[i] |= set1.m_buffer[i]; - for (Index i = 0; i < set2.m_buffer.getCount(); i++) - outRs.m_buffer[i] |= set2.m_buffer[i]; - } - static void calcIntersection(IntSet& outRs, const IntSet& set1, const IntSet& set2) - { - const Index minCount = Math::Min(set1.m_buffer.getCount(), set2.m_buffer.getCount()); - outRs.m_buffer.setCount(minCount); - - for (Index i = 0; i < minCount; i++) - outRs.m_buffer[i] = set1.m_buffer[i] & set2.m_buffer[i]; - } - static void calcSubtract(IntSet& outRs, const IntSet& set1, const IntSet& set2) - { - outRs.m_buffer.setCount(set1.m_buffer.getCount()); - - const Index minCount = Math::Min(set1.m_buffer.getCount(), set2.m_buffer.getCount()); - for (Index i = 0; i < minCount; i++) - outRs.m_buffer[i] = set1.m_buffer[i] & (~set2.m_buffer[i]); - } - static bool hasIntersection(const IntSet& set1, const IntSet& set2) - { - const Index minCount = Math::Min(set1.m_buffer.getCount(), set2.m_buffer.getCount()); - for (Index i = 0; i < minCount; i++) - { - if (set1.m_buffer[i] & set2.m_buffer[i]) - return true; - } - return false; - } - - private: - static bool _areRemainingZeros(const List<Element>& elems, Index minCount) - { - const Index count = elems.getCount(); - const Element* base = elems.getBuffer(); - - for (Index i = minCount; i < count; ++i) - { - if (base[i]) - { - return false; - } - } - return true; - } - - }; -} - -#endif |
