-
Notifications
You must be signed in to change notification settings - Fork 3
/
stringlist.e
270 lines (219 loc) · 5.75 KB
/
stringlist.e
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/*
string list class that manages the freeing of its own strings and hides access to the underlying list implementation
*/
OPT MODULE
EXPORT OBJECT stringlist
PRIVATE items:PTR TO LONG
PRIVATE initialMax:LONG
ENDOBJECT
EXPORT PROC end() OF stringlist -> destructor
self.clear()
DisposeLink(self.items)
ENDPROC
EXPORT PROC stringlist(maxSize=-1) OF stringlist ->constructor
IF maxSize=-1 THEN maxSize:=100
self.initialMax:=maxSize
self.items:=List(maxSize)
ENDPROC
EXPORT PROC item(n) OF stringlist
->IF (n<0) OR (n>=ListLen(self.items)) THEN WriteF('stringlist index error \d',n)
ENDPROC self.items[n]
EXPORT PROC clear() OF stringlist
DEF i
FOR i:=0 TO ListLen(self.items)-1
DisposeLink(self.items[i])
ENDFOR
IF ListMax(self.items)>self.initialMax
DisposeLink(self.items)
self.items:=List(self.initialMax)
ELSE
SetList(self.items,0)
ENDIF
ENDPROC
EXPORT PROC expand() OF stringlist
DEF old,len,inc
old:=self.items
len:=ListLen(old)
inc:=Shr(len,2)
IF inc<5 THEN inc:=5
len:=len+inc
self.items:=List(len)
ListAdd(self.items,old)
DisposeLink(old)
ENDPROC len
EXPORT PROC insert(pos,stringVal:PTR TO CHAR) OF stringlist
DEF s,c,i
c:=ListLen(self.items)
IF c=ListMax(self.items) THEN self.expand()
ListAdd(self.items,[NIL])
FOR i:=ListLen(self.items)-1 TO (pos+1) STEP -1
self.items[i]:=self.items[i-1]
ENDFOR
s:=String(StrLen(stringVal))
StrCopy(s,stringVal)
self.items[pos]:=s
ENDPROC
EXPORT PROC contains(stringVal:PTR TO CHAR) OF stringlist
DEF i
FOR i:=0 TO ListLen(self.items)-1
IF StriCmp(self.items[i],stringVal) THEN RETURN TRUE
ENDFOR
ENDPROC FALSE
EXPORT PROC add(stringVal:PTR TO CHAR) OF stringlist
DEF s,c
c:=ListLen(self.items)
IF c=ListMax(self.items) THEN self.expand()
s:=String(StrLen(stringVal))
StrCopy(s,stringVal)
ListAddItem(self.items,s)
ENDPROC c
EXPORT PROC setItem(n,stringVal:PTR TO CHAR) OF stringlist
DEF s
WHILE n>=ListLen(self.items) DO self.add('')
s:=String(StrLen(stringVal))
StrCopy(s,stringVal)
DisposeLink(self.items[n])
self.items[n]:=s
ENDPROC
EXPORT PROC remove(n) OF stringlist
DEF i,t
DisposeLink(self.items[n])
t:=ListLen(self.items)
FOR i:=n TO t-2
self.items[i]:=self.items[i+1]
ENDFOR
SetList(self.items,t-1)
ENDPROC
EXPORT PROC setSize(n) OF stringlist
DEF i
FOR i:=n TO ListLen(self.items)-1
DisposeLink(self.items[i])
ENDFOR
SetList(self.items,n)
ENDPROC
EXPORT PROC makeTextString(sep=0:PTR TO CHAR) OF stringlist
DEF res,i,len=0
FOR i:=0 TO ListLen(self.items)-1
len:=len+StrLen(self.items[i])
IF i THEN len:=len+2
ENDFOR
res:=String(len)
FOR i:=0 TO ListLen(self.items)-1
IF i THEN StrAdd(res,IF sep THEN sep ELSE '\n')
StrAdd(res,self.items[i])
ENDFOR
ENDPROC res
EXPORT PROC setFromTextString(newText:PTR TO CHAR) OF stringlist
DEF pos,n,str
self.clear()
pos:=0
WHILE (n:=InStr(newText+pos,'\n'))>=0
str:=AstrClone(newText+pos,n)
self.add(str)
DisposeLink(str)
pos:=pos+n+1
ENDWHILE
IF StrLen(newText+pos) THEN self.add(newText+pos)
ENDPROC
EXPORT PROC count() OF stringlist IS ListLen(self.items)
EXPORT PROC maxSize() OF stringlist IS ListMax(self.items)
PROC partition(first, last) OF stringlist
DEF splitv, up, down, i
splitv:=self.items[first]
up:=first
down:=last
REPEAT
WHILE (StrCompare(self.items[up],splitv)<=0) AND (up<last) DO up++
WHILE (StrCompare(self.items[down],splitv)>0) AND (down>first) DO down--
IF up<down
i:=self.items[up]
self.items[up]:=self.items[down]
self.items[down]:=i
ENDIF
UNTIL up>=down
i:=self.items[first]
self.items[first]:=self.items[down]
self.items[down]:=i
ENDPROC down
PROC quicksort(first, last) OF stringlist
DEF index
IF first<last
index:=self.partition(first, last)
self.quicksort(first, index-1)
self.quicksort(index+1, last)
ENDIF
ENDPROC
EXPORT PROC sort() OF stringlist
self. quicksort(0,ListLen(self.items)-1)
ENDPROC
/*
list class that holds long values
*/
EXPORT OBJECT stdlist
PRIVATE items:PTR TO LONG
PRIVATE initialMax:LONG
ENDOBJECT
EXPORT PROC end() OF stdlist -> destructor
DisposeLink(self.items)
ENDPROC
EXPORT PROC stdlist(maxSize=-1) OF stdlist ->constructor
IF maxSize=-1 THEN maxSize:=100
self.initialMax:=maxSize
self.items:=List(maxSize)
ENDPROC
EXPORT PROC item(n) OF stdlist
->IF (n<0) OR (n>=ListLen(self.items)) THEN WriteF('stdlist index error \d',n)
ENDPROC self.items[n]
EXPORT PROC clear() OF stdlist
IF ListMax(self.items)>self.initialMax
DisposeLink(self.items)
self.items:=List(self.initialMax)
ELSE
SetList(self.items,0)
ENDIF
ENDPROC
EXPORT PROC expand() OF stdlist
DEF old,len,inc
old:=self.items
len:=ListLen(old)
inc:=Shr(len,2)
IF inc<5 THEN inc:=5
len:=len+inc
self.items:=List(len)
ListAdd(self.items,old)
DisposeLink(old)
ENDPROC len
EXPORT PROC insert(pos,v:LONG) OF stdlist
DEF s,c,i
c:=ListLen(self.items)
IF c=ListMax(self.items) THEN self.expand()
ListAdd(self.items,[NIL])
FOR i:=ListLen(self.items)-1 TO (pos+1) STEP -1
self.items[i]:=self.items[i-1]
ENDFOR
self.items[pos]:=v
ENDPROC
EXPORT PROC add(v:LONG) OF stdlist
DEF c
c:=ListLen(self.items)
IF c=ListMax(self.items) THEN self.expand()
ListAdd(self.items,[0])
self.items[c]:=v
ENDPROC c
EXPORT PROC setItem(n,v) OF stdlist
WHILE n>=ListLen(self.items) DO self.add(0)
self.items[n]:=v
ENDPROC
EXPORT PROC remove(n) OF stdlist
DEF i,t
t:=ListLen(self.items)
FOR i:=n TO t-2
self.items[i]:=self.items[i+1]
ENDFOR
SetList(self.items,t-1)
ENDPROC
EXPORT PROC setSize(n) OF stdlist
SetList(self.items,n)
ENDPROC
EXPORT PROC count() OF stdlist IS ListLen(self.items)
EXPORT PROC maxSize() OF stdlist IS ListMax(self.items)