git.fiddlerwoaroof.com
Browse code

refactor(wrapped-sequence): use Shinmera/trivial-extensible-sequences

fiddlerwoaroof authored on 20/12/2020 03:35:50
Showing 1 changed files
... ...
@@ -6,20 +6,26 @@
6 6
 (defgeneric underlying (wrapper)
7 7
   (:documentation "Return the underlying object of a wrapper"))
8 8
 
9
-(defclass tagged-sequence (standard-object sequence)
10
-  ((%underlying-sequence :initarg :underlying :accessor underlying)
11
-   (%key-fn :initarg :key :reader key)
12
-   (%invert-key :initarg :invert-key :reader invert-key)))
9
+(defgeneric key (tagged-sequence))
10
+(defgeneric invert-key (tagged-sequence))
11
+(defclass tagged-sequence (standard-object
12
+                           org.shirakumo.trivial-extensible-sequences:sequence)
13
+  ((%underlying-sequence :initarg :underlying :accessor underlying)))
13 14
 
14
-(defmethod sb-sequence:length ((sequence tagged-sequence))
15
+
16
+
17
+(defmethod org.shirakumo.trivial-extensible-sequences:length
18
+    ((sequence tagged-sequence))
15 19
   (length (underlying sequence)))
16 20
 
17
-(defmethod sb-sequence:elt ((sequence tagged-sequence) index)
21
+(defmethod org.shirakumo.trivial-extensible-sequences:elt
22
+    ((sequence tagged-sequence) index)
18 23
   (funcall (key sequence)
19 24
            (elt (underlying sequence)
20 25
                 index)))
21 26
 
22
-(defmethod (setf sb-sequence:elt) (new-value (sequence tagged-sequence) index)
27
+(defmethod (setf org.shirakumo.trivial-extensible-sequences:elt)
28
+    (new-value (sequence tagged-sequence) index)
23 29
   (setf (elt (underlying sequence)
24 30
              index)
25 31
         (funcall (invert-key sequence)
... ...
@@ -27,27 +33,25 @@
27 33
                       index)
28 34
                  new-value)))
29 35
 
30
-(defmethod sb-sequence:adjust-sequence ((sequence tagged-sequence) length
31
-                                        &rest r
32
-                                        &key initial-element initial-contents)
36
+(defmethod org.shirakumo.trivial-extensible-sequences:adjust-sequence
37
+    ((sequence tagged-sequence) length
38
+     &rest r
39
+     &key initial-element initial-contents)
33 40
   (declare (ignore initial-element initial-contents))
34
-  (make-instance 'tagged-sequence
35
-                 :underlying (apply #'sb-sequence:adjust-sequence
36
-                                    (copy-seq (underlying sequence)) length
37
-                                    r)
38
-                 :key-fn (key sequence)
39
-                 :invert-key (invert-key sequence)))
40
-
41
-(defmethod sb-sequence:make-sequence-like
41
+  (unless (slot-boundp sequence '%underlying-sequence)
42
+    (setf (underlying sequence) ()))
43
+  (fw.lu:prog1-bind (it (make-instance (class-of sequence)
44
+                                       :underlying (apply
45
+                                                    #'org.shirakumo.trivial-extensible-sequences:adjust-sequence
46
+                                                    (copy-seq (underlying sequence)) length
47
+                                                    r)))
48
+    (describe it)))
49
+
50
+(defmethod org.shirakumo.trivial-extensible-sequences:make-sequence-like
42 51
     ((sequence tagged-sequence) length &rest r)
43
-  (apply #'sb-sequence:adjust-sequence sequence length r))
44
-
45
-(defun wrap-sequence (seq key-fn invert-key-fn)
46
-  (if invert-key-fn
47
-      (make-instance 'tagged-sequence
48
-                     :underlying seq
49
-                     :key key-fn
50
-                     :invert-key invert-key-fn)
51
-      (make-instance 'tagged-sequence
52
-                     :underlying seq
53
-                     :key key-fn)))
52
+  (apply #'org.shirakumo.trivial-extensible-sequences:adjust-sequence
53
+         sequence length r))
54
+
55
+(defun wrap-sequence (class seq)
56
+  (make-instance class
57
+                 :underlying seq))