ATLAS Offline Software
Loading...
Searching...
No Matches
CxxUtils::PackedArray Class Reference

An array of unsigned values of some bit size, packed tightly. More...

#include <PackedArray.h>

Collaboration diagram for CxxUtils::PackedArray:

Classes

class  proxy
 proxy class for representing an lvalue to an element of PackedArray. More...

Public Types

typedef size_t size_type
typedef unsigned int value_type
typedef basetype::allocator_type allocator_type

Public Member Functions

 PackedArray (int bitsize=8, const allocator_type &allocator=allocator_type())
 Constructor.
 PackedArray (int bitsize, size_type n, value_type val=0, const allocator_type &allocator=allocator_type())
 Constructor.
void assign (size_type n, value_type u)
 Set the container to multiple copies of the same value.
allocator_type get_allocator () const
 Returns the allocator of the underlying vector.
size_type size () const
 Returns the number of elements in the collection.
size_type max_size () const
 Returns the size() of the largest possible collection.
size_type capacity () const
 Returns the total number of elements that the collection can hold before needing to allocate more memory.
void resize (size_type sz, value_type c=0)
 Resizes the collection to the specified number of elements.
bool empty () const
 Returns true if the collection is empty.
void reserve (size_type n)
 Attempt to preallocate enough memory for a specified number of elements.
value_type get (size_type n) const
 Return the entry at index n.
void set (size_type n, value_type val)
 Set the entry at index n.
value_type operator[] (size_type n) const
 Access an element, as an rvalue.
proxy operator[] (size_type n)
 Access an element, as an lvalue.
value_type at (size_type n) const
 Access an element, as an rvalue.
proxy at (size_type n)
 Access an element, as an lvalue.
value_type front () const
 Access the first element in the collection as an rvalue.
value_type back () const
 Access the last element in the collection as an rvalue.
proxy front ()
 Access the first element in the collection as an lvalue.
proxy back ()
 Access the last element in the collection as an lvalue.
void push_back (value_type x)
 Add an element to the end of the collection.
void pop_back ()
 Remove the last element from the collection.
void swap (PackedArray &other)
 Swap this collection with another.
void clear ()
 Erase all the elements in the collection.
void set_bitsize (int bitsize)
 Change the bitsize of the container.
int bitsize () const
 Return the bitsize of the container.

Private Types

typedef std::vector< unsigned int > basetype

Private Member Functions

size_t nbase (size_type n) const
 Calculate the number of entries in the base vector needed to hold
entries with the current bitsize.
size_t tondx (size_type n) const
 Find the index in the base vector where entry
starts.
int tooff (size_type n) const
 Find the bit offset of entry
within its entry in the base vector.
value_type doget (size_type ndx, int off) const
 Return the entry at base index ndx/offset off.
void doset (size_type ndx, int off, value_type v)
 Set the entry at base index ndx/offset off to v.
void range_check (size_type n) const
 Check that n is in range and throw out_of_range if not.

Private Attributes

int m_bitsize
 The current bitsize of the container.
size_type m_size
 The current number of entries in the container.
value_type m_mask
 Mask with m_bitsize bits set.
basetype m_vec
 Underlying vector holding the data.

Detailed Description

An array of unsigned values of some bit size, packed tightly.

When creating an instance of this class, specify the bit size of the entries. The entries will be packed so that each takes exactly that many bits. For example, if the bit size is 10, 16 entries will be packed into 5 32-bit words. The bitsize may be changed, but only if the container is empty.

Any values assigned to the array that are too large to be represented in the specified number of bits will have the high bits silently dropped.

The interface is modeled after std::vector, except that there are no iterators. They could be added if there is a need for them.

Definition at line 41 of file PackedArray.h.

Member Typedef Documentation

◆ allocator_type

typedef basetype::allocator_type CxxUtils::PackedArray::allocator_type

Definition at line 51 of file PackedArray.h.

◆ basetype

typedef std::vector<unsigned int> CxxUtils::PackedArray::basetype
private

