-
Notifications
You must be signed in to change notification settings - Fork 0
/
Term.java
163 lines (117 loc) · 3.56 KB
/
Term.java
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
import java.util.*;
//list of factors
public class Term implements Comparable, Iterable{
private boolean sign;
private static boolean firstPushAfterExp;
private BinaryTreeList<Factor> factors;
private LinkedList<Constant> constants;
public boolean isEmpty(){
return factors.size() + constants.size() != 0;
}
public Factor addFactor(Factor f){
// System.out.println(f+" was added.");
if(f instanceof Constant)
constants.add((Constant)f);
else
factors.add(f);
// System.out.println("And the term looks like: "+this);
// System.out.println("The tree size is: " +factors.size());
return f;
}
public Iterator<Factor> iterator(){
return new Iterator<Factor>(){
Iterator<Constant> constIter = constants.iterator();
Iterator<Factor> factorIter = factors.iterator();
public boolean hasNext(){
if(constIter.hasNext())
return true;
else if(factorIter.hasNext())
return true;
return false;
}
public Factor next(){
if(constIter.hasNext())
return constIter.next();
else return factorIter.next();
}
public void remove(){
throw new UnsupportedOperationException();
}
};
}
public int size(){
return factors.size() + (constants != null ? constants.size() : 0);
}
public void addFactors(List<? extends Factor> factors){
Iterator<? extends Factor> iter = factors.iterator();
while(iter.hasNext())
addFactor(iter.next());
}
public void setSign(boolean sign){
this.sign = sign;
}
public List<Constant> constants(){
return constants;
}
public Term(){
factors = new BinaryTreeList<Factor>();
constants = new LinkedList<Constant>();
this.sign = true;
}
public Term(Term t){
factors = new BinaryTreeList<Factor>();
constants = new LinkedList<Constant>();
for(Factor f : factors.asList()){
factors.add(Factor.deepCopy(f));
}
}
public List<Factor> factors(){
return factors.asList();
}
public String toString(){
// System.out.println("In the Term toString() method.");
StringBuilder termString = new StringBuilder();
if(constants != null){
for(Constant c : constants)
termString.append(c.toString());
}
if(factors != null){
for(Factor f : factors.asList()){
// System.out.println("~Term: Appending the factors of this term.");
termString.append(f.toString());
}
}
return termString.toString();
}
public boolean getSign(){
return this.sign;
}
public int compareTo(Object o){
if(!(o instanceof Term))
throw new IllegalArgumentException("Illegal comparison.");
Term that = (Term)o;
Iterator<Factor> thatIter = that.factors().iterator();
Iterator<Factor> thisIter = factors().iterator();
if(this.size() == that.size()){
int elementCompare = 0;
while(thisIter.hasNext()){
elementCompare = thisIter.next().compareTo(thatIter.next());
if(elementCompare != 0)
return elementCompare;
return 0;
}
}
else if(this.size() < that.size())
return -1;
return 1;
}
public void removeFactor(Factor f){
factors.remove(f);
}
public List<Factor> allFactors(){
LinkedList<Factor> toReturn = new LinkedList<Factor>();
toReturn.addAll(constants);
toReturn.addAll(factors.asList());
return toReturn;
}
}