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

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. More...
 
size_t tondx (size_type n) const
 Find the index in the base vector where entry
starts. More...
 
int tooff (size_type n) const
 Find the bit offset of entry
within its entry in the base vector. More...
 
value_type doget (size_type ndx, int off) const
 Return the entry at base index ndx/offset off. More...
 
void doset (size_type ndx, int off, value_type v)
 Set the entry at base index ndx/offset off to v. More...
 
void range_check (size_type n) const
 Check that n is in range and throw out_of_range if not. More...
 

Private Attributes

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

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 143 of file PackedArray.cxx.

145  : m_bitsize (bitsize),
146  m_size (0),
147  m_mask (mask (bitsize)),
148  m_vec (allocator)
149 {
150  assert (m_bitsize > 0 && m_bitsize <= nper);
151 }

◆ 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 163 of file PackedArray.cxx.

167  : m_bitsize (bitsize),
168  m_size (n),
169  m_mask (mask (bitsize)),
170  m_vec (nbase(n), 0, allocator)
171 {
172  assert (m_bitsize > 0 && m_bitsize <= nper);
173  if (val != 0) {
174  for (size_type i = 0; i < n; i++)
175  set (i, val);
176  }
177 }

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 185 of file PackedArray.cxx.

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

◆ 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 360 of file PackedArray.cxx.

361 {
362  range_check (n);
363  return proxy (*this, n);
364 }

◆ 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 345 of file PackedArray.cxx.

346 {
347  range_check (n);
348  return doget (tondx (n), tooff (n));
349 }

◆ 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 413 of file PackedArray.cxx.

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

◆ 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 387 of file PackedArray.cxx.

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

◆ bitsize()

int CxxUtils::PackedArray::bitsize ( ) const

Return the bitsize of the container.

Definition at line 486 of file PackedArray.cxx.

487 {
488  return m_bitsize;
489 }

◆ 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 228 of file PackedArray.cxx.

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

◆ clear()

void CxxUtils::PackedArray::clear ( )

Erase all the elements in the collection.

Definition at line 461 of file PackedArray.cxx.

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

◆ 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 83 of file PackedArray.cxx.

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

◆ 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 110 of file PackedArray.cxx.

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

◆ empty()

bool CxxUtils::PackedArray::empty ( ) const

Returns true if the collection is empty.

Definition at line 271 of file PackedArray.cxx.

272 {
273  return m_size == 0;
274 }

◆ 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 400 of file PackedArray.cxx.

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

◆ 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 374 of file PackedArray.cxx.

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

◆ 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 292 of file PackedArray.cxx.

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

◆ get_allocator()

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

Returns the allocator of the underlying vector.

Definition at line 200 of file PackedArray.cxx.

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

◆ max_size()

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

Returns the size() of the largest possible collection.

Definition at line 218 of file PackedArray.cxx.

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

◆ 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 48 of file PackedArray.cxx.

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

◆ 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 331 of file PackedArray.cxx.

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

◆ 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 317 of file PackedArray.cxx.

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

◆ pop_back()

void CxxUtils::PackedArray::pop_back ( )

Remove the last element from the collection.

Definition at line 436 of file PackedArray.cxx.

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

◆ 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 423 of file PackedArray.cxx.

424 {
425  ++m_size;
426  size_t nb = nbase (m_size);
427  if (nb != m_vec.size())
428  m_vec.resize (nb);
429  doset (tondx (m_size-1), tooff (m_size-1), x);
430 }

◆ 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 128 of file PackedArray.cxx.

129 {
130  if (n >= m_size) {
131  throw std::out_of_range ("PackedArray");
132  }
133 }

◆ 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 282 of file PackedArray.cxx.

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

◆ 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 239 of file PackedArray.cxx.

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

◆ 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 303 of file PackedArray.cxx.

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

◆ 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 474 of file PackedArray.cxx.

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

◆ size()

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

Returns the number of elements in the collection.

Definition at line 209 of file PackedArray.cxx.

210 {
211  return m_size;
212 }

◆ swap()

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

Swap this collection with another.

Parameters
otherThe collection with which to swap.

Definition at line 449 of file PackedArray.cxx.

450 {
451  std::swap (m_bitsize, other.m_bitsize);
452  std::swap (m_size, other.m_size);
453  std::swap (m_mask, other.m_mask);
454  std::swap (m_vec, other.m_vec);
455 }

◆ 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 59 of file PackedArray.cxx.

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

◆ 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 70 of file PackedArray.cxx.

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

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:
CxxUtils::PackedArray::m_vec
basetype m_vec
Underlying vector holding the data.
Definition: PackedArray.h:285
fitman.sz
sz
Definition: fitman.py:527
StateLessPT_NewConfig.proxy
proxy
Definition: StateLessPT_NewConfig.py:392
CxxUtils::PackedArray::nbase
size_t nbase(size_type n) const
Calculate the number of entries in the base vector needed to hold entries with the current bitsize.
Definition: PackedArray.cxx:48
CxxUtils::PackedArray::m_size
size_type m_size
The current number of entries in the container.
Definition: PackedArray.h:279
CxxUtils::PackedArray::tooff
int tooff(size_type n) const
Find the bit offset of entry within its entry in the base vector.
Definition: PackedArray.cxx:70
x
#define x
CxxUtils::PackedArray::bitsize
int bitsize() const
Return the bitsize of the container.
Definition: PackedArray.cxx:486
Trk::u
@ u
Enums for curvilinear frames.
Definition: ParamDefs.h:77
python.utils.AtlRunQueryLookup.mask
string mask
Definition: AtlRunQueryLookup.py:460
CxxUtils::PackedArray::doget
value_type doget(size_type ndx, int off) const
Return the entry at base index ndx/offset off.
Definition: PackedArray.cxx:83
lumiFormat.i
int i
Definition: lumiFormat.py:85
beamspotman.n
n
Definition: beamspotman.py:731
CxxUtils::PackedArray::range_check
void range_check(size_type n) const
Check that n is in range and throw out_of_range if not.
Definition: PackedArray.cxx:128
WriteCalibToCool.swap
swap
Definition: WriteCalibToCool.py:94
CxxUtils::PackedArray::m_bitsize
int m_bitsize
The current bitsize of the container.
Definition: PackedArray.h:276
CxxUtils::PackedArray::m_mask
value_type m_mask
Mask with m_bitsize bits set.
Definition: PackedArray.h:282
python.PyAthena.v
v
Definition: PyAthena.py:154
CxxUtils::PackedArray::tondx
size_t tondx(size_type n) const
Find the index in the base vector where entry starts.
Definition: PackedArray.cxx:59
InDetDD::other
@ other
Definition: InDetDD_Defs.h:16
Pythia8_RapidityOrderMPI.val
val
Definition: Pythia8_RapidityOrderMPI.py:14
CxxUtils::PackedArray::set
void set(size_type n, value_type val)
Set the entry at index n.
Definition: PackedArray.cxx:303
CxxUtils::PackedArray::doset
void doset(size_type ndx, int off, value_type v)
Set the entry at base index ndx/offset off to v.
Definition: PackedArray.cxx:110
hotSpotInTAG.nb
nb
Definition: hotSpotInTAG.py:164
value_type
Definition: EDM_MasterSearch.h:11
python.compressB64.c
def c
Definition: compressB64.py:93
CxxUtils::PackedArray::size_type
size_t size_type
Definition: PackedArray.h:49