Definition at line 45 of file PackedArray.h.

◆ size_type

Definition at line 49 of file PackedArray.h.

◆ value_type

typedef unsigned int CxxUtils::PackedArray::value_type

Definition at line 50 of file PackedArray.h.

Constructor & Destructor Documentation

◆ PackedArray() [1/2]

CxxUtils::PackedArray::PackedArray ( int bitsize = 8,
const allocator_type & allocator = allocator_type() )

Constructor.

Parameters
bitsizeThe size, in bits, of each element. Must be greater than zero, and not larger than the size of an unsigned int.
allocatorAllocator for the underlying vector.

Definition at line 144 of file PackedArray.cxx.

146 : m_bitsize (bitsize),
147 m_size (0),
148 m_mask (mask (bitsize)),
149 m_vec (allocator)
150{
151 assert (m_bitsize > 0 && m_bitsize <= nper);
152}
int m_bitsize
The current bitsize of the container.
size_type m_size
The current number of entries in the container.
value_type m_mask
Mask with m_bitsize bits set.
basetype m_vec
Underlying vector holding the data.
int bitsize() const
Return the bitsize of the container.

◆ PackedArray() [2/2]

CxxUtils::PackedArray::PackedArray ( int bitsize,
size_type n,
value_type val = 0,
const allocator_type & allocator = allocator_type() )

Constructor.

Parameters
bitsizeThe size, in bits, of each element. Must be greater than zero, and not larger than the size of an unsigned int.
nInitial number of entries in the container.
valValue to which the initial entries are to be set.
allocatorAllocator for the underlying vector.

Definition at line 164 of file PackedArray.cxx.

168 : m_bitsize (bitsize),
169 m_size (n),
170 m_mask (mask (bitsize)),
171 m_vec (nbase(n), 0, allocator)
172{
173 assert (m_bitsize > 0 && m_bitsize <= nper);
174 if (val != 0) {
175 for (size_type i = 0; i < n; i++)
176 set (i, val);
177 }
178}
size_t nbase(size_type n) const
Calculate the number of entries in the base vector needed to hold entries with the current bitsize.
void set(size_type n, value_type val)
Set the entry at index n.

Member Function Documentation

◆ assign()

void CxxUtils::PackedArray::assign ( size_type n,
value_type u )

Set the container to multiple copies of the same value.

Parameters
nNumber of entries to which the container is to be set.
uValue to which the entries are to be set.
nNumber of entries to which the container is to be set.
valValue to which the entries are to be set.

Definition at line 186 of file PackedArray.cxx.

187{
188 m_size = n;
189 m_vec.clear();
190 m_vec.resize (nbase(n));
191 if (u != 0) {
192 for (size_type i = 0; i < n; i++)
193 set (i, u);
194 }
195}

◆ at() [1/2]

PackedArray::proxy CxxUtils::PackedArray::at ( size_type n)

Access an element, as an lvalue.

Parameters
nArray index to access.
Returns
Proxy to the element at n.

Will raise std::out_of_range if the index is out-of-bounds. Note that we return a proxy object rather than a reference.

Definition at line 361 of file PackedArray.cxx.

362{
363 range_check (n);
364 return proxy (*this, n);
365}
proxy class for representing an lvalue to an element of PackedArray.
Definition PackedArray.h:58
void range_check(size_type n) const
Check that n is in range and throw out_of_range if not.

◆ at() [2/2]

PackedArray::value_type CxxUtils::PackedArray::at ( size_type n) const

Access an element, as an rvalue.

Parameters
nArray index to access.
Returns
The element at n.

Will raise std::out_of_range if the index is out-of-bounds. Note that we return a value_type rather than a reference.

Definition at line 346 of file PackedArray.cxx.

347{
348 range_check (n);
349 return doget (tondx (n), tooff (n));
350}
size_t tondx(size_type n) const
Find the index in the base vector where entry starts.
value_type doget(size_type ndx, int off) const
Return the entry at base index ndx/offset off.
int tooff(size_type n) const
Find the bit offset of entry within its entry in the base vector.

