-
Notifications
You must be signed in to change notification settings - Fork 0
/
Expressions.cpp
184 lines (154 loc) · 4.39 KB
/
Expressions.cpp
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include <string.h>
#include <iostream>
#include <typeinfo>
#include "Expressions.hpp"
// Constructors
LetterExpr::LetterExpr(char letter) : letter(letter)
{
_hash = std::hash_value(letter);
sharp_height=0;
};
// Second constructor: a copy constructor which performs a memcopy of the field sons
ConcatExpr::ConcatExpr(const ConcatExpr & other) : sonsNb(other.sonsNb)
{
_hash = other._hash;
sons = (const ExtendedExpression **)malloc(sonsNb * sizeof(void *));
memcpy(sons,other.sons,sonsNb * sizeof(void *));
char m=0;
for(int i=0;i<sonsNb;i++){
if (sons[i]->sharp_height>m) m=sons[i]->sharp_height;
}
sharp_height=m;
}
// Concatenation constructor: creates the concatenation of two expressions
ConcatExpr::ConcatExpr(const ExtendedExpression * expr_left, const ExtendedExpression * expr_right)
{
// Casts
const ConcatExpr * ConcatExprLeft = isConcatExpr(expr_left);
const ConcatExpr * ConcatExprRight = isConcatExpr(expr_right);
int subtrees_nb_left = (ConcatExprLeft != NULL) ? ConcatExprLeft->sonsNb : 1;
sonsNb = subtrees_nb_left + ((ConcatExprRight != NULL) ? ConcatExprRight->sonsNb : 1);
/* temporary array used to create all infixes */
sons = (const ExtendedExpression **)malloc(sonsNb * sizeof(void *));
/* copy the expressions in the array*/
if (ConcatExprLeft != NULL)
memcpy(sons, ConcatExprLeft->sons, ConcatExprLeft->sonsNb * sizeof(void*));
else
sons[0] = expr_left;
if (ConcatExprRight != NULL)
memcpy(sons + subtrees_nb_left, ConcatExprRight->sons, ConcatExprRight->sonsNb * sizeof(void*));
else
sons[subtrees_nb_left] = expr_right;
if (expr_left->sharp_height > expr_right->sharp_height)
sharp_height=expr_left->sharp_height;
else
sharp_height=expr_right->sharp_height;
update_hash();
}
// This is an assignment operator which performs a memcopy of the field sons
ConcatExpr & ConcatExpr::operator=(const ConcatExpr & other)
{
if (this != &other)
{
sonsNb = other.sonsNb;
_hash = other._hash;
sons = (const ExtendedExpression **)malloc(sonsNb * sizeof(void *));
memcpy(sons, other.sons, sonsNb * sizeof(void *));
sharp_height=other.sharp_height;
}
return *this;
}
// Adds a son to the left
// Assumes that enough memory was allocated when creating the son's pointer
/*
void ConcatExpr::addLeftSon(const ExtendedExpression * new_son)
{
*(sons + sonsNb) = new_son;
sonsNb++;
//_hash ^= hash_value(new_son->Hash()) + 0x9e3779b9 + (_hash << 6) + (_hash >> 2);
update_hash();
}
*/
SharpedExpr::SharpedExpr(const ExtendedExpression * son) : son(son)
{
_hash = std::hash_value(son->Hash());
sharp_height=1+son->sharp_height;
}
// Equality operator
bool ExtendedExpression::operator == (const ExtendedExpression & exp) const
{
const ExtendedExpression * pexp = &exp;
if (typeid(this) != typeid(pexp))
return false;
const SharpedExpr * sexpr = isSharpedExpr(this);
if (sexpr != NULL)
return (*sexpr == *(SharpedExpr *)pexp);
else
{
const ConcatExpr * cexpr = isConcatExpr(this);
if (cexpr != NULL)
return (*cexpr == *(ConcatExpr *)pexp);
else
return *(LetterExpr *)this == *(LetterExpr *)pexp;
}
}
std::ostream& operator<<(std::ostream& os, const ExtendedExpression & expr){ expr.print(os); return os; };
// Dynamic casts
const SharpedExpr * isSharpedExpr(const ExtendedExpression * expr) { return dynamic_cast<const SharpedExpr *>(expr); }
const ConcatExpr * isConcatExpr(const ExtendedExpression * expr) { return dynamic_cast<const ConcatExpr *>(expr); }
const LetterExpr * isLetterExpr(const ExtendedExpression * expr){ return dynamic_cast<const LetterExpr *>(expr); }
//Sharp-height
/*
char LetterExpr::sharp_height() const
{
return 0;
}
char ConcatExpr::sharp_height() const
{
char maxs=0, temp;
for (uint i = sonsNb ; i > 0; i--){
temp=sons[i -1]->sharp_height();
if (temp>maxs) maxs=temp;
}
return maxs;
}
char SharpedExpr::sharp_height() const
{
return 1+son->sharp_height();
}
*/
// Printing functions
using namespace std;
void LetterExpr::print(std::ostream& os) const
{
if(letter < 'a')
os << (char)('a' + letter);
else
os << (char) letter;
}
void ConcatExpr::print(std::ostream& os) const
{
//was reversed, put the normal order again
for (uint i = 0 ; i <sonsNb; i++)
sons[i]->print(os);
}
void SharpedExpr::print(std::ostream& os) const
{
if (isLetterExpr(son))
{
son->print(os);
os << "#";
}
else
{
os << "(";
son->print(os);
os << ")^#";
}
}
// Free the memory
ConcatExpr::~ConcatExpr()
{
free(sons);
sons = NULL;
}