M: f new-sequence drop dup zero? [ drop f ] [ f <array> ] if ;
-M: array like drop dup array? [ >array ] unless ;
-
M: array equal?
over array? [ sequence= ] [ 2drop f ] if ;
M: byte-array nth-unsafe swap >fixnum alien-unsigned-1 ;
M: byte-array set-nth-unsafe swap >fixnum set-alien-unsigned-1 ;
: >byte-array ( seq -- byte-array ) B{ } clone-like ; inline
-M: byte-array like drop dup byte-array? [ >byte-array ] unless ;
M: byte-array new-sequence drop <byte-array> ;
M: byte-array equal?
! Copyright (C) 2008 Slava Pestov.\r
! See http://factorcode.org/license.txt for BSD license.\r
USING: arrays kernel kernel.private math sequences\r
-sequences.private growable byte-arrays ;\r
+sequences.private growable byte-arrays accessors ;\r
IN: byte-vectors\r
\r
TUPLE: byte-vector\r
M: byte-vector equal?\r
over byte-vector? [ sequence= ] [ 2drop f ] if ;\r
\r
+M: byte-array like\r
+ #! If we have an byte-array, we're done.\r
+ #! If we have a byte-vector, and it's at full capacity,\r
+ #! we're done. Otherwise, call resize-byte-array, which is a\r
+ #! relatively fast primitive.\r
+ drop dup byte-array? [\r
+ dup byte-vector? [\r
+ [ length ] [ underlying>> ] bi\r
+ 2dup length eq?\r
+ [ nip ] [ resize-byte-array ] if\r
+ ] [ >byte-array ] if\r
+ ] unless ;\r
+\r
M: byte-array new-resizable drop <byte-vector> ;\r
\r
INSTANCE: byte-vector growable\r
M: string new-resizable drop <sbuf> ;
M: string like
+ #! If we have a string, we're done.
+ #! If we have an sbuf, and it's at full capacity, we're done.
+ #! Otherwise, call resize-string, which is a relatively
+ #! fast primitive.
drop dup string? [
dup sbuf? [
- dup length over underlying>> length eq? [
- underlying>> dup reset-string-hashcode
- ] [
- >string
- ] if
- ] [
- >string
- ] if
+ [ length ] [ underlying>> ] bi
+ 2dup length eq?
+ [ nip dup reset-string-hashcode ] [ resize-string ] if
+ ] [ >string ] if
] unless ;
INSTANCE: sbuf growable
! Copyright (C) 2004, 2008 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license.
-USING: arrays kernel math sequences sequences.private growable ;
+USING: arrays kernel math sequences sequences.private growable
+accessors ;
IN: vectors
TUPLE: vector
M: vector equal?
over vector? [ sequence= ] [ 2drop f ] if ;
+M: array like
+ #! If we have an array, we're done.
+ #! If we have a vector, and it's at full capacity, we're done.
+ #! Otherwise, call resize-array, which is a relatively
+ #! fast primitive.
+ drop dup array? [
+ dup vector? [
+ [ length ] [ underlying>> ] bi
+ 2dup length eq?
+ [ nip ] [ resize-array ] if
+ ] [ >array ] if
+ ] unless ;
+
M: sequence new-resizable drop <vector> ;
INSTANCE: vector growable