Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2929_PrioritizedGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.PGE/3.3/src/go-symexpr/types.go @ 16620

Last change on this file since 16620 was 16620, checked in by hmaislin, 5 years ago

#2929: Reorganized folder structure for make script, removed explicit marshalling, erased go-side logging

File size: 6.6 KB
Line 
1package symexpr
2
3import "sort"
4
5type Leaf struct {
6  ExprStats
7}
8
9type Unary struct {
10  ExprStats
11  C Expr
12}
13
14type Binary struct {
15  ExprStats
16  C1, C2 Expr
17}
18
19type N_ary struct {
20  ExprStats
21  CS []Expr
22}
23
24// Leaf Nodes
25type Time struct {
26  Leaf
27}
28
29func NewTime() *Time               { return new(Time) }
30func (t *Time) ExprType() ExprType { return TIME }
31func (t *Time) Clone() Expr        { return NewTime() }
32
33type Var struct {
34  Leaf
35  P int
36}
37
38func NewVar(i int) *Var {
39  v := new(Var)
40  v.P = i
41  return v
42}
43func (v *Var) ExprType() ExprType { return VAR }
44func (v *Var) Clone() Expr        { return NewVar(v.P) }
45
46type Constant struct {
47  Leaf
48  P int
49}
50
51func NewConstant(i int) *Constant {
52  c := new(Constant)
53  c.P = i
54  return c
55}
56func (c *Constant) ExprType() ExprType { return CONSTANT }
57func (c *Constant) Clone() Expr        { return NewConstant(c.P) }
58
59type ConstantF struct {
60  Leaf
61  F float64
62}
63
64func NewConstantF(f float64) *ConstantF {
65  c := new(ConstantF)
66  c.F = f
67  return c
68}
69func (c *ConstantF) ExprType() ExprType { return CONSTANTF }
70func (c *ConstantF) Clone() Expr        { return NewConstantF(c.F) }
71
72type System struct {
73  Leaf
74  P int
75}
76
77func NewSystem(i int) *System {
78  s := new(System)
79  s.P = i
80  return s
81}
82func (s *System) ExprType() ExprType { return SYSTEM }
83func (s *System) Clone() Expr        { return NewSystem(s.P) }
84
85// Unary Operators
86type Neg struct {
87  Unary
88}
89
90func NewNeg(e Expr) *Neg {
91  n := new(Neg)
92  n.C = e
93  return n
94}
95func (u *Neg) ExprType() ExprType { return NEG }
96func (u *Neg) Clone() Expr {
97  var C Expr
98  if u.C != nil {
99    C = u.C.Clone()
100  }
101  return NewNeg(C)
102}
103
104type Abs struct {
105  Unary
106}
107
108func NewAbs(e Expr) *Abs {
109  n := new(Abs)
110  n.C = e
111  return n
112}
113func (u *Abs) ExprType() ExprType { return ABS }
114func (u *Abs) Clone() Expr {
115  var C Expr
116  if u.C != nil {
117    C = u.C.Clone()
118  }
119  return NewAbs(C)
120}
121
122type Sqrt struct {
123  Unary
124}
125
126func NewSqrt(e Expr) *Sqrt {
127  n := new(Sqrt)
128  n.C = e
129  return n
130}
131func (u *Sqrt) ExprType() ExprType { return SQRT }
132func (u *Sqrt) Clone() Expr {
133  var C Expr
134  if u.C != nil {
135    C = u.C.Clone()
136  }
137  return NewSqrt(C)
138}
139
140type Sin struct {
141  Unary
142}
143
144func NewSin(e Expr) *Sin {
145  n := new(Sin)
146  n.C = e
147  return n
148}
149func (u *Sin) ExprType() ExprType { return SIN }
150func (u *Sin) Clone() Expr {
151  var C Expr
152  if u.C != nil {
153    C = u.C.Clone()
154  }
155  return NewSin(C)
156}
157
158type Cos struct {
159  Unary
160}
161
162func NewCos(e Expr) *Cos {
163  n := new(Cos)
164  n.C = e
165  return n
166}
167func (u *Cos) ExprType() ExprType { return COS }
168func (u *Cos) Clone() Expr {
169  var C Expr
170  if u.C != nil {
171    C = u.C.Clone()
172  }
173  return NewCos(C)
174}
175
176type Tan struct {
177  Unary
178}
179
180func NewTan(e Expr) *Tan {
181  n := new(Tan)
182  n.C = e
183  return n
184}
185func (u *Tan) ExprType() ExprType { return TAN }
186func (u *Tan) Clone() Expr {
187  var C Expr
188  if u.C != nil {
189    C = u.C.Clone()
190  }
191  return NewTan(C)
192}
193
194type Exp struct {
195  Unary
196}
197
198func NewExp(e Expr) *Exp {
199  n := new(Exp)
200  n.C = e
201  return n
202}
203func (u *Exp) ExprType() ExprType { return EXP }
204func (u *Exp) Clone() Expr {
205  var C Expr
206  if u.C != nil {
207    C = u.C.Clone()
208  }
209  return NewExp(C)
210}
211
212type Log struct {
213  Unary
214}
215
216func NewLog(e Expr) *Log {
217  n := new(Log)
218  n.C = e
219  return n
220}
221func (u *Log) ExprType() ExprType { return LOG }
222func (u *Log) Clone() Expr {
223  var C Expr
224  if u.C != nil {
225    C = u.C.Clone()
226  }
227  return NewLog(C)
228}
229
230// Hmmm... Operators
231type PowI struct {
232  ExprStats
233  Base  Expr
234  Power int
235}
236
237func NewPowI(e Expr, i int) *PowI {
238  n := new(PowI)
239  if e != nil {
240    n.Base = e.Clone()
241  }
242  n.Power = i
243  return n
244}
245func (u *PowI) ExprType() ExprType { return POWI }
246func (u *PowI) Clone() Expr        { return NewPowI(u.Base, u.Power) }
247
248type PowF struct {
249  ExprStats
250  Base  Expr
251  Power float64
252}
253
254func NewPowF(b Expr, f float64) *PowF {
255  n := new(PowF)
256  n.Base = b
257  n.Power = f
258  return n
259}
260func (u *PowF) ExprType() ExprType { return POWF }
261func (u *PowF) Clone() Expr {
262  var base Expr
263  if u.Base != nil {
264    base = u.Base.Clone()
265  }
266  return NewPowF(base, u.Power)
267}
268
269type PowE struct {
270  ExprStats
271  Base  Expr
272  Power Expr
273}
274
275func NewPowE(b, p Expr) *PowE {
276  n := new(PowE)
277  n.Base = b
278  n.Power = p
279  return n
280}
281func (n *PowE) ExprType() ExprType { return POWE }
282func (n *PowE) Clone() Expr {
283  var base, pow Expr
284  if n.Base != nil {
285    base = n.Base.Clone()
286  }
287  if n.Power != nil {
288    pow = n.Power.Clone()
289  }
290  return NewPowE(base, pow)
291}
292
293type Div struct {
294  ExprStats
295  Numer Expr
296  Denom Expr
297}
298
299func NewDiv(n, d Expr) *Div {
300  D := new(Div)
301  D.Numer = n
302  D.Denom = d
303  return D
304}
305func (n *Div) ExprType() ExprType { return DIV }
306func (n *Div) Clone() Expr {
307  var N, D Expr
308  if n.Numer != nil {
309    N = n.Numer.Clone()
310  }
311  if n.Denom != nil {
312    D = n.Denom.Clone()
313  }
314  return NewDiv(N, D)
315}
316
317// N-ary Operators
318type Add struct {
319  N_ary
320}
321
322func NewAdd() *Add {
323  a := new(Add)
324  a.CS = make([]Expr, 0)
325  return a
326}
327func (n *Add) ExprType() ExprType { return ADD }
328
329func (n *Add) Clone() Expr {
330  a := new(Add)
331  a.CS = make([]Expr, len(n.CS))
332  for i, C := range n.CS {
333    if C != nil {
334      a.CS[i] = C.Clone()
335    }
336  }
337  return a
338}
339func (a *Add) Insert(e Expr) {
340  if len(a.CS) == cap(a.CS) {
341    tmp := make([]Expr, len(a.CS), 2*len(a.CS))
342    copy(tmp[:len(a.CS)], a.CS)
343    a.CS = tmp
344  }
345  a.CS = append(a.CS, e)
346  sort.Sort(a)
347}
348
349type Mul struct {
350  N_ary
351}
352
353func NewMul() *Mul {
354  m := new(Mul)
355  m.CS = make([]Expr, 0)
356  return m
357}
358func (n *Mul) ExprType() ExprType { return MUL }
359func (n *Mul) Clone() Expr {
360  a := NewMul()
361  a.CS = make([]Expr, len(n.CS))
362  for i, C := range n.CS {
363    if C != nil {
364      a.CS[i] = C.Clone()
365    }
366  }
367  return a
368}
369func (a *Mul) Insert(e Expr) {
370  if len(a.CS) == cap(a.CS) {
371    tmp := make([]Expr, len(a.CS), 2*len(a.CS))
372    copy(tmp[:len(a.CS)], a.CS)
373    a.CS = tmp
374  }
375  a.CS = append(a.CS, e)
376  sort.Sort(a)
377}
378
379func (n *Add) Len() int      { return len(n.CS) }
380func (n *Add) Swap(i, j int) { n.CS[i], n.CS[j] = n.CS[j], n.CS[i] }
381func (n *Add) Less(i, j int) bool {
382  if n.CS[i] == nil && n.CS[j] == nil {
383    return false
384  }
385  if n.CS[i] == nil {
386    return false
387  }
388  if n.CS[j] == nil {
389    return true
390  }
391  return n.CS[i].AmILess(n.CS[j])
392}
393
394func (n *Mul) Len() int      { return len(n.CS) }
395func (n *Mul) Swap(i, j int) { n.CS[i], n.CS[j] = n.CS[j], n.CS[i] }
396func (n *Mul) Less(i, j int) bool {
397  if n.CS[i] == nil && n.CS[j] == nil {
398    return false
399  }
400  if n.CS[i] == nil {
401    return false
402  }
403  if n.CS[j] == nil {
404    return true
405  }
406  return n.CS[i].AmILess(n.CS[j])
407}
Note: See TracBrowser for help on using the repository browser.