operator== and better archive for BitArray

This commit is contained in:
Christopher Lackner 2020-08-29 15:37:48 +02:00
parent 020dd4373d
commit f45fbfd811
2 changed files with 43 additions and 20 deletions

View File

@ -83,6 +83,18 @@ namespace ngcore
return *this; return *this;
} }
bool BitArray :: operator==(const BitArray& other) const
{
if(size != other.Size())
return false;
for(auto i : Range(size/CHAR_BIT))
if(data[i] != other.data[i])
return false;
for(auto i : Range(size%CHAR_BIT))
if(Test(i + size * (size/CHAR_BIT)) != other.Test(i + size * (size/CHAR_BIT)))
return false;
return true;
}
BitArray & BitArray :: operator= (const BitArray & ba2) BitArray & BitArray :: operator= (const BitArray & ba2)
{ {
@ -115,29 +127,39 @@ namespace ngcore
return cnt; return cnt;
} }
Archive & operator & (Archive & archive, BitArray & ba) void BitArray :: DoArchive(Archive& archive)
{
if(archive.GetVersion("netgen") >= "v6.2.2007-62")
{
archive.NeedsVersion("netgen", "v6.2.2007-62");
auto size = Size();
archive & size;
if(archive.Input())
SetSize(size);
archive.Do(data, size/CHAR_BIT+1);
}
else
{ {
if (archive.Output()) if (archive.Output())
{ {
archive << ba.Size(); throw Exception("should not get here");
for (size_t i = 0; i < ba.Size(); i++) archive << Size();
archive << ba[i]; for (size_t i = 0; i < Size(); i++)
archive << (*this)[i];
} }
else else
{ {
size_t size; size_t size;
archive & size; archive & size;
ba.SetSize (size); SetSize (size);
ba.Clear(); Clear();
for (size_t i = 0; i < size; i++) for (size_t i = 0; i < size; i++)
{ {
bool b; bool b;
archive & b; archive & b;
if (b) ba.SetBit(i); if (b) SetBit(i);
} }
} }
return archive;
} }
} }
} // namespace ngcore

View File

@ -131,6 +131,7 @@ public:
return Test(i); return Test(i);
} }
bool operator==(const BitArray& other) const;
/// invert all bits /// invert all bits
NGCORE_API BitArray & Invert (); NGCORE_API BitArray & Invert ();
@ -145,6 +146,9 @@ public:
NGCORE_API BitArray & operator= (const BitArray & ba2); NGCORE_API BitArray & operator= (const BitArray & ba2);
NGCORE_API size_t NumSet () const; NGCORE_API size_t NumSet () const;
void DoArchive(Archive& archive);
private: private:
/// ///
unsigned char Mask (size_t i) const unsigned char Mask (size_t i) const
@ -190,11 +194,8 @@ private:
return res; return res;
} }
NGCORE_API std::ostream & operator<<(std::ostream & s, const BitArray & ba); NGCORE_API std::ostream & operator<<(std::ostream & s, const BitArray & ba);
NGCORE_API Archive & operator & (Archive & archive, BitArray & ba);
} // namespace ngcore } // namespace ngcore
#endif // NETGEN_CORE_BITARRAY #endif // NETGEN_CORE_BITARRAY