◆ back() [1/2]

PackedArray::proxy CxxUtils::PackedArray::back ( )

Access the last element in the collection as an lvalue.

Returns
Proxy to the last element in the collection.

No checking is done to ensure that the container is not empty. Note that we return a proxy object rather than a reference.

Definition at line 414 of file PackedArray.cxx.

415{
416 return proxy (*this, m_size-1);
417}

◆ back() [2/2]

PackedArray::value_type CxxUtils::PackedArray::back ( ) const

Access the last element in the collection as an rvalue.

Returns
The last element in the collection.

No checking is done to ensure that the container is not empty. Note that we return a value_type rather than a reference.

Definition at line 388 of file PackedArray.cxx.

389{
390 return doget (tondx (m_size-1), tooff (m_size-1));
391}

◆ bitsize()

int CxxUtils::PackedArray::bitsize ( ) const

Return the bitsize of the container.

Definition at line 487 of file PackedArray.cxx.

488{
489 return m_bitsize;
490}

◆ capacity()

PackedArray::size_type CxxUtils::PackedArray::capacity ( ) const

Returns the total number of elements that the collection can hold before needing to allocate more memory.

Definition at line 229 of file PackedArray.cxx.

230{
231 return m_vec.capacity() * nper / m_bitsize;
232}

◆ clear()

void CxxUtils::PackedArray::clear ( )

Erase all the elements in the collection.

Definition at line 462 of file PackedArray.cxx.

463{
464 m_size = 0;
465 m_vec.clear();
466}

◆ doget()

PackedArray::value_type CxxUtils::PackedArray::doget ( size_type ndx,
int off ) const
inlineprivate

Return the entry at base index ndx/offset off.

Parameters
ndxIndex of the entry in the base vector at which the packed entry starts.
offBit offset within that entry where the packed entry starts.

Definition at line 84 of file PackedArray.cxx.

85{
86 // Get the bits from the first entry.
87 value_type v = m_vec[ndx] >> off;
88
89 // If the packed entry wraps between two base entries, collect the bits
90 // from the next base entry.
91 if (m_bitsize > nper - off) {
92 int bits = m_bitsize - (nper - off);
93 // cppcheck-suppress shiftTooManyBits; false positive
94 // m_bitsize <= nper
95 v |= ((m_vec[ndx+1] & mask(bits)) << (nper - off));
96 }
97
98 // Mask down to the proper number of bits.
99 return v & m_mask;
100}
unsigned int value_type
Definition PackedArray.h:50

◆ doset()

void CxxUtils::PackedArray::doset ( size_type ndx,
int off,
value_type v )
inlineprivate

Set the entry at base index ndx/offset off to v.

Set the entry at base index ndx/offset off.

Parameters
ndxIndex of the entry in the base vector at which the packed entry starts.
offBit offset within that entry where the packed entry starts.
vValue to which the entry should be set.

Definition at line 111 of file PackedArray.cxx.

112{
113 // Set the bits in the first entry.
114 m_vec[ndx] = (m_vec[ndx] & ~(m_mask<<off)) | ((v&m_mask) << off);
115
116 // If the packed entry wraps between two base entries, set the bits
117 // in the next entry.
118 if (m_bitsize > nper - off) {
119 value_type mask2 = mask (m_bitsize - (nper - off));
120 m_vec[ndx+1] = (m_vec[ndx+1] & ~mask2) | ((v >> (nper - off)) & mask2);
121 }
122}

◆ empty()

bool CxxUtils::PackedArray::empty ( ) const

Returns true if the collection is empty.

Definition at line 272 of file PackedArray.cxx.

273{
274 return m_size == 0;
275}

◆ front() [1/2]

PackedArray::proxy CxxUtils::PackedArray::front ( )

Access the first element in the collection as an lvalue.

Returns
Proxy to the first element in the collection.

No checking is done to ensure that the container is not empty. Note that we return a proxy object rather than a reference.

Definition at line 401 of file PackedArray.cxx.

402{
403 return proxy (*this, 0);
404}

◆ front() [2/2]

PackedArray::value_type CxxUtils::PackedArray::front ( ) const

Access the first element in the collection as an rvalue.

Returns
The first element in the collection.

No checking is done to ensure that the container is not empty. Note that we return a value_type rather than a reference.

Definition at line 375 of file PackedArray.cxx.

376{
377 return doget (0, 0);
378}

◆ get()

PackedArray::value_type CxxUtils::PackedArray::get ( size_type n) const

Return the entry at index n.

Parameters
nThe index of the entry to retrieve.

Definition at line 293 of file PackedArray.cxx.

294{
295 return doget (tondx (n), tooff (n));
296}

◆ get_allocator()

PackedArray::allocator_type CxxUtils::PackedArray::get_allocator ( ) const

Returns the allocator of the underlying vector.

Definition at line 201 of file PackedArray.cxx.

202{
203 return m_vec.get_allocator();
204}

◆ max_size()

PackedArray::size_type CxxUtils::PackedArray::max_size ( ) const

Returns the size() of the largest possible collection.

Definition at line 219 of file PackedArray.cxx.

220{
221 return m_vec.max_size();
222}

◆ nbase()

PackedArray::size_type CxxUtils::PackedArray::nbase ( size_type n) const
inlineprivate

Calculate the number of entries in the base vector needed to hold
entries with the current bitsize.

Parameters
nNumber of packed entries desired.

Definition at line 49 of file PackedArray.cxx.

50{
51 return (n * m_bitsize + nper-1) / nper;
52}

◆ operator[]() [1/2]

PackedArray::proxy CxxUtils::PackedArray::operator[] ( size_type n)

Access an element, as an lvalue.

Parameters
nArray index to access.
Returns
Proxy to the element at n.

No bounds checking is done. Note that we return a proxy object rather than a reference.

Definition at line 332 of file PackedArray.cxx.

333{
334 return proxy (*this, n);
335}

◆ operator[]() [2/2]

PackedArray::value_type CxxUtils::PackedArray::operator[] ( size_type n) const

Access an element, as an rvalue.

Parameters
nArray index to access.
Returns
The element at n.

No bounds checking is done. Note that we return a value_type rather than a reference.

Definition at line 318 of file PackedArray.cxx.

319{
320 return doget (tondx (n), tooff (n));
321}

◆ pop_back()

void CxxUtils::PackedArray::pop_back ( )

Remove the last element from the collection.

Definition at line 437 of file PackedArray.cxx.

438{
439 --m_size;
440 size_t nb = nbase (m_size);
441 if (nb != m_vec.size())
442 m_vec.resize (nb);
443}

◆ push_back()

void CxxUtils::PackedArray::push_back ( value_type x)

Add an element to the end of the collection.

Parameters
xThe element to add to the collection.

Definition at line 424 of file PackedArray.cxx.

425{
426 ++m_size;
427 size_t nb = nbase (m_size);
428 if (nb != m_vec.size())
429 m_vec.resize (nb);
430 doset (tondx (m_size-1), tooff (m_size-1), x);
431}
#define x
void doset(size_type ndx, int off, value_type v)
Set the entry at base index ndx/offset off to v.

◆ range_check()

void CxxUtils::PackedArray::range_check ( size_type n) const
private

Check that n is in range and throw out_of_range if not.

Parameters
nIndex to check.

Definition at line 129 of file PackedArray.cxx.

130{
131 if (n >= m_size) {
132 throw_out_of_range (__PRETTY_FUNCTION__, n, m_size, this);
133 }
134}
void throw_out_of_range(const std::string &what, size_t index, size_t size, const void *obj)
Throw an out_of_range exception.

◆ reserve()

void CxxUtils::PackedArray::reserve ( size_type n)

Attempt to preallocate enough memory for a specified number of elements.

Parameters
nNumber of elements required.

Definition at line 283 of file PackedArray.cxx.

