summaryrefslogtreecommitdiff
path: root/TAO/orbsvcs/orbsvcs/Notify/Bit_Vector.cpp
blob: 65fc33cde839fe87db20b891b2cbb341a69b2847 (plain)
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
// $Id$

#include "Bit_Vector.h"

namespace TAO_Notify
{

Bit_Vector::Bit_Vector()
  : size_(0)
  , first_set_bit_(0)
  , first_cleared_bit_(0)
{
}

Bit_Vector::~Bit_Vector()
{
}

bool
Bit_Vector::is_set(const size_t location) const
{
  bool result = false;
  if (location < this->size_)
  {
    result = (0 != (this->bitvec_[location >> BPW_LOG_2] & (1 << (location % BITS_PER_WORD))));
  }
  return result;
}

void
Bit_Vector::set_bit(const size_t location, bool set)
{
  if (location >= this->size_)
  {
    if ((location >> BPW_LOG_2) >= (this->size_ >> BPW_LOG_2))
    {
      size_t need = (location >> BPW_LOG_2) - (this->size_ >> BPW_LOG_2);
      this->bitvec_.resize(this->bitvec_.size() + need + 1, 0);
    }
    this->size_ = location + 1;
  }
  if (set)
  {
    this->bitvec_[location >> BPW_LOG_2] |= (1 << (location % BITS_PER_WORD));
  }
  else
  {
    this->bitvec_[location >> BPW_LOG_2] &= ~(1 << (location % BITS_PER_WORD));
  }
  this->evaluate_firsts(location, set);
}

size_t
Bit_Vector::find_first_bit(bool set) const
{
  size_t result = 0;
  if (set)
  {
    result = this->first_set_bit_;
  }
  else
  {
    result = this->first_cleared_bit_;
  }
  return result;
}

void
Bit_Vector::evaluate_firsts(const size_t location, bool set)
{
  if (set)
  {
    if (this->first_cleared_bit_ == location)
    {
      this->first_cleared_bit_ = this->find_first_bit_of(location, false);
    }
    if (this->first_set_bit_ > location)
    {
      this->first_set_bit_ = location;
    }
  }
  else if (!set)
  {
    if (this->first_set_bit_ == location)
    {
      this->first_set_bit_ = this->find_first_bit_of(location, true);
    }
    if (this->first_cleared_bit_ > location)
    {
      this->first_cleared_bit_ = location;
    }
  }
}

size_t
Bit_Vector::find_first_bit_of(const size_t location, bool set)
{
  size_t newloc = 0;
  size_t idx = 0;
  for (idx = location; (newloc == 0) && (idx < this->size_ + 1); idx++)
  {
    if (is_set(idx) == set)
    {
      newloc = idx;
    }
  }
  return newloc;
}

}

#if defined (ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION)
template class ACE_Vector<ACE_UINT32>;
template class ACE_Array_Base<ACE_UINT32>;
#elif defined (ACE_HAS_TEMPLATE_INSTANTIATION_PRAGMA)
#pragma instantiate ACE_Vector<ACE_UINT32>
#pragma instantiate ACE_Array_Base<ACE_UINT32>
#endif /* ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION */