unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
From: Daniel Hartwig <mandyke@gmail.com>
To: guile-devel@gnu.org
Subject: Re: Growable arrays?
Date: Mon, 11 Jun 2012 17:13:00 +0800	[thread overview]
Message-ID: <CAN3veRerWrskD4qKpe2J=kakQeq-pAzEOr9xTSHhhCEwb-3uew@mail.gmail.com> (raw)
In-Reply-To: <CAN3veRdbqo4QdCTNM-a4A0dhoe4Dc4iDut2Jdyk_xgO-_PSMaA@mail.gmail.com>

[-- Attachment #1: Type: text/plain, Size: 512 bytes --]

On 11 June 2012 17:01, Daniel Hartwig <mandyke@gmail.com> wrote:
> For reference, attached is a growable vector I use in several
> projects, adapted to support the length operation similar to Lua (i.e.
> first unset numerical index).  There is no catching of exceptions
> here, every access to the data is through the dynvector procedures
> which check the index and vector size.

Always test before posting even small changes to existing code :-)

Updated attachment to actually run without problems

[-- Attachment #2: dynvector.scm --]
[-- Type: application/octet-stream, Size: 1915 bytes --]

(define-module (dynvector)
  #:use-module (srfi srfi-9)
  #:export (dynvector
            make-dynvector
            dynvector?
            dynvector-length
            dynvector-capacity
            dynvector-ref
            dynvector-set!
            dynvector-growth-factor))

(define-record-type dynvector-type
  (%make-dynvector data cached-length)
  dynvector?
  (data dynvector-data set-dynvector-data!)
  (cached-length dynvector-cached-length set-dynvector-cached-length!))

(define dynvector-growth-factor
  (make-fluid 2))

(define (dynvector . l)
  (%make-dynvector (apply vector l) #f))

(define (make-dynvector len . rest)
  (%make-dynvector (apply make-vector len rest) #f))

(define (%dynvector-length dynvector)
  (let iter ((k 0))
    (cond ((eq? k (dynvector-capacity dynvector)) k)
          ((eq? *unspecified* (dynvector-ref dynvector k)) k)
          (else (iter (1+ k))))))

;; index of the first unspecified value
(define (dynvector-length dynvector)
  (unless (dynvector-cached-length dynvector)
    (set-dynvector-cached-length! dynvector (%dynvector-length dynvector)))
  (dynvector-cached-length dynvector))

(define (dynvector-capacity dynvector)
 (vector-length (dynvector-data dynvector)))

(define (dynvector-ref dynvector k)
  (if (< k (dynvector-capacity dynvector))
      (vector-ref (dynvector-data dynvector) k)
      *unspecified*))

(define (dynvector-set! dynvector k obj)
  (while (<= (dynvector-capacity dynvector) k)
    (dynvector-grow! dynvector))
  (set-dynvector-cached-length! dynvector #f)
  (vector-set! (dynvector-data dynvector) k obj))

(define (dynvector-grow! dynvector)
  (let* ((vec1 (dynvector-data dynvector))
         (end1 (vector-length vec1))
         (vec2 (make-vector
                (* (fluid-ref dynvector-growth-factor) end1)
                *unspecified*)))
    (vector-move-left! vec1 0 end1 vec2 0)
    (set-dynvector-data! dynvector vec2)))

  reply	other threads:[~2012-06-11  9:13 UTC|newest]

Thread overview: 43+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-06-09 12:32 Growable arrays? David Kastrup
2012-06-09 14:43 ` Krister Svanlund
2012-06-09 17:35   ` David Kastrup
2012-06-11  4:23 ` Daniel Hartwig
2012-06-11  4:37   ` David Kastrup
2012-06-11  5:00     ` Daniel Hartwig
2012-06-11  7:25       ` David Kastrup
2012-06-11  9:01         ` Daniel Hartwig
2012-06-11  9:13           ` Daniel Hartwig [this message]
2012-06-11 10:38             ` David Kastrup
2012-06-11 11:57               ` Daniel Hartwig
2012-06-11 12:13         ` Noah Lavine
2012-06-11 12:28           ` David Kastrup
2012-06-11 23:50             ` Mark H Weaver
2012-06-12  9:34               ` David Kastrup
2012-06-12 20:34                 ` Mark H Weaver
2012-06-12 20:47                   ` David Kastrup
2012-06-12 21:03                     ` Mark H Weaver
2012-06-12 21:18                       ` David Kastrup
2012-06-11  8:14 ` Thien-Thi Nguyen
2012-06-11  9:08 ` Andy Wingo
2012-06-11  9:55   ` David Kastrup
2012-06-11 11:25     ` Andy Wingo
2012-06-11 12:00       ` David Kastrup
2012-06-11 12:12         ` David Kastrup
2012-06-11 12:20           ` David Kastrup
2012-06-11 13:04             ` Daniel Hartwig
2012-06-11 14:19               ` David Kastrup
2012-06-11 15:24                 ` Stefan Israelsson Tampe
2012-06-11 15:27                 ` Andy Wingo
2012-06-11 16:03                   ` David Kastrup
2012-06-11 12:20         ` Daniel Hartwig
2012-06-11 12:36           ` David Kastrup
2012-06-11 12:02 ` Ludovic Courtès
2012-06-12 13:36 ` Hans Aberg
2012-06-14 14:33 ` Mark H Weaver
2012-06-14 14:47   ` David Kastrup
2012-06-14 15:23     ` Daniel Hartwig
2012-06-14 15:34       ` David Kastrup
2012-06-14 16:56         ` Daniel Hartwig
2012-06-14 17:15           ` David Kastrup
2012-06-14 17:23             ` Daniel Hartwig
2012-06-14 17:49               ` David Kastrup

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAN3veRerWrskD4qKpe2J=kakQeq-pAzEOr9xTSHhhCEwb-3uew@mail.gmail.com' \
    --to=mandyke@gmail.com \
    --cc=guile-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).