284{
285 m_vec.reserve (nbase (n));
286}

◆ resize()

void CxxUtils::PackedArray::resize ( size_type sz,
value_type c = 0 )

Resizes the collection to the specified number of elements.

Parameters
szThe new size of the collection.
cValue to which any new elements are to be set.

Definition at line 240 of file PackedArray.cxx.

241{
242 m_vec.resize (nbase (sz));
243 if (sz > m_size) {
244 // Making the container bigger. Need to fill the remaining values.
245 if (c != 0) {
246 // Set them to something non-zero.
247 for (size_t i = m_size; i < sz; i++)
248 set (i, c);
249 }
250 else {
251 // Filling the new entries with 0.
252 // New elements in the base vector will have been set to 0.
253 // However, we also need to zero out any remaining packed elements
254 // (or piece thereof) in the last base element that was occupied
255 // before the resize.
256 int off = tooff (m_size);
257 // Don't need to do anything if packed entries exactly fit
258 // in the allocated base size.
259 if (off != 0) {
260 size_t ndx = tondx (m_size);
261 m_vec[ndx] &= mask (off);
262 }
263 }
264 }
265 m_size = sz;
266}
static Double_t sz

◆ set()

void CxxUtils::PackedArray::set ( size_type n,
value_type val )

Set the entry at index n.

Parameters
nThe index of the entry to set.
valThe new value for the entry at index n.

Definition at line 304 of file PackedArray.cxx.

305{
306 return doset (tondx (n), tooff (n), val);
307}

◆ set_bitsize()

void CxxUtils::PackedArray::set_bitsize ( int bitsize)

Change the bitsize of the container.

bitsize The new bitsize.

This method may only be called when the container is empty.

Definition at line 475 of file PackedArray.cxx.

476{
477 assert (m_size == 0);
478 assert (bitsize > 0 && bitsize <= nper);
480 m_mask = mask (bitsize);
481}

◆ size()

PackedArray::size_type CxxUtils::PackedArray::size ( ) const

Returns the number of elements in the collection.

Definition at line 210 of file PackedArray.cxx.

211{
212 return m_size;
213}

◆ swap()

void CxxUtils::PackedArray::swap ( PackedArray & other)

Swap this collection with another.

Parameters
otherThe collection with which to swap.

Definition at line 450 of file PackedArray.cxx.

451{
452 std::swap (m_bitsize, other.m_bitsize);
453 std::swap (m_size, other.m_size);
454 std::swap (m_mask, other.m_mask);
455 std::swap (m_vec, other.m_vec);
456}
void swap(ElementLinkVector< DOBJ > &lhs, ElementLinkVector< DOBJ > &rhs)

◆ tondx()

PackedArray::size_type CxxUtils::PackedArray::tondx ( size_type n) const
inlineprivate

Find the index in the base vector where entry
starts.

Parameters
nPacked entry desired.

Definition at line 60 of file PackedArray.cxx.

61{
62 return (n * m_bitsize) / nper;
63}

◆ tooff()

int CxxUtils::PackedArray::tooff ( size_type n) const
inlineprivate

Find the bit offset of entry
within its entry in the base vector.

Parameters
nPacked entry desired.

Definition at line 71 of file PackedArray.cxx.

72{
73 return (n * m_bitsize) % nper;
74}

Member Data Documentation

◆ m_bitsize

int CxxUtils::PackedArray::m_bitsize
private

The current bitsize of the container.

Definition at line 276 of file PackedArray.h.

◆ m_mask

value_type CxxUtils::PackedArray::m_mask
private

Mask with m_bitsize bits set.

Definition at line 282 of file PackedArray.h.

◆ m_size

size_type CxxUtils::PackedArray::m_size
private

The current number of entries in the container.

Definition at line 279 of file PackedArray.h.

◆ m_vec

basetype CxxUtils::PackedArray::m_vec
private

Underlying vector holding the data.

Definition at line 285 of file PackedArray.h.


The documentation for this class was generated from the following files: