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


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

;; start,
;;       which is the starting index of the edge going from its parent
;;       node
;; end, the index of the end index
;; suffix-link, the index of the node its 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+ st-position)) start)
;; which is actually how far the position is on that edge,
;; if it is on that edge.

;;;###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
   ((> need-sl 0)
    (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)))))

;;; For review later
;;;###autoload
;; (defun st-init)
;; {
;; 	needSL = 0, last_added = 0, pos = -1, 
;; 	remainder = 0, active_node = 0, active_e = 0, active_len = 0;
;; 	root = active_node = new_node(-1, -1);
;; }

;;;###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))
      (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 actual-node) (user-error "active-node: %S" active-node)))
        ;; (message "aei: %S" active-edge-index)
        ;; (message "act: %S" (gethash active-node tree))
        (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 0) slink)
                                        ; or root
                         (t 1))))))))))
    ;; (message "remain is %d" remain)
    (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))

;;;###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 \"" str "\"" "\n")
    (st-print-tree (symbol-value symbol) 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)))
                                  (substring str (car node) (cond ((integerp (cadr node)) (cadr node)))))))))))
















;;; Some printing functions

(use-package "hierarchy" 'hierarchy)

;; ;;;###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
;;            ))))











(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.")