Ignore:
Timestamp:
03/15/21 05:15:31 (3 months ago)
Author:
Kon Lovett
Message:

queue depth limit (wip)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • release/5/mailbox/trunk/inline-queue.scm

    r39710 r39716  
    1414(define queue 'queue)
    1515(define-record-type-variant queue (unsafe unchecked inline)
    16   (%make-queue ln hd tl)
     16  (%make-queue lm ln hd tl)
    1717  (%queue?)
     18  (lm %queue-limit %queue-limit-set!)
    1819  (ln %queue-count %queue-count-set!)
    1920  (hd %queue-first-pair %queue-first-pair-set!)
    2021  (tl %queue-last-pair %queue-last-pair-set!) )
    2122
    22 (define-inline (%queue-count-inc! q) (%queue-count-set! q (add1 (%queue-count q))))
    23 (define-inline (%queue-count-dec! q) (%queue-count-set! q (sub1 (%queue-count q))))
     23(define-constant QUEUE-UNLIMITED -1)
     24(define-constant QUEUE-UNBUFFERED 0)
    2425
    25 (define-inline (%make-empty-queue) (%make-queue 0 '() '()))
     26(define-inline (%queue-count-inc! q) (%queue-count-set! q (fx+ 1 (%queue-count q))))
     27(define-inline (%queue-count-dec! q) (%queue-count-set! q (fx- 1 (%queue-count q))))
    2628
    27 (define-inline (%queue-empty? q) (zero? (%queue-count q)))
     29(define-inline (%make-empty-queue #!optional (lm QUEUE-UNLIMITED))
     30  (%make-queue lm 0 '() '()) )
     31
     32(define-inline (%queue-room q)
     33  (fx- (%queue-limit q) (%queue-count q)) )
     34
     35(define-inline (%queue-full? q)
     36  (and
     37    (not (fx= QUEUE-UNLIMITED (%queue-limit q)))
     38    (if (fx= QUEUE-UNBUFFERED (%queue-limit q))
     39      (fx= (%queue-count q) 1)
     40      (fx= (%queue-count q) (%queue-limit q)) ) ) )
     41
     42(define-inline (%queue-room? q rq)
     43  (or
     44    (fx= QUEUE-UNLIMITED (%queue-limit q))
     45    (if (fx= QUEUE-UNBUFFERED (%queue-limit q))
     46      (and (fx= (%queue-count q) 0) (fx= rq 1))
     47      (fx<= rq (%queue-room q)) ) ) )
     48
     49(define-inline (%queue-empty? q)
     50  (fx= 0 (%queue-count q)) )
    2851
    2952;; Operations
    3053
    31 (define-inline (%queue-last-pair-empty! q) (%queue-last-pair-set! q '()))
    32 
    33 (define-inline (%queue-add! q datum)
    34   (let ((new-pair (cons datum '())))
    35     (if (null? (%queue-first-pair q))
    36       (%queue-first-pair-set! q new-pair)
    37       (set-cdr! (%queue-last-pair q) new-pair) )
    38     (%queue-last-pair-set! q new-pair) )
    39   (%queue-count-inc! q) )
     54(define-inline (%queue-add! q item)
     55  (if (%queue-full? q)
     56    (warning 'queue-add! "queue full")
     57    (let ((new-pair (cons item '())))
     58      (if (null? (%queue-first-pair q))
     59        (%queue-first-pair-set! q new-pair)
     60        (set-cdr! (%queue-last-pair q) new-pair) )
     61      (%queue-last-pair-set! q new-pair)
     62      (%queue-count-inc! q) ) ) )
    4063
    4164(define-inline (%queue-remove! q)
    42   (let* ((first-pair (%queue-first-pair q))
    43          (next-pair (cdr first-pair)))
    44     (%queue-first-pair-set! q next-pair)
    45     (when (null? next-pair) (%queue-last-pair-empty! q))
    46     (%queue-count-dec! q)
    47     (car first-pair) ) )
     65  (if (%queue-empty? q)
     66    (warning 'queue-remove! "queue empty")
     67    (let* ((first-pair (%queue-first-pair q))
     68           (next-pair (cdr first-pair)))
     69      (%queue-first-pair-set! q next-pair)
     70      (when (null? next-pair) (%queue-last-pair-set! q '()))
     71      (%queue-count-dec! q)
     72      (car first-pair) ) ) )
    4873
    4974(define-inline (%queue-push-back! q item)
    50   (let ((newlist (cons item (%queue-first-pair q))))
    51     (%queue-first-pair-set! q newlist)
    52     (when (null? (%queue-last-pair q)) (%queue-last-pair-set! q newlist) ) )
    53   (%queue-count-inc! q) )
     75  (if (%queue-full? q)
     76    (warning 'queue-push-back! "queue full")
     77    (let ((newlist (cons item (%queue-first-pair q))))
     78      (%queue-first-pair-set! q newlist)
     79      (when (null? (%queue-last-pair q)) (%queue-last-pair-set! q newlist) )
     80      (%queue-count-inc! q) ) ) )
    5481
    5582(define-inline (%queue-push-back-list! q itemlist)
    56   (let ((newlist (append! (list-copy itemlist) (%queue-first-pair q))))
    57     (%queue-first-pair-set! q newlist)
    58     (if (null? newlist)
    59       (%queue-last-pair-empty! q)
    60       (%queue-last-pair-set! q (last-pair newlist) ) ) )
    61   (%queue-count-set! q (+ (length itemlist) (%queue-count q))) )
     83  (if (not (%queue-room? q (length itemlist)))
     84    (warning 'queue-push-back-list! "queue short" (%queue-room q))
     85    (let ((newlist (append! (list-copy itemlist) (%queue-first-pair q))))
     86      (%queue-first-pair-set! q newlist)
     87      (if (null? newlist)
     88        (%queue-last-pair-set! q '())
     89        (%queue-last-pair-set! q (last-pair newlist) ) )
     90      (%queue-count-set! q (+ (length itemlist) (%queue-count q))) ) ) )
    6291
    6392(define-inline (%queue-extract-pair! q targ-pair)
     
    79108            (set-cdr! prev-pair next-pair) )
    80109          ;when the cut pair is the last item update the last pair ref.
    81           (when (eq? this-pair (%queue-last-pair q))
    82             (%queue-last-pair-set! q prev-pair)) )
    83         (%queue-count-dec! q) )
     110          (when (eq? this-pair (%queue-last-pair q)) (%queue-last-pair-set! q prev-pair))
     111          (%queue-count-dec! q) ) )
    84112      ;not found
    85113      (else
Note: See TracChangeset for help on using the changeset viewer.