-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcl_base.h
143 lines (106 loc) · 3.88 KB
/
cl_base.h
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
//cl_base.h
/*
Copyright (C) 2013 AIDEUS
authors: Sergey Rodionov ([email protected])
Alexey Potapov ([email protected])
aideus.com
This file is part of cl-lab.
cl-lab is released under the GNU General Public License (GNU GPL).
Please read the included file COPYING for more information.
*/
#ifndef __SEGER__CL_BASE_H__
#define __SEGER__CL_BASE_H__
#include <list>
#include <string>
#include <cstdlib>
#include <iostream>
#include "cl_resultator.h"
#if defined _WIN32 || defined _WIN64
#include <time.h>
#define random rand
#define srandom srand
#endif
using namespace std;
class cl_termstorage;
class cl_term
{
public:
cl_term(char term_, int* counter); //create empty
//copy all tree and make delaying copy (is_delayed must be true)
cl_term(cl_term&, bool is_delayed);
//make simple copy from constant term
cl_term(const cl_term&);
cl_term(string s, int* counter);
~cl_term();
//convert to string (recursively, so result is attached to str)
//max_len --- maximal lenght of the string (0 - no limits, but could be dangerous)
//return false if hit max_len
bool rec_conv2str(string &str, size_t max_len = 0) const;
string conv2str(size_t max_len = 0) const;
//make full reduction
//0 --- we finish but resultat haven't obtained or we wanted full reduction with
// resultator = NULL
//1 --- resultator told that we finish, so we stoped
//-1 --- resultator told that we fail, so we stoped
//-2 --- we hit step limit
//-3 --- we hit memory limit
int reduce_all(int max_steps, int max_mem, cl_resultator* resultator = NULL);
//try to make one step of reduction
//0 - cannot reduce on this level
//1 - success
//< 0 - some limit was hit (reduction incomplite)
int reduce_step(int& steps_left, int max_mem);
//can return only < 0 if hits limit or 0
int one_level_reduce(int& steps_left, int max_mem);
int reduce_all_(int& steps_left, int max_mem, cl_resultator* resultator = NULL);
int count_nonterm_nodes();
int get_nonterm_node(cl_term** term, int n);
void crossover_a(cl_term* a);
//count number of sub_terms (except this term)
int count_subterms();
//get sub term
//0 -- return this term
//num can be from 0 to nsubterms including
//(should be call strictly after count_subterms)
cl_term* get_subterm(int num);
//make term exchange crossover
void crossover_s1(cl_term*);
void exchange_mutation(double p, string alphabet);
//make only one trim at once!!!
void trim_mutation(double p);
//add simple postfix
void add_postfix(string postfix);
//is this term is difficult enough to make delayed copy?
bool is_nontrivial();
private:
void install_counter_null_ts(int* counter);
void init_from_term(const cl_term&); //only copy term and chain!
void apply_S(); //Sabc ac(bc)
void apply_K(); //Kab a
void apply_I(); //Ia a
void apply_B(); //Babc a(bc)
void apply_C(); //Cabc acb
void apply_W(); //Wab abb
void apply_Y(); //Yx x(Yx)
void apply_M(); //Mx xx
void apply_T(); //Txy yx
void apply_J(); //Jxyzv xy(xvz)
void apply_b(); //B'xyz y(xz)
void apply_V(); //Vxyz zxy
//Fabcd if (c == d) "a" else "b"
//cd have to be fully reduced
//return < 0 if some limit was hit during reduction of c or d
int apply_F(int& steps_left, int max_mem);
void replace_this_with_a(cl_term* a);
//recursively create term
void rec_create_term(string s, size_t& pos);
friend class cl_termstorage;
private:
char term;
list<cl_term*> chain; // to which this term is applied
//number of subterms. Internal TEMPORAL variable which we use during crosover
int nsubterms;
int * counter; //internal variable for reduce_all (memory control)
cl_termstorage* ts;
};
#endif