File: string_pool.e

package info (click to toggle)
smarteiffel 1.1-11
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 12,288 kB
  • ctags: 40,785
  • sloc: ansic: 35,791; lisp: 4,036; sh: 1,783; java: 895; ruby: 613; python: 209; makefile: 115; csh: 78; cpp: 50
file content (443 lines) | stat: -rw-r--r-- 13,394 bytes parent folder | download | duplicates (2)
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
-- This file is part of SmartEiffel The GNU Eiffel Compiler Tools and Libraries
--
-- SmartEiffel is  free software;  you can redistribute it and/or  modify it
-- under  the terms of the  GNU General Public License, as published by  the
-- Free Software Foundation; either version 2, or (at your option) any later
-- version.
-- SmartEiffel is distributed in the hope that it will be useful but WITHOUT 
-- ANY WARRANTY;  without  even the implied warranty  of MERCHANTABILITY  or
-- FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
-- more details.  You should have received a copy of  the GNU General Public
-- License along with SmartEiffel;  see the file COPYING.  If not,  write to
-- the Free Software Foundation,  Inc., 59 Temple Place - Suite 330,  Boston, 
-- MA 02111-1307, USA.
--
-- Copyright(C) 1994-2002: INRIA - LORIA (INRIA Lorraine) - ESIAL U.H.P.
--			   - University of Nancy 1 - FRANCE
-- Copyright(C) 2003:      INRIA - LORIA (INRIA Lorraine) - I.U.T. Charlemagne
--			   - University of Nancy 2 - FRANCE
--
--		 Dominique COLNET, Suzanne COLLIN, Olivier ZENDRA,
--			   Philippe RIBET, Cyril ADRIAN
--
-- http://SmartEiffel.loria.fr - SmartEiffel@loria.fr
--
deferred class STRING_POOL
   --
   -- The STRING_POOL is a collection of strings. It is initialized from
   -- another collection. The strings are not kept "as-is"; that way, you
   -- can reuse your initial collection, or the strings in it; the
   -- string_pool won't be changed.
   --
   -- Be aware that using a STRING_POOL is not always interesting, because
   -- it is very memory-consuming. It's advantage, though, is to be able to
   -- find if a string is in the pool or not (and find its index), very
   -- quickly.
   --
inherit
   GLOBALS
   VISITABLE

feature {STRING_POOL_VISITOR}

   accept(visitor: STRING_POOL_VISITOR) is
      deferred
      end

feature {ANY} -- Interface:

   has_empty: BOOLEAN
         -- The headers list has an empty string

   has(a_string: STRING): BOOLEAN is
      require
         string_not_void:  a_string /= Void
         --string_not_empty: a_string.count > 0
      do
         compute_final_state(a_string, 1, a_string.count)
         Result := last_match /= unknown_state
      end

   has_substring(a_string: STRING; first, last: INTEGER): BOOLEAN is
      require
         string_not_void:   a_string /= Void
         --string_not_empty:  a_string.count > 0
         first_in_string:   first > 0
         last_in_string:    last <= a_string.count
         first_before_last: last >= first
      do
         compute_final_state(a_string, first, last)
         Result := last_match /= unknown_state
      end

   index_of(a_string: STRING): INTEGER is
      require
         string_not_void:  a_string /= Void
         --string_not_empty: a_string.count > 0
         has_string:       has(a_string)
      do
         compute_final_state(a_string, 1, a_string.count)
         Result := last_external_match
      end

   index_of_substring(a_string: STRING; first, last: INTEGER): INTEGER is
      require
         string_not_void:   a_string /= Void
         --string_not_empty:  a_string.count > 0
         first_in_string:   first > 0
         last_in_string:    last <= a_string.count
         first_before_last: last >= first
         has_substring:     has_substring(a_string, first, last)
      do
         compute_final_state(a_string, first, last)
         Result := last_external_match
      end

