ATLAS Offline Software
Loading...
Searching...
No Matches
TCS::Heap< T > Class Template Reference

#include <Heap.h>

Collaboration diagram for TCS::Heap< T >:

Classes

class  HeapStructure

Public Member Functions

 Heap (const std::string &name, size_t capacity=120)
 ~Heap ()
void clear ()
T * create (const T &obj)
 create an object on the heap
size_t size () const
size_t capacity () const

Private Member Functions

void * allocate (size_t size)
void deallocate (void *&mem)
void extend ()

Private Attributes

HeapStructure m_heap
std::string m_name {}
size_t m_originalCapacity {0}

Detailed Description

template<class T>
class TCS::Heap< T >

Definition at line 13 of file Heap.h.

Constructor & Destructor Documentation

◆ Heap()

template<class T>
TCS::Heap< T >::Heap ( const std::string & name,
size_t capacity = 120 )
inline

Definition at line 15 of file Heap.h.

15 :
16 m_name(name),
18 {
20 m_heap.pos = (T*)m_heap.heap;
21 }
size_t capacity() const
Definition Heap.h:62
std::string m_name
Definition Heap.h:76
void * allocate(size_t size)
Definition Heap.h:80
size_t m_originalCapacity
Definition Heap.h:77
HeapStructure m_heap
Definition Heap.h:75

◆ ~Heap()

template<class T>
TCS::Heap< T >::~Heap ( )
inline

Definition at line 23 of file Heap.h.

23 {
24 clear();
25 deallocate(m_heap.heap);
26 m_heap.heap = nullptr;
27 m_heap.pos = nullptr;
28 }
void clear()
Definition Heap.h:32
void deallocate(void *&mem)
Definition Heap.h:85

Member Function Documentation

◆ allocate()

template<class T>
void * TCS::Heap< T >::allocate ( size_t size)
inlineprivate

Definition at line 80 of file Heap.h.

80 {
81 return ::operator new ( size * sizeof(T) );
82 }
size_t size() const
Definition Heap.h:58

◆ capacity()

template<class T>
size_t TCS::Heap< T >::capacity ( ) const
inline

Definition at line 62 of file Heap.h.

62 {
63 return (m_heap.heapCollection.size() + 1) * m_originalCapacity;
64 }

◆ clear()

template<class T>
void TCS::Heap< T >::clear ( )
inline

Definition at line 32 of file Heap.h.

32 {
33 // destruct objects on the currently active memory block
34 T * p = (T*)m_heap.heap;
35 while(p != m_heap.pos) {
36 (p++)->~T();
37 }
38 m_heap.pos = (T*)m_heap.heap; // reset the position to the beginning of the active memory block
39 // destroy objects on all other memory blocks
40 for(void * heap : m_heap.heapCollection) {
41 T * p = (T*)heap;
42 for(unsigned int t=0; t<m_originalCapacity; ++t)
43 (p++)->~T();
44 deallocate(heap); // destroy other blocks
45 }
46 m_heap.heapCollection.clear(); // clear the collection of memory blocks
47 }

◆ create()

template<class T>
T * TCS::Heap< T >::create ( const T & obj)
inline

create an object on the heap

Definition at line 50 of file Heap.h.

50 {
51 if( (int)(m_heap.pos - (T*)m_heap.heap) == (int)m_originalCapacity ) {
52 extend();
53 }
54 T* newObject = new(m_heap.pos++) T(obj);
55 return newObject;
56 }
void extend()
Definition Heap.h:91

◆ deallocate()

template<class T>
void TCS::Heap< T >::deallocate ( void *& mem)
inlineprivate

Definition at line 85 of file Heap.h.

85 {
86 ::operator delete ( mem );
87 mem = nullptr;
88 }

◆ extend()

template<class T>
void TCS::Heap< T >::extend ( )
inlineprivate

Definition at line 91 of file Heap.h.

91 {
92 m_heap.heapCollection.push_back(m_heap.heap); // park the old heap which has grown too small
93 m_heap.heap = allocate(m_originalCapacity); // create a new heap the same size as the original one
94 m_heap.pos = (T*)m_heap.heap; // make current position point to it
95 }

◆ size()

template<class T>
size_t TCS::Heap< T >::size ( ) const
inline

Definition at line 58 of file Heap.h.

58 {
59 return (m_heap.pos - (T*)m_heap.heap) + m_heap.heapCollection.size() * m_originalCapacity;
60 }

Member Data Documentation

◆ m_heap

template<class T>
HeapStructure TCS::Heap< T >::m_heap
private

Definition at line 75 of file Heap.h.

◆ m_name

template<class T>
std::string TCS::Heap< T >::m_name {}
private

Definition at line 76 of file Heap.h.

76{};

◆ m_originalCapacity

template<class T>
size_t TCS::Heap< T >::m_originalCapacity {0}
private

Definition at line 77 of file Heap.h.

77{0};

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