00001
00002
00003 #include "orbsvcs/Notify/Bit_Vector.h"
00004
00005 TAO_BEGIN_VERSIONED_NAMESPACE_DECL
00006
00007 namespace TAO_Notify
00008 {
00009
00010 Bit_Vector::Bit_Vector()
00011 : size_(0)
00012 , first_set_bit_(0)
00013 , first_cleared_bit_(0)
00014 {
00015 }
00016
00017 Bit_Vector::~Bit_Vector()
00018 {
00019 }
00020
00021 bool
00022 Bit_Vector::is_set(const size_t location) const
00023 {
00024 bool result = false;
00025 if (location < this->size_)
00026 {
00027 result = (0 != (this->bitvec_[location >> BPW_LOG_2] & (1 << (location % BITS_PER_WORD))));
00028 }
00029 return result;
00030 }
00031
00032 void
00033 Bit_Vector::set_bit(const size_t location, bool set)
00034 {
00035 if (location >= this->size_)
00036 {
00037 if ((location >> BPW_LOG_2) >= (this->size_ >> BPW_LOG_2))
00038 {
00039 size_t need = (location >> BPW_LOG_2) - (this->size_ >> BPW_LOG_2);
00040 this->bitvec_.resize(this->bitvec_.size() + need + 1, 0);
00041 }
00042 this->size_ = location + 1;
00043 }
00044 if (set)
00045 {
00046 this->bitvec_[location >> BPW_LOG_2] |= (1 << (location % BITS_PER_WORD));
00047 }
00048 else
00049 {
00050 this->bitvec_[location >> BPW_LOG_2] &= ~(1 << (location % BITS_PER_WORD));
00051 }
00052 this->evaluate_firsts(location, set);
00053 }
00054
00055 size_t
00056 Bit_Vector::find_first_bit(bool set) const
00057 {
00058 size_t result = 0;
00059 if (set)
00060 {
00061 result = this->first_set_bit_;
00062 }
00063 else
00064 {
00065 result = this->first_cleared_bit_;
00066 }
00067 return result;
00068 }
00069
00070 void
00071 Bit_Vector::evaluate_firsts(const size_t location, bool set)
00072 {
00073 if (set)
00074 {
00075 if (this->first_cleared_bit_ == location)
00076 {
00077 this->first_cleared_bit_ = this->find_first_bit_of(location, false);
00078 }
00079 if (this->first_set_bit_ > location)
00080 {
00081 this->first_set_bit_ = location;
00082 }
00083 }
00084 else if (!set)
00085 {
00086 if (this->first_set_bit_ == location)
00087 {
00088 this->first_set_bit_ = this->find_first_bit_of(location, true);
00089 }
00090 if (this->first_cleared_bit_ > location)
00091 {
00092 this->first_cleared_bit_ = location;
00093 }
00094 }
00095 }
00096
00097 size_t
00098 Bit_Vector::find_first_bit_of(const size_t location, bool set)
00099 {
00100 size_t newloc = 0;
00101 size_t idx = 0;
00102 for (idx = location; (newloc == 0) && (idx < this->size_ + 1); idx++)
00103 {
00104 if (is_set(idx) == set)
00105 {
00106 newloc = idx;
00107 }
00108 }
00109 return newloc;
00110 }
00111
00112 }
00113
00114 TAO_END_VERSIONED_NAMESPACE_DECL