-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPattern.cxx
162 lines (141 loc) · 4.07 KB
/
Pattern.cxx
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
//*-- Author : Ole Hansen, Jefferson Lab 19-Sep-2007
///////////////////////////////////////////////////////////////////////////////
// //
// TreeSearch::Pattern //
// //
///////////////////////////////////////////////////////////////////////////////
#include "Pattern.h"
#include <iostream>
using namespace std;
namespace TreeSearch {
//_____________________________________________________________________________
Pattern::Pattern( UInt_t size )
: fChild(0), fNbits(size), fDelBits(true), fDelChld(false)
{
// Constructor. If size > 0, create array for the bits on the heap.
assert( fNbits <= 16 );
if( fNbits == 0 ) {
fDelBits = false;
fBits = 0;
} else {
fBits = new UShort_t[fNbits];
memset( fBits, 0, fNbits*sizeof(UShort_t) );
}
}
//_____________________________________________________________________________
Pattern::Pattern( const Pattern& orig )
: fChild(0), fNbits(orig.fNbits), fDelBits(orig.fDelBits), fDelChld(false)
{
// Copy constructor. Copy has same bits, no children
if( fDelBits ) {
fBits = new UShort_t[fNbits];
memcpy( fBits, orig.fBits, fNbits*sizeof(UShort_t) );
} else {
fBits = orig.fBits;
}
}
//_____________________________________________________________________________
const Pattern& Pattern::operator=( const Pattern& rhs )
{
// Assignment. Copies only the bits, not the pointers to the children.
if( this != &rhs ) {
if( fDelChld ) {
while( fChild ) {
Link* child = fChild;
fChild = fChild->Next();
delete child;
}
}
fChild = 0;
fNbits = rhs.fNbits;
if( fDelBits )
delete [] fBits;
fDelBits = rhs.fDelBits;
fDelChld = false;
if( fDelBits ) {
if( fNbits ) {
fBits = new UShort_t[fNbits];
memcpy( fBits, rhs.fBits, fNbits*sizeof(UShort_t) );
} else {
fBits = 0;
}
} else {
fBits = rhs.fBits;
}
}
return *this;
}
//_____________________________________________________________________________
Pattern::~Pattern()
{
// Destroys a Pattern. Deletes all child links (but not the Patterns
// they point to)
if( fDelChld ) {
while( fChild ) {
Link* child = fChild;
fChild = fChild->Next();
delete child;
}
}
if( fDelBits )
delete [] fBits;
}
//_____________________________________________________________________________
Int_t Pattern::GetNchildren() const
{
// Returns number of child nodes of the pattern
Int_t n = 0;
Link* ln = fChild;
while( ln ) {
++n;
ln = ln->Next();
}
return n;
}
//_____________________________________________________________________________
void Pattern::Print( bool print_links, ostream& os, bool end_line ) const
{
// Print this pattern and, if requested, its child patterns
for( UInt_t i=0; i<fNbits; i++ ) {
os << fBits[i] << " ";
}
// os << "=" << fMinDepth;
if( print_links ) {
os << ": ";
Link* ln = fChild;
while( ln ) {
// Print each link
os << "(" << ln->Type() << ") ";
ln->GetPattern()->Print( false, os, false );
ln = ln->Next();
if( ln )
os << ", ";
}
}
if( end_line )
os << endl;
}
//_____________________________________________________________________________
void Pattern::SetBitloc( UShort_t* bitloc )
{
// Tell the pattern to store its bits at the externally-managed location
// bitloc. The current fBits[fNbits] will be copied to bitloc.
// If bitloc is NULL, revert to internally-managed bits.
UShort_t* oldbits = fBits;
if( bitloc ) {
fBits = bitloc;
if( oldbits ) {
memcpy( fBits, oldbits, fNbits*sizeof(UShort_t) );
if( fDelBits )
delete [] oldbits;
}
fDelBits = false;
} else if( !fDelBits ) {
fBits = new UShort_t[fNbits];
if( oldbits )
memcpy( fBits, oldbits, fNbits*sizeof(UShort_t) );
fDelBits = true;
}
}
///////////////////////////////////////////////////////////////////////////////
} // end namespace TreeSearch