Changeset 39694 in project
- Timestamp:
- 03/13/21 22:10:44 (6 weeks ago)
- Location:
- release/5/stack/trunk
- Files:
-
- 1 added
- 2 deleted
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
release/5/stack/trunk/stack.egg
r38630 r39694 3 3 4 4 ((synopsis "Provides LIFO queue (stack) operations") 5 (version "3.0. 4")5 (version "3.0.5") 6 6 (category data) 7 (author " [[kon lovett]]")7 (author "Kon Lovett") 8 8 (license "BSD") 9 (dependencies 10 (check-errors "3.1.0")) 9 (dependencies record-variants check-errors) 11 10 (test-dependencies test) 12 11 (components -
release/5/stack/trunk/stack.scm
r38630 r39694 1 ;;;; stack.scm 1 ;;;; stack.scm -*- Scheme -*- 2 2 ;;;; Kon Lovett, May '17 3 3 ;;;; Kon Lovett, Mar '09 … … 8 8 ;; Issues 9 9 ;; 10 ;; - All operations inlined & primitive due to high-performance nature.11 10 12 11 (declare … … 29 28 stack-cut! 30 29 stack-pop! 30 stack-fold 31 31 stack-map 32 32 stack-for-each 33 33 stack-literal-form) 34 34 35 (import scheme) 36 (import (srfi 10)) 37 (import (chicken base)) 38 (import (chicken fixnum)) 39 (import (chicken type)) 40 (import (only (chicken port) with-output-to-port)) 41 (import (only (chicken format) format)) 42 (import (only type-errors define-error-type error-list error-fixnum)) 43 44 (include "chicken-primitive-object-inlines") 45 (include "inline-type-checks") 35 (import scheme 36 (srfi 10) 37 (chicken base) 38 (chicken fixnum) 39 (chicken type) 40 (only (chicken port) with-output-to-port) 41 (only (chicken format) format) 42 (only record-variants define-record-type-variant) 43 (only type-checks define-check+error-type check-list check-fixnum)) 46 44 47 45 #| chicken 46 flip 47 foldl 48 foldr 48 49 warning 49 50 : … … 60 61 |# 61 62 63 (define-inline (%list-pair-ref ls0 i0) 64 ;(assert (and (proper-list? ls0) (exact? i0) (<= 0 i0 (sub1 (length ls0))))) 65 (let loop ((ls ls0) (i i0)) 66 (cond 67 ((null? ls) '() ) 68 ((fx= 0 i) ls ) 69 (else (loop (cdr ls) (fx- i 1)) ) ) ) ) 70 71 (define-inline (%list-copy ls) (foldr cons '() ls)) 72 73 (define-inline (%list-fold f init ls) (foldl (flip f) init ls)) 74 75 (define-inline (%fxclosed-left? l x h) (and (fx<= l x) (fx< x h))) 76 62 77 ;; Stack Type 63 78 64 (define-type stack (struct stack#stack)) 65 66 (define-constant stack 'stack#stack) 67 68 (define stack) 79 ;(define-type stack (struct stack#stack)) 80 (define-type stack (struct stack)) 81 82 (: make-stack (-> stack)) 83 (: list->stack (list -> stack)) 84 (: stack? (* -> boolean)) 85 (: stack-empty? (stack -> boolean)) 86 (: stack-count (stack -> fixnum)) 87 (: stack-peek (stack #!optional fixnum -> *)) 88 (: stack-empty! (stack -> void)) 89 (: stack-poke! (stack * #!optional fixnum -> void)) 90 (: stack-push! (stack #!rest * -> void)) 91 (: stack-cut! (stack fixnum #!optional fixnum -> list)) 92 (: stack-pop! (stack -> *)) 93 (: stack->list (stack -> list)) 94 (: stack-fold (stack procedure * -> *)) 95 (: stack-map (stack procedure -> list)) 96 (: stack-for-each (stack procedure -> void)) 97 98 ;; Stack Object 99 100 (define stack 'stack) 101 (define-record-type-variant stack (unchecked inline unsafe) 102 (%make-stack lst cnt) 103 (%stack?) 104 (lst %stack-list %stack-list-set!) 105 (cnt %stack-count %stack-count-set!)) 106 107 ;NOTE ref's forward defined `stack?' 108 (define-check+error-type stack) 109 110 (define (error-corrupted-stack loc obj) (##sys#signal-hook #:runtime-error loc "stack corrupted" obj)) 111 (define (error-stack-underflow loc stk) (##sys#signal-hook #:limit-error loc "stack underflow" stk)) 112 (define (error-outside-range loc obj low high) (##sys#signal-hook #:bounds-error loc "out of range" obj low high)) 113 114 (define-inline (%make-empty-stack) (%make-stack '() 0)) 69 115 70 116 ;; Stack List 71 117 72 (define-inline (%stack-list stk) (%structure-ref stk 1)) 73 (define-inline (%stack-list-empty? stk) (%null? (%stack-list stk))) 74 (define-inline (%stack-list-set! stk ls) (%structure-set! stk 1 ls)) 75 (define-inline (%stack-list-empty! stk) (%structure-set!/immediate stk 1 '())) 118 (define-inline (%stack-list-empty? stk) (null? (%stack-list stk))) 119 (define-inline (%stack-list-empty! stk) (%stack-list-set! stk '())) 76 120 77 121 ;; Stack Count 78 122 79 (define-inline (%stack-count stk) (%structure-ref stk 2)) 80 (define-inline (%stack-count-set! stk cnt) (%structure-set!/immediate stk 2 cnt)) 81 (define-inline (%stack-count-inc! stk cnt) (%stack-count-set! stk (%fx+ (%stack-count stk) cnt))) 82 (define-inline (%stack-count-dec! stk cnt) (%stack-count-set! stk (%fx- (%stack-count stk) cnt))) 123 (define-inline (%stack-count-inc! stk cnt) (%stack-count-set! stk (fx+ (%stack-count stk) cnt))) 124 (define-inline (%stack-count-dec! stk cnt) (%stack-count-set! stk (fx- (%stack-count stk) cnt))) 83 125 84 126 ;; Stack Object 85 127 86 (define-inline (%make-stack) (%make-structure stack '() 0)) 87 88 (define-inline (%stack? obj) (%structure-instance? obj stack)) 89 128 #; ;UNUSED 90 129 (define-inline (%valid-as-stack? obj) 91 130 (and 92 (%fx= 3 (%structure-length obj)) 93 (%list? (%stack-list obj)) ) ) 131 (fx= 3 (%structure-length obj)) 132 (list? (%stack-list obj)) 133 (fixnum? (%stack-count obj)) ) ) 94 134 95 135 ;; Stack Operations … … 104 144 (%stack-count-dec! stk 1) 105 145 (let ((ls (%stack-list stk))) 106 (%stack-list-set! stk ( %cdr ls))107 ( %car ls) ) )146 (%stack-list-set! stk (cdr ls)) 147 (car ls) ) ) 108 148 109 149 (define-inline (%stack-push/1! stk obj) 110 150 (%stack-count-inc! stk 1) 111 (%stack-list-set! stk ( %cons obj (%stack-list stk))) )151 (%stack-list-set! stk (cons obj (%stack-list stk))) ) 112 152 113 153 (define-inline (%stack-push! stk ls) 114 (if ( %null? (%cdr ls))115 (%stack-push/1! stk ( %car ls))116 ( %list-for-each/1(lambda (x) (%stack-push/1! stk x)) ls) ) )154 (if (null? (cdr ls)) 155 (%stack-push/1! stk (car ls)) 156 (for-each (lambda (x) (%stack-push/1! stk x)) ls) ) ) 117 157 118 158 (define-inline (%stack-node-ref loc stk idx) 119 159 (let ((pr (%list-pair-ref (%stack-list stk) idx))) 120 (if ( %pair? pr)160 (if (pair? pr) 121 161 pr 122 162 (error-outside-range loc idx 0 (%stack-count stk)) ) ) ) … … 124 164 ;; Helpers 125 165 126 (define-inline (%check-stack loc obj) 127 (unless (%stack? obj) (error-stack loc obj)) 128 (unless (%valid-as-stack? obj) (error-corrupted-stack loc obj)) 129 obj ) 130 131 (define-inline (%check-stack-underflow loc stk) 166 (define (check-stack-underflow loc stk) 132 167 (when (%stack-empty? stk) (error-stack-underflow loc stk)) 133 168 stk ) 134 169 135 (define -inline (%check-fixnum-index loc lfx fx hfx)170 (define (check-fixnum-index loc lfx fx hfx) 136 171 (unless (%fxclosed-left? lfx fx hfx) (error-outside-range loc fx lfx hfx)) 137 172 ;cannot return useful value (singular) … … 140 175 ;;; 141 176 142 (define-error-type stack) 143 144 (define (error-corrupted-stack loc obj) (##sys#signal-hook #:runtime-error loc "stack corrupted" obj)) 145 (define (error-stack-underflow loc stk) (##sys#signal-hook #:limit-error loc "stack underflow" stk)) 146 (define (error-outside-range loc obj low high) (##sys#signal-hook #:bounds-error loc "out of range" obj low high)) 147 148 ;;; 149 150 (: make-stack (-> stack)) 151 ; 152 (define (make-stack) (%make-stack)) 153 154 (: list->stack (list -> stack)) 155 ; 177 (define (make-stack) (%make-empty-stack)) 178 156 179 (define (list->stack ls) 157 ( %check-list 'list->stack ls)158 (let ((stk (%make- stack)))159 (%stack-count-set! stk ( %length ls))180 (check-list 'list->stack ls) 181 (let ((stk (%make-empty-stack))) 182 (%stack-count-set! stk (length ls)) 160 183 (%stack-list-set! stk (%list-copy ls)) 161 184 stk ) ) 162 185 163 (: stack? (* -> boolean))164 ;165 186 (define (stack? obj) (%stack? obj)) 166 187 167 (: stack-empty? (stack -> boolean)) 168 ; 169 (define (stack-empty? stk) (%stack-empty? (%check-stack 'stack-empty? stk))) 170 171 (: stack-count (stack -> fixnum)) 172 ; 173 (define (stack-count stk) (%stack-count (%check-stack 'stack-count stk))) 174 175 (: stack-peek (stack #!optional fixnum -> *)) 176 ; 177 (define (stack-peek stk #!optional (idx 0)) (%car (%stack-node-ref 'stack-peek (%check-stack 'stack-peek stk) idx))) 178 179 (: stack-empty! (stack -> void)) 180 ; 181 (define (stack-empty! stk) (%stack-empty! (%check-stack 'stack-empty! stk))) 182 183 (: stack-poke! (stack * #!optional fixnum -> void)) 184 ; 185 (define (stack-poke! stk obj #!optional (idx 0)) (%set-car!/mutate (%stack-node-ref 'stack-poke! (%check-stack 'stack-poke! stk) idx) obj)) 186 187 (: stack-push! (stack #!rest * -> void)) 188 ; 189 (define (stack-push! stk #!rest ls) (unless (%null? ls) (%stack-push! (%check-stack 'stack-push! stk) ls))) 190 191 (: stack-cut! (stack fixnum #!optional fixnum -> list)) 192 ; 188 (define (stack-empty? stk) (%stack-empty? (check-stack 'stack-empty? stk))) 189 190 (define (stack-count stk) (%stack-count (check-stack 'stack-count stk))) 191 192 (define (stack-peek stk #!optional (idx 0)) (car (%stack-node-ref 'stack-peek (check-stack 'stack-peek stk) idx))) 193 194 (define (stack-empty! stk) (%stack-empty! (check-stack 'stack-empty! stk))) 195 196 (define (stack-poke! stk obj #!optional (idx 0)) (set-car! (%stack-node-ref 'stack-poke! (check-stack 'stack-poke! stk) idx) obj)) 197 198 (define (stack-push! stk #!rest ls) (unless (null? ls) (%stack-push! (check-stack 'stack-push! stk) ls))) 199 193 200 (define (stack-cut! stk start #!optional (end (%stack-count stk))) 194 ( %check-stack 'stack-cut! stk)195 ( %check-fixnum 'stack-cut! start)196 ( %check-fixnum 'stack-cut! end)197 ( %check-fixnum-index 'stack-cut! 0 start end)198 ( %check-fixnum-index 'stack-cut! start end (%fx+ (%stack-count stk) 1))199 (let ((cnt ( %fx- end start)))201 (check-stack 'stack-cut! stk) 202 (check-fixnum 'stack-cut! start) 203 (check-fixnum 'stack-cut! end) 204 (check-fixnum-index 'stack-cut! 0 start end) 205 (check-fixnum-index 'stack-cut! start end (fx+ (%stack-count stk) 1)) 206 (let ((cnt (fx- end start))) 200 207 (%stack-count-dec! stk cnt) 201 208 ; From the top? 202 (if ( %fx= 0 start)209 (if (fx= 0 start) 203 210 ;then removing leading elements 204 211 (let* ( 205 212 (spr (%stack-list stk)) 206 (epr (%list-pair-ref spr ( %fx- cnt 1)))213 (epr (%list-pair-ref spr (fx- cnt 1))) 207 214 (ls spr) ) 208 (%stack-list-set! stk ( %cdr epr))209 ( %set-cdr!/immediateepr '())215 (%stack-list-set! stk (cdr epr)) 216 (set-cdr! epr '()) 210 217 ls ) 211 218 ;else removing interior elements 212 219 (let* ( 213 (spr (%stack-node-ref 'stack-cut! stk ( %fx- start 1)))220 (spr (%stack-node-ref 'stack-cut! stk (fx- start 1))) 214 221 (epr (%list-pair-ref spr cnt)) 215 (ls ( %cdr spr)) )216 ( %set-cdr!/mutate spr (%cdr epr))217 ( %set-cdr!/immediateepr '())222 (ls (cdr spr)) ) 223 (set-cdr! spr (cdr epr)) 224 (set-cdr! epr '()) 218 225 ls ) ) ) ) 219 226 220 (: stack-pop! (stack -> *))221 ;222 227 (define (stack-pop! stk) 223 ( %check-stack 'stack-pop! stk)224 ( %check-stack-underflow 'stack-pop! stk)228 (check-stack 'stack-pop! stk) 229 (check-stack-underflow 'stack-pop! stk) 225 230 (%stack-pop! stk) ) 226 231 227 (: stack->list (stack -> list)) 228 ; 229 (define (stack->list stk) (%list-copy (%stack-list (%check-stack 'stack->list stk)))) 230 231 (: stack-fold (stack procedure * -> *)) 232 ; 233 (define (stack-fold stk func init) (%list-fold/1 func init (%stack-list (%check-stack 'stack-fold stk)))) 234 235 (: stack-map (stack procedure -> list)) 236 ; 237 (define (stack-map stk func) (%list-map/1 func (%stack-list (%check-stack 'stack-map stk)))) 238 239 (: stack-for-each (stack procedure -> void)) 240 ; 241 (define (stack-for-each stk proc) (%list-for-each/1 proc (%stack-list (%check-stack 'stack-for-each stk)))) 232 (define (stack->list stk) (%list-copy (%stack-list (check-stack 'stack->list stk)))) 233 234 (define (stack-fold stk func init) (%list-fold func init (%stack-list (check-stack 'stack-fold stk)))) 235 236 (define (stack-map stk func) (map func (%stack-list (check-stack 'stack-map stk)))) 237 238 (define (stack-for-each stk proc) (for-each proc (%stack-list (check-stack 'stack-for-each stk)))) 242 239 243 240 ;;; Read/Print Syntax -
release/5/stack/trunk/tests/run.scm
r38630 r39694 3 3 (import scheme) 4 4 5 ;; ;Create Egg Const5 ;; Create Egg Const 6 6 7 ( define EGG-NAME "stack")7 (include-relative "run-ident") 8 8 9 9 ;chicken-install invokes as "<csi> -s run.scm <eggnam> <eggdir>" … … 18 18 19 19 (define *args* (argv)) 20 (define *current-directory* (cond-expand (unix "./") (else #f))) 21 ;no -disable-interrupts or -no-lambda-info 22 (define *csc-init-options* '(-inline-global -local -inline -specialize 23 -optimize-leaf-routines -clustering -lfa2 -no-trace -unsafe -strict-types)) 24 (define *test-files-rx* (irregex '(: (+ graph) #\- "test" #\. "scm"))) 20 25 21 (define (egg-name args #!optional (def EGG-NAME)) 26 (define (remq obj ls) 27 (let loop ((curr ls) (prev '())) 28 (cond 29 ((null? curr) 30 ls ) 31 ((eq? obj (car curr)) 32 (if (null? prev) 33 (cdr ls) 34 (begin 35 (set-cdr! prev (cdr curr)) 36 ls ) ) ) 37 (else 38 (loop (cdr curr) curr) ) ) ) ) 39 40 (define (remqs os ls) 41 (let loop ((ls ls) (os os)) 42 (cond 43 ((null? os) 44 ls ) 45 (else 46 (loop (remq (car os) ls) (cdr os)) ) ) ) ) 47 48 (define (egg-name #!optional (args *args*) (def EGG-NAME)) 22 49 (cond 23 50 ((<= 4 (length *args*)) (cadddr *args*) ) … … 26 53 (error 'run "cannot determine egg-name") ) ) ) 27 54 28 (define *current-directory* (cond-expand (unix "./") (else #f)))29 (define *egg* (egg-name *args*))55 (define (as-csc-options ls) 56 (apply string-append (intersperse (map symbol->string ls) " ")) ) 30 57 31 ;no -disable-interrupts or -no-lambda-info 32 (define *csc-options* "-inline-global -local -inline \ 33 -specialize -optimize-leaf-routines -clustering -lfa2 \ 34 -no-trace -unsafe \ 35 -strict-types") 58 (define (csc-options) 59 (as-csc-options (remqs *csc-remv-options* *csc-init-options*)) ) 36 60 37 (define *test-files-rx* (irregex '(: (+ graph) #\- "test" #\. "scm")))38 61 (define (test-filename name) (string-append name "-test")) 62 39 63 (define (test-files) (find-files "." #:test *test-files-rx* #:limit 1)) 40 64 … … 43 67 name 44 68 (make-pathname *current-directory* (test-filename name) "scm") ) ) 69 70 ;; 45 71 46 72 (define (run-test-evaluated source) … … 54 80 (system (pathname-replace-directory (pathname-strip-extension source) *current-directory*)) ) 55 81 56 ;;; 57 58 (define (run-test #!optional (name *egg*) (csc-options *csc-options*)) 82 (define (run-test #!optional (name (egg-name)) (csc-options (csc-options))) 59 83 (let ( 60 84 (source (ensure-test-source-name name)) ) … … 65 89 (run-test-compiled source csc-options) ) ) 66 90 67 (define (run-tests #!optional (tests (test-files)) (csc-options *csc-options*))91 (define (run-tests #!optional (tests (test-files)) (csc-options (csc-options))) 68 92 (for-each (cut run-test <> csc-options) tests) ) 69 93 70 ;; ;Do Test94 ;; Do Test 71 95 72 96 (run-tests) -
release/5/stack/trunk/tests/stack-test.scm
r35980 r39694 18 18 19 19 (test-group "Push!/Pop!/Peek/Poke!" 20 (let ( [stk (make-stack)])20 (let ((stk (make-stack))) 21 21 (stack-push! stk 1) 22 22 (stack-push! stk 2 3) … … 36 36 37 37 (test-group "Cut!" 38 (let ( [stk (make-stack)])38 (let ((stk (make-stack))) 39 39 ;3 2 1 40 40 (stack-push! stk 1 2 3) … … 55 55 56 56 (test-group "Stack from List" 57 (let ( [stk (make-stack)]58 [stk1 (list->stack '(1 2 3))])57 (let ((stk (make-stack)) 58 (stk1 (list->stack '(1 2 3)))) 59 59 ; 60 60 (stack-push! stk 1 2 3) … … 66 66 ) 67 67 68 (test-group "Stack HOF" 69 (let ((stk (list->stack '(1 2 3)))) 70 (test 6 (stack-fold stk + 0)) 71 (test '(2 3 4) (stack-map stk add1)) ) 72 ) 73 68 74 ;;; 69 75
Note: See TracChangeset
for help on using the changeset viewer.