feature {ANY} -- Initialization:

   from_collection(a_headers: COLLECTION[STRING]) is
         -- Initializes the automaton. The given strings will be the ones
         -- that can be matched. In that case, `match' will return the index
         -- of the matched string in that collection.
         --
         -- Be aware that the procesing time is quite heavy during the
         -- initialization. It's the pay-off to make it really lighter
         -- matches. Don't use the string_pool for a one-time test, it's only
         -- useful in the long run.
         --
         -- After initialization, you can safely discard or modify the
         -- collection; it will not change the automaton.
      require
         headers_not_void:  a_headers /= Void
         headers_not_empty: a_headers.count > 0
         unique_headers:    -- each string contains a different word
         --headers_not_empty: -- each string is non-empty
      local
         cs: COLLECTION_SORTER[STRING]
      do
         counter.increment
         id := counter.value

         create headers.from_collection(a_headers)
         cs.sort(headers)
         count_states

         make_transitions_array
         compile_headers
         externalize_tags(a_headers)
         report(a_headers)
      end

feature {NONE} -- At initialization time:

   report(a_headers: COLLECTION[STRING]) is
      local
         i, j, n, st: INTEGER
         s: STRING
      do
         echo.put_string(once "STRING_POOL#")
         echo.put_integer(id)
         echo.put_string(once ": ")
         echo.put_integer(maxstate+1)
         echo.put_string(once " transition(s)%N")
         from
            i := a_headers.lower
         until
            i > a_headers.upper
         loop
            s := a_headers.item(i)
            echo.put_string("%T%"")
            echo.put_string(s)
            echo.put_string(once "%": ")
            if s.is_empty then
               st := empty_idx
            else
               from
                  st := unknown_state
                  n := s.count
                  j := 1
               until
                  j > n
               loop
                  if j > 1 then
                     echo.put_character(',')
                  end
                  st := state(st, s.item(j))
                  echo.put_integer(st)
                  j := j + 1
               end
            end
            echo.put_string(once " -> ")
            io.flush
            echo.put_integer(tag_state.at(st))
            echo.put_new_line
            i := i + 1
         end
      end

   count_states is
      local
         c, k: CHARACTER
         i, j: INTEGER
         again, next_c: BOOLEAN
         s: STRING
         defstate, oldstate, newstate: INTEGER
      do
         transition_memory.make(headers.count)
         transition_memory.set_all_with(unknown_state)
         from
            maxstate := 0
            newstate := unknown_state
            defstate := headers.upper
            again := true
            i := 1
         until
            not again
         loop
            again := false
            from
               next_c := true
               j := headers.lower
            until
               j > headers.upper
            loop
               s := headers.item(j)
               if not has_empty and then s.is_empty then
                  has_empty := True
                  empty_idx := j
               end
               debug
                  io.put_string(s)
                  io.put_string(once "%T[")
                  io.put_integer(i)
                  io.put_string(once "]%N")
               end
               if oldstate /= transition_memory.item(j) then
                  oldstate := transition_memory.item(j)
                  next_c := true
               end
               if i > s.count then
                  next_c := true
               else
                  check
                     transition_memory.item(j) /= j
                  end
                  again := true
                  k := s.item(i)
                  if i = s.count then
                     check
                        next_c or else c /= k
                     end
                     newstate := j
                     c := k
                  elseif next_c or else c /= k then
                     defstate := defstate + 1
                     newstate := defstate
                     c := k
                  end
                  next_c := false
                  check
                     newstate /= unknown_state
                  end
                  transition_memory.put(newstate, j)
               end
               j := j + 1
            end
            i := i + 1
         end

         maxstate := defstate

         debug
            io.put_string(once "maxstate = ")
            io.put_integer(maxstate)
            io.put_new_line
         end
      end

   compile_headers is
         -- Create the automaton.
      local
         c, k: CHARACTER
         i, j: INTEGER
         again, next_c: BOOLEAN
         s: STRING
         defstate, oldstate, newstate: INTEGER
      do
         transition_memory.make(headers.count)
         transition_memory.set_all_with(unknown_state)

         newstate := unknown_state
         defstate := headers.upper

         from
            again := true
            i := 1
         until
            not again
         loop
            again := false
            from
               next_c := true
               j := headers.lower
            until
               j > headers.upper
            loop
               s := headers.item(j)
               debug
                  io.put_string(s)
                  io.put_string(once "%T[")
                  io.put_integer(i)
                  io.put_string(once "]%N")
               end
               if oldstate /= transition_memory.item(j) then
                  oldstate := transition_memory.item(j)
                  next_c := true
               end
               if i > s.count then
                  next_c := true
               else
                  check
                     transition_memory.item(j) /= j
                  end
                  again := true
                  k := s.item(i)
                  if i = s.count then
                     check
                        next_c or else c /= k
                     end
                     newstate := j
                     c := k
                     set_state(newstate, oldstate, k)
                  elseif next_c or else c /= k then
                     defstate := defstate + 1
                     newstate := defstate
                     c := k
                     set_state(newstate, oldstate, k)
                  end
                  next_c := false
                  check
                     newstate /= unknown_state
                  end
                  transition_memory.put(newstate, j)
               end
               j := j + 1
            end
            i := i + 1
         end
      end

   externalize_tags(a_headers: COLLECTION[STRING]) is
         -- Create tag_state
      local
         intern, extern: INTEGER; s: STRING
      do
         if tag_state = Void then
            create tag_state.with_capacity(headers.count)
         else
            tag_state.clear
         end

         from
            extern := a_headers.lower
         until
            extern > a_headers.upper
         loop
            s := a_headers.item(extern)
            intern := the_final_state(s, 1, s.count)
            debug
               echo.put_integer(intern)
               echo.put_string(once " [int] <-> ")
               echo.put_integer(extern)
               echo.put_string(once " [ext]%N")
            end
            tag_state.put(extern, intern)
            extern := extern + 1
         end
      end

feature {NONE} -- State management:

   make_transitions_array is
      deferred
      end

   set_state(new_state, previous_state: INTEGER; character: CHARACTER) is
      require
         previous_state.in_range(unknown_state, maxstate)
         new_state.in_range(0, maxstate)
      deferred
      ensure
         state(previous_state, character) = new_state
      end

   state(previous_state: INTEGER; character: CHARACTER): INTEGER is
      require
         previous_state.in_range(unknown_state, maxstate)
      deferred
      end

feature {NONE} -- Finding a string:

   compute_final_state(a_string: STRING; first, last: INTEGER) is
      do
         last_match := the_final_state(a_string, first, last)
         if last_match > headers.count then
            last_match := unknown_state
         elseif last_match /= unknown_state then
            check
               headers.item(last_match).is_equal(a_string.substring(first, last))
            end
            -- now externalize
            last_external_match := tag_state.at(last_match)
         end
      end

   the_final_state(a_string: STRING; first, last: INTEGER): INTEGER is
      local
         i: INTEGER
      do
         if a_string.is_empty then
            Result := empty_idx
         else
            from
               Result := state(unknown_state, a_string.item(first))
               i := first + 1
            until
               i > last or else Result = unknown_state
            loop
               Result := state(Result, a_string.item(i))
               i := i + 1
            end
         end
      end

feature {NONE}

   maxstate: INTEGER
   id: INTEGER

   empty_idx: INTEGER

   last_match: INTEGER
   last_external_match: INTEGER

   tag_state: DICTIONARY[INTEGER, INTEGER] -- internal => external states

feature {NONE}

   unknown_state: INTEGER is -1

   headers: FIXED_ARRAY[STRING]

   transition_memory: FIXED_ARRAY[INTEGER] is
      once
         create Result.with_capacity(4)
      end

   counter: COUNTER is
      once
         create Result
      end

end -- STRING_POOL