aboutsummaryrefslogtreecommitdiff
path: root/integer-partition/accel-asc.scm
blob: 870cb0baaf966c6b219f7fa3df1be059651487e8 (about) (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
;;; Copyright 2024 Masaya Tojo <masaya@tojo.tokyo>
;;;
;;; Licensed under the Apache License, Version 2.0 (the "License");
;;; you may not use this file except in compliance with the License.
;;; You may obtain a copy of the License at
;;;
;;;     http://www.apache.org/licenses/LICENSE-2.0
;;;
;;; Unless required by applicable law or agreed to in writing, software
;;; distributed under the License is distributed on an "AS IS" BASIS,
;;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
;;; See the License for the specific language governing permissions and
;;; limitations under the License.

(define-library (integer-partition accel-asc)
  (export accel-asc)
  (import (scheme base))
  (cond-expand
    ((library (scheme generator))
     (import (only (scheme generator) make-coroutine-generator)))
    ((library (srfi 158))
     (import (only (srfi 158) make-coroutine-generator))))
  (begin
    (define-syntax while
      (syntax-rules ()
        ((_ condition body body* ...)
         (let loop ()
           (if condition
               (begin
                 body
                 body* ...
                 (loop))
               (if #f #f)))))))
  (begin
    (define (accel-asc n)
      (accel-asc* n vector-copy))

    (define (accel-asc* n convert)
      (make-coroutine-generator
       (lambda (yield)
         (let ((a (make-vector (+ n 1) 0))
               (k 1)
               (x 0)
               (y (- n 1)))
           (while (not (= k 0))
             (set! x (+ (vector-ref a (- k 1)) 1))
             (set! k (- k 1))
             (while (<= (* 2 x) y)
               (vector-set! a k x)
               (set! y (- y x))
               (set! k (+ k 1)))
             (let ((l (+ k 1)))
               (while (<= x y)
                 (vector-set! a k x)
                 (vector-set! a l y)
                 (yield (convert a 0 (+ k 2)))
                 (set! x (+ x 1))
                 (set! y (- y 1)))
               (vector-set! a k (+ x y))
               (set! y (+ x y -1))
               (yield (convert a 0 (+ k 1)))))))))
    ))