//============================================================================= // bitvect.cpp // Primitive bit vector template class functions. // // Notes // Macro 'NO_NAMESPACES' should be set to nonzero (true) if the compiler // does not support namespaces. // // Macro 'NO_AND' should be set to nonzero (true) if the compiler does not // support the 'and' alternate spelling keyword. // // History // 1.00, 1998-09-28, David R Tribble. // First cut. // // 1.01, 1998-10-03, David R Tribble. // Minor changes. // // 1.02, 1998-10-14, David R Tribble. // Added namespace control. // // Author // David R. Tribble // dtribble@technologist.com // http://www.flash.net/~dtribble // // Copyright ©1998 by David R. Tribble, all rights reserved. // Permission is hereby granted to use, distribute, and modify this source // code provided that the original copyright and authorship notices remain // intact. //----------------------------------------------------------------------------- // Identification static const char drt_bitvect_cpp_id[] = "@(#)bitvect.cpp 1.02 1998-10-14 dtribble"; // Special includes #ifdef _DLL #define drt_bitvect_lib #endif #include "bitvect.hpp" // System includes #if DrtBitVect_TEST #include #endif // Compiler deficiencies #if NO_AND #ifndef and #define and && #define or || #define not ! #endif #endif // Namespace #if !NO_NAMESPACES namespace Drt { #endif //----------------------------------------------------------------------------- // Shared (static) class constants //----------------------------------------------------------------------------- #if DrtBitVectImpl_VERS/100 != 1 #error DrtBitVectImpl_VERS has changed #endif /*static*/ const int DrtBitVectImpl::VERS = DrtBitVectImpl_VERS; // Class version number //----------------------------------------------------------------------------- // Shared (static) class constants //----------------------------------------------------------------------------- #if DrtBitVectAsg_VERS/100 != 1 #error DrtBitVectAsg_VERS has changed #endif /*static*/ const int DrtBitVectAsg::VERS = DrtBitVectAsg_VERS; // Class version number //----------------------------------------------------------------------------- // Shared (static) class variables //----------------------------------------------------------------------------- #if DrtBitVectImpl_VERS/100 != 1 #error DrtBitVectImpl_VERS has changed #endif /*static*/ unsigned long DrtBitVectImpl::s_dummy; // Assignable dummy bits //----------------------------------------------------------------------------- // Shared (static) class member functions //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- // DrtBitVectImpl::get() // Retrieves bit number 'b' out of bit vector 's' of 'sz' bits. // // Returns // The retrieved bit value (either true or false), or false on error. //----------------------------------------------------------------------------- /*static*/ bool DrtBitVectImpl::get(const unsigned long *s, int sz, int b) { #if DrtBitVectImpl_VERS/100 != 1 #error DrtBitVectImpl_VERS has changed #endif #if DrtBitVect_TEST printf("Impl::get(%08p:%d, %d)\n", s, sz, b); #endif // Get bit 'b' of bit vector if (b < 0 or b >= sz*BPW) return (false); // throw(Exception_BadSubscript); else return ((s[b/BPW] & (1UL << b%BPW)) != 0); } //----------------------------------------------------------------------------- // DrtBitVectImpl::set() // Sets bit number 'b' out of bit vector 's' of 'sz' bits to true. // // Returns // True, or false on error. //----------------------------------------------------------------------------- /*static*/ bool DrtBitVectImpl::set(unsigned long *s, int sz, int b) { #if DrtBitVectImpl_VERS/100 != 1 #error DrtBitVectImpl_VERS has changed #endif #if DrtBitVect_TEST printf("Impl::set(%08p:%d, %d)\n", s, sz, b); #endif // Set bit 'b' of bit vector if (b < 0 or b >= sz*BPW) return (false); // throw(Exception_BadSubscript); else { s[b/BPW] |= (1UL << b%BPW); return (true); } } //----------------------------------------------------------------------------- // DrtBitVectImpl::set() // Sets bit number 'b' out of bit vector 's' of 'sz' bits to value 'v'. // // Returns // Value 'v', or false on error. //----------------------------------------------------------------------------- /*static*/ bool DrtBitVectImpl::set(unsigned long *s, int sz, int b, bool v) { #if DrtBitVectImpl_VERS/100 != 1 #error DrtBitVectImpl_VERS has changed #endif #if DrtBitVect_TEST printf("Impl::set(%08p:%d, %d, %d)\n", s, sz, b, v); #endif // Set bit 'b' of bit vector to 'v' if (b < 0 or b >= sz*BPW) return (false); // throw(Exception_BadSubscript); else { if (v) s[b/BPW] |= (1UL << b%BPW); else s[b/BPW] &= ~(1UL << b%BPW); return (v); } } //----------------------------------------------------------------------------- // DrtBitVectImpl::clear() // Resets bit number 'b' out of bit vector 's' of 'sz' bits to false. // // Returns // False. //----------------------------------------------------------------------------- /*static*/ bool DrtBitVectImpl::clear(unsigned long *s, int sz, int b) { #if DrtBitVectImpl_VERS/100 != 1 #error DrtBitVectImpl_VERS has changed #endif #if DrtBitVect_TEST printf("Impl::clear(%08p:%d, %d)\n", s, sz, b); #endif // Clear bit 'b' of bit vector if (b < 0 or b >= sz*BPW) return (false); // throw(Exception_BadSubscript); else { s[b/BPW] &= ~(1UL << b%BPW); return (false); } } //----------------------------------------------------------------------------- // DrtBitVectImpl::reset() // Resets all the bits of bit vector 's' of 'sz' bits to false. //----------------------------------------------------------------------------- /*static*/ void DrtBitVectImpl::reset(unsigned long *s, int sz) { #if DrtBitVectImpl_VERS/100 != 1 #error DrtBitVectImpl_VERS has changed #endif #if DrtBitVect_TEST printf("Impl::reset(%08p:%d)\n", s, sz); #endif // Clear all bits of bit vector for (int i = 0; i < sz; i++) s[i] = 0; } //----------------------------------------------------------------------------- // DrtBitVectImpl::copy() // Copies bit vector 'o' of 'sz' bits to 's'. //----------------------------------------------------------------------------- /*static*/ void DrtBitVectImpl::copy(unsigned long *s, int sz, const unsigned long *o) { #if DrtBitVectImpl_VERS/100 != 1 #error DrtBitVectImpl_VERS has changed #endif #if DrtBitVect_TEST printf("Impl::copy(%08p:%d, %08p)\n", s, sz, o); #endif // Copy all bits of bit vector for (int i = 0; i < sz; i++) s[i] = o[i]; } //----------------------------------------------------------------------------- // DrtBitVectImpl::assign() // Retrieves assignable bit 'b' from bit vector 's' of 'sz' bits. // // Returns // Assignable bit object, representing bit 'b' of bit vector 's'. //----------------------------------------------------------------------------- /*static*/ DrtBitVectAsg DrtBitVectImpl::assign(unsigned long *s, int sz, int b) { #if DrtBitVectImpl_VERS/100 != 1 #error DrtBitVectImpl_VERS has changed #endif #if DrtBitVect_TEST printf("Impl::assign(%08p:%d, %d)\n", s, sz, b); #endif // Get assignable bit 'b' of bit vector if (b < 0 or b >= sz*BPW) return (DrtBitVectAsg(&s_dummy, 0)); // throw(Exception_BadSubscript); else return (DrtBitVectAsg(&s[b/BPW], 1UL << b%BPW)); } // Namespace #if !NO_NAMESPACES } // namespace Drt #endif // Settings for Emacs: // Local Variables: // tab-width: 8 // End: // End bitvect.cpp