tsIntegerSet.h 3.38 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
//-----------------------------------------------------------------------------
// Copyright (c) 2012 GarageGames, LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//-----------------------------------------------------------------------------

#ifndef _TSINTEGERSET_H_
#define _TSINTEGERSET_H_

#ifndef _PLATFORM_H_
#include "platform/platform.h"
#endif
#ifndef _TVECTOR_H_
#include "core/util/tVector.h"
#endif


#if defined(TORQUE_MAX_LIB)
#define MAX_TS_SET_DWORDS 32
#else
#define MAX_TS_SET_DWORDS 64
#endif

#define MAX_TS_SET_SIZE   (32*MAX_TS_SET_DWORDS)

class Stream;

/// The standard mathmatical set, where there are no duplicates.  However,
/// this set uses bits instead of numbers.
class TSIntegerSet
{
   /// The bits!
   U32 bits[MAX_TS_SET_DWORDS];

public:

   /// Sets this bit to false
   void clear(S32 index);
   /// Set this bit to true
   void set(S32 index);
   /// Is this bit true?
   bool test(S32 index) const;

   /// Sets all bits to false
   void clearAll(S32 upto = MAX_TS_SET_SIZE);
   /// Sets all bits to true
   void setAll(S32 upto = MAX_TS_SET_SIZE);
   /// Tests all bits for true
   bool testAll(S32 upto = MAX_TS_SET_SIZE) const;

   /// Counts set bits
   S32 count(S32 upto = MAX_TS_SET_SIZE) const;

   /// intersection (a & b)
   void intersect(const TSIntegerSet&);
   /// union (a | b)
   void overlap(const TSIntegerSet&);
   /// xor (a | b) & ( !(a & b) )
   void difference(const TSIntegerSet&);
   /// subtraction (a - b)
   void takeAway(const TSIntegerSet&);

   /// copy one integer set into another
   void copy(const TSIntegerSet&);

   void insert(S32 index, bool value);
   void erase(S32 index);

   void operator=(const TSIntegerSet& otherSet) { copy(otherSet); }

   S32 start() const;
   S32 end() const;
   void next(S32 & i) const;

   void read(Stream *);
   void write(Stream *) const;

   TSIntegerSet();
   TSIntegerSet(const TSIntegerSet&);
};

inline void TSIntegerSet::clear(S32 index)
{
   AssertFatal(index>=0 && index<MAX_TS_SET_SIZE,"TS::IntegerSet::clear");

   bits[index>>5] &= ~(1 << (index & 31));
}

inline void TSIntegerSet::set(S32 index)
{
   AssertFatal(index>=0 && index<MAX_TS_SET_SIZE,"TS::IntegerSet::set");

   bits[index>>5] |= 1 << (index & 31);
}

inline bool TSIntegerSet::test(S32 index) const
{
   AssertFatal(index>=0 && index<MAX_TS_SET_SIZE,"TS::IntegerSet::test");

   return ((bits[index>>5] & (1 << (index & 31)))!=0);
}

#endif