tsIntegerSet.h

Engine/source/ts/tsIntegerSet.h

More...

Classes:

class

The standard mathmatical set, where there are no duplicates.

Public Defines

define
define

Detailed Description

Public Defines

MAX_TS_SET_DWORDS() 64
MAX_TS_SET_SIZE() (32*)
  1
  2//-----------------------------------------------------------------------------
  3// Copyright (c) 2012 GarageGames, LLC
  4//
  5// Permission is hereby granted, free of charge, to any person obtaining a copy
  6// of this software and associated documentation files (the "Software"), to
  7// deal in the Software without restriction, including without limitation the
  8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9// sell copies of the Software, and to permit persons to whom the Software is
 10// furnished to do so, subject to the following conditions:
 11//
 12// The above copyright notice and this permission notice shall be included in
 13// all copies or substantial portions of the Software.
 14//
 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 21// IN THE SOFTWARE.
 22//-----------------------------------------------------------------------------
 23
 24#ifndef _TSINTEGERSET_H_
 25#define _TSINTEGERSET_H_
 26
 27#ifndef _PLATFORM_H_
 28#include "platform/platform.h"
 29#endif
 30#ifndef _TVECTOR_H_
 31#include "core/util/tVector.h"
 32#endif
 33
 34
 35#if defined(TORQUE_MAX_LIB)
 36#define MAX_TS_SET_DWORDS 32
 37#else
 38#define MAX_TS_SET_DWORDS 64
 39#endif
 40
 41#define MAX_TS_SET_SIZE   (32*MAX_TS_SET_DWORDS)
 42
 43class Stream;
 44
 45/// The standard mathmatical set, where there are no duplicates.  However,
 46/// this set uses bits instead of numbers.
 47class TSIntegerSet
 48{
 49   /// The bits!
 50   U32 bits[MAX_TS_SET_DWORDS];
 51
 52public:
 53
 54   /// Sets this bit to false
 55   void clear(S32 index);
 56   /// Set this bit to true
 57   void set(S32 index);
 58   /// Is this bit true?
 59   bool test(S32 index) const;
 60
 61   /// Sets all bits to false
 62   void clearAll(S32 upto = MAX_TS_SET_SIZE);
 63   /// Sets all bits to true
 64   void setAll(S32 upto = MAX_TS_SET_SIZE);
 65   /// Tests all bits for true
 66   bool testAll(S32 upto = MAX_TS_SET_SIZE) const;
 67
 68   /// Counts set bits
 69   S32 count(S32 upto = MAX_TS_SET_SIZE) const;
 70
 71   /// intersection (a & b)
 72   void intersect(const TSIntegerSet&);
 73   /// union (a | b)
 74   void overlap(const TSIntegerSet&);
 75   /// xor (a | b) & ( !(a & b) )
 76   void difference(const TSIntegerSet&);
 77   /// subtraction (a - b)
 78   void takeAway(const TSIntegerSet&);
 79
 80   /// copy one integer set into another
 81   void copy(const TSIntegerSet&);
 82
 83   void insert(S32 index, bool value);
 84   void erase(S32 index);
 85
 86   void operator=(const TSIntegerSet& otherSet) { copy(otherSet); }
 87
 88   S32 start() const;
 89   S32 end() const;
 90   void next(S32 & i) const;
 91
 92   void read(Stream *);
 93   void write(Stream *) const;
 94
 95   TSIntegerSet();
 96   TSIntegerSet(const TSIntegerSet&);
 97};
 98
 99inline void TSIntegerSet::clear(S32 index)
100{
101   AssertFatal(index>=0 && index<MAX_TS_SET_SIZE,"TS::IntegerSet::clear");
102
103   bits[index>>5] &= ~(1 << (index & 31));
104}
105
106inline void TSIntegerSet::set(S32 index)
107{
108   AssertFatal(index>=0 && index<MAX_TS_SET_SIZE,"TS::IntegerSet::set");
109
110   bits[index>>5] |= 1 << (index & 31);
111}
112
113inline bool TSIntegerSet::test(S32 index) const
114{
115   AssertFatal(index>=0 && index<MAX_TS_SET_SIZE,"TS::IntegerSet::test");
116
117   return ((bits[index>>5] & (1 << (index & 31)))!=0);
118}
119
120#endif
121