summaryrefslogtreecommitdiff
path: root/suffix tree/suffix-tree.el
blob: 0d929d0f2e69ac9df4fbe1a5de19735fe7ed9115 (plain)
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
;;; suffiex-tree.el --- Ukkonen algorithm for building a suffix tree -*- lexical-binding: t; -*-

;;; Author: Durand
;;; Version: 0.0.1

;;; Commentary:

;; Our node is represented as a list of the following elements:

;; start      , which is the starting index of the edge going from its parent
;;              node to this node
;; end        , the index of the end of the above edge
;; suffix-link, the index of the node this node's suffix-link points to
;; children   , a hash-table of pairs of integers and indices of its
;;              children

;; To compute the length of the edge going into NODE we use:
;; (- (min end (1+ position)) start)
;; which is actually how far the position is on that edge,
;; if it is on that edge.

;;; Code:

;;;###autoload
(defun st-min (&rest args)
  "Return the minimum among ARGS.
If an argument is 'infty, then it is considered greater than
every number."
  (apply #'min (delq nil
                     (mapcar
                      (lambda (arg)
                        (cond
                         ((number-or-marker-p arg) arg)
                         ((eq arg 'infty) nil)))
                      args))))

;;;###autoload
(defun st-edge-length (node position)
  "Return the length of the edge into NODE.
See the comment above this function for the reason
POSITION is here."
  (- (st-min (car (cdr node)) (1+ position))
     (car node)))

;;;###autoload
(defun st-new-node (tree last-added start &optional end)
  "Make a new node with START and END as the coming edge.
Then add the new node to TREE.
LAST-ADDED is the number of elements already in the TREE."
  (let* ((end (or end 'infty) ;; 'infty represents the index of a leaf
              )
         (suffix-link 0) ;; The suffix link is initially 0
         (new-node
          (list start end
                suffix-link
                (make-hash-table))))
    (puthash (1+ last-added) new-node tree)
    (1+ last-added)))

;;;###autoload
(defun st-add-suffix-link (tree need-sl node)
  "If NEED-SL is positive, then add the suffix link.
In particular, the node corresponding to NEED-SL in TREE
gets a suffix link pointing to NODE.

This always returns NODE."
  (cond
   ((and (> need-sl 1) (/= need-sl node))
    (setcar (cdr (cdr (gethash need-sl tree))) node)))
  node)

;;;###autoload
(defun st-canonize (tree node position active-edge-index active-length active-node)
  "Walk down TREE to find the correct active point."
  (let ((node-edge-length (st-edge-length (gethash node tree) position)))
    (cond
     ((>= active-length node-edge-length)
      (list t
            (+ active-edge-index
               node-edge-length)
            (- active-length
               node-edge-length)
            node))
     (t
      (list nil
            active-edge-index
            active-length
            active-node)))))

;;;###autoload
(defun st-extend-tree (tree last-added position remain
                       active-node active-edge-index
                       active-length character str)
  "Extend a tree by CHARACTER.
The return value is
(tree last-added remain active-node
 active-edge-index active-length)"
  (let* ((need-sl 0)
         (remain (1+ remain))
         continue-p breakp)
    (while (and (not breakp) (> remain 0))
      (setq continue-p nil breakp nil)
      (cond
       ((= active-length 0)
        (setq active-edge-index position)))
      (let* ((actual-node (gethash active-node tree))
             (nxt (cond
                   (actual-node
                    (gethash (aref str active-edge-index)
                             (cadr (cdr (cdr actual-node))))))))
        (cond
         ((null nxt)
          (let ((leaf (st-new-node tree last-added position)))
            (setq last-added leaf)
            (puthash (aref str active-edge-index)
                     leaf
                     (cadr (cdr (cdr (gethash active-node tree)))))
            (setq need-sl (st-add-suffix-link tree need-sl active-node)))
            ;; rule 2
            )
         (t
          (let* ((result (st-canonize
                          tree nxt position active-edge-index
                          active-length active-node)))
            (cond
             ((car result)
              ;; observation 2
              (setq active-edge-index (cadr result))
              (setq active-length (caddr result))
              (setq active-node (cadr (cddr result)))
              (setq continue-p t))
             (t
              (cond
               ((eq (aref str (+ active-length
                                 (car (gethash nxt tree))))
                    character)
                ;; observation 1
                (setq active-length (1+ active-length))
                (setq need-sl (st-add-suffix-link tree need-sl active-node))
                (setq breakp t)))
              (cond
               (breakp)
               (t ;; splitting
                (let ((split (st-new-node
                              tree last-added (car (gethash nxt tree))
                              (+ (car (gethash nxt tree)) active-length))))
                  (setq last-added split)
                  (puthash
                   (aref str active-edge-index)
                   split (cadr (cdr (cdr (gethash active-node tree)))))
                  (let ((leaf (st-new-node tree last-added position)))
                    (setq last-added leaf)
                    (puthash character leaf
                             (cadddr (gethash split tree)))
                    (setcar (gethash nxt tree)
                            (+ (car (gethash nxt tree))
                               active-length))
                    (puthash (aref str (car (gethash nxt tree))) nxt
                             (cadddr (gethash split tree)))
                    ;; rule 2
                    (setq need-sl
                          (st-add-suffix-link tree need-sl split)))))))))))
            (cond
             ((or continue-p breakp))
             (t
              (setq remain (1- remain))
              (cond
               ((and (eq active-node 1) ; root
                     (> active-length 0))
                (setq active-length (1- active-length))
                (setq active-edge-index
                      (1+ (- position remain))))
               (t
                (setq active-node
                      (let ((slink (caddr (gethash active-node tree))))
                        (cond
                         ((> slink 1) slink)
                                        ; or root
                         (t 1))))))))))
    (list tree last-added remain active-node
          active-edge-index active-length)))

;;;###autoload
(defun st-build-for-str (str)
  "Build the suffix tree for STR."
  (let* ((position 0)
         (character (ignore-error 'error (aref str position)))
         (tree (make-hash-table))
         (last-added 0)
         (remain 0)
         (active-node (st-new-node tree last-added -1 -1))
         (active-edge-index 0)
         (active-length 0)
         result)
    (setq last-added active-node)
    (while character
      (setq result (st-extend-tree tree last-added position remain
                                   active-node active-edge-index
                                   active-length character str))
      (setq tree (pop result))
      (setq last-added (pop result))
      (setq remain (pop result))
      (setq active-node (pop result))
      (setq active-edge-index (pop result))
      (setq active-length (pop result))
      (setq position (1+ position))
      (setq character (ignore-error 'error (aref str position))))
    tree))

;;; Some printing functions

(require 'hierarchy)

;;;###autoload
(defun st-print-str (str)
  "Print generalized string"
  (mapc (lambda (char)
          (cond ((characterp char) (insert char))
                (t (insert ", "))))
        str))

;;;###autoload
(defun st-print-tree-for-str (str)
  "Print the suffix tree for STR."
  (let* ((symbol (make-symbol "test")))
    (set symbol (st-build-for-str str))
    (insert "suffix tree for strings: ")
    (st-print-str str)
    (insert "\n")
    (st-print-tree (symbol-value symbol) str)))

;;;###autoload
(defun st-reduce-index (node strs)
  "Reduce the second element of the NODE."
  (let* ((lengths (mapcar #'length strs))
         (start (car node))
         (end (cadr node))
         (first-greater-than-start
          (let ((sum -1) (index 0) done result)
            (while (and (not done) (< index (length strs)))
              (cond
               ((<= (+ sum (nth index lengths)) start)
                (setq index (1+ index))
                (setq sum (+ sum (nth index lengths))))
               (t (setq result (+ sum (nth index lengths)))
                  (setq done t))))
            (cond ((null result) (setq result (1- (apply #'+ lengths)))))
            result)))
    (st-min end first-greater-than-start))
  ;; (let* ((start (car node))
  ;;        (end (cadr node))
  ;;        (index start)
  ;;        result stop)
  ;;   (while (and (not stop) (or (eq end 'infty) (<= index end)))
  ;;     (cond
  ;;      ((and (< index (length str)) (characterp (aref str index))) (setq index (1+ index)))
  ;;      (t (setq stop t)
  ;;         (setq result index))))
  ;;   (cond ((and (numberp end) (= index (1+ end))) (setq result end)))
  ;;   (cond ((<= result start) (setq result 'end)))
  ;;   result)
  )

;; ;;;###autoload
;; (defun st-build-generalized-tree (strs)
;;   (let* ((copy strs)
;;          (long-str
;;           (let ((index 1) result)
;;             (while (consp copy)
;;               (setq result (vconcat result
;;                                     (car copy)
;;                                     (vector (- index))))
;;               (setq index (1+ index))
;;               (setq copy (cdr copy)))
;;             result))
;;          (tree (make-symbol "tree")))
;;     (set tree (st-build-for-str long-str))))

;;;###autoload
(defun st-print-tree (tree str)
  "Print TREE with the aid of STR."
  (let* ((symbol-tree (make-symbol "test-tree")))
    (set symbol-tree (hierarchy-new))
    (maphash
     (lambda (key value)
       (hierarchy-add-tree
        (symbol-value symbol-tree) key nil
        (lambda (item)
          (hash-table-values (cadr (cdr (cdr (gethash item tree))))))))
     tree)
    (hierarchy-print (symbol-value symbol-tree)
                     (lambda (item)
                       (cond ((= item 1) "root")
                             (t (let* ((node (gethash item tree))
                                       ;; (reduced (st-reduce-index node str))
                                       )
                                  (substring str (car node)
                                             (cond ((integerp (cadr node)) (cadr node))))
                                  ;; (cond ((eq reduced 'end) "$")
                                  ;;       (t (apply #'string
                                  ;;                 (append
                                  ;;                  (seq-subseq str (car node)
                                  ;;                              reduced
                                  ;;                              ;; (cond ((integerp (cadr node)) (cadr node)))
                                  ;;                              )
                                  ;;                  nil))))
                                  
                                  ;; (format "%d): (%S, %S): (%d): \"%s\""
                                  ;;         item
                                  ;;         (car node)
                                  ;;         (cadr node)
                                  ;;         (caddr node)
                                  ;;         (substring str (car node) (cond ((integerp (cadr node)) (cadr node))))
                                  ;;         )
                                  )))))))

(provide 'suffix-tree)
;;; suffiex-tree.el ends here

;;; archive

;; ;;;###autoload
;; (defvar st-root nil
;;   "The root of the suffix tree.")

;; ;;;###autoload
;; (defvar st-position nil
;;   "The current position in the string.")

;; ;;;###autoload
;; (defvar st-wait-for-link nil
;;   "The node that is waiting to have a suffix link.")

;; ;;;###autoload
;; (defvar st-remain nil
;;   "The number of remaining suffixes to insert.")

;; ;;;###autoload
;; (defvar st-active-node nil
;;   "Where to insert a new suffix.")

;; ;;;###autoload
;; (defvar st-active-edge-index nil
;;   "The index of the active edge.")

;; ;;;###autoload
;; (defvar st-active-length nil
;;   "How far down is the active point down the active edge.")

;; (insert (format "after character %c, the tree becomes\n" character))
;; (insert (format "string is %s\n" (substring str 0 (1+ position))))
;; (insert (format "active-node: %d\n" active-node))
;; (insert (format "active-edge: %c\n" (aref str active-edge-index)))
;; (insert (format "active-length: %d\n" active-length))
;; (cond ((eq last-added 11)
;;        (insert (format "slink: %d\n" (caddr (gethash active-node tree))))
;;        (insert (format "con: %S" continue-p))
;;        (insert (format "breakp: %S\n" breakp))))
;; (st-print-tree tree (substring str 0 (1+ position)))
;; (insert "\n\n")


;; ;;;###autoload
;; (defvar st-output-buffer-name "*suffix-tree*"
;;   "The name of the buffer that contains the output.")

;; ;;;###autoload
;; (defun st-print-tree (tree node str prefix)
;;   "Print TREE in the dedicated output buffer starting at NODE.
;; PREFIX is used to recursively call this function.

;; We need STR since the information we stored in the tree
;; only contains the index into STR,
;; for the sake of optimizations."
;;   (with-temp-buffer-window st-output-buffer-name '((display-buffer-at-bottom)) nil
;;     (st-print-tree-internal tree node str prefix)))

;; ;;;###autoload
;; (defun st-print-tree-internal (tree node str prefix)
;;   "The function that really prints out the data."
;;   (let* ((node-data (gethash node tree))
;;          (start (car node-data))
;;          (end (cadr node-data))
;;          (edges (cdddr node-data))
;;          (node-num (format "(%d)" node))
;;          (node-label (format "%s-%s-%s"
;;                              prefix node-num
;;                              (substring str start end))))
;;     (prin1 node-label)
;;     ;; print the first edge
;;     (let* ((prefix-base (concat
;;                          prefix
;;                          (make-string (length node-label) 32)))
;;            ;; TODO
;;            ))))