IN: unrolled-lists
USING: help.markup help.syntax hashtables search-deques dlists
-deques ;
+deques unrolled-lists.private ;
HELP: unrolled-list
-{ $class-description "The class of unrolled lists." } ;
+{ $class-description "The class of unrolled lists."
+ $nl
+ "All nodes in an unrolled list contain an array of 32 items. Nodes point to the previous"
+ " node and next node, or " { $link f } " if they do not exist."
+ { $slots
+ { "front" { "The front " { $link node } " of the list or " { $link f } "." } }
+ { "front-pos" { "The position of the front element of the list in "
+ { $snippet "front" } "." } }
+ { "back" { "The back " { $link node } " of the list or " { $link f } "." } }
+ { "back-pos" { "The position of the back element of the list in "
+ { $snippet "back" } "." } }
+ }
+ $nl
+ "It is not recommended to modify any of these slots manually. Using the "
+ { $link deque } " protocol provides safer operations."
+} ;
HELP: <unrolled-list>
{ $values { "list" unrolled-list } }
USING: unrolled-lists tools.test deques kernel sequences
-random prettyprint grouping math ;
+random prettyprint grouping math ranges ;
{ 1 } [ <unrolled-list> 1 over push-front pop-front ] unit-test
{ 1 } [ <unrolled-list> 1 over push-front pop-back ] unit-test
dup pop-back 30 assert=
deque-empty?
] unit-test
+
+! In relation to https://github.com/factor/factor/issues/2729
+10 [| |
+ <unrolled-list> :> l
+ [
+ 33 200 [a..b] random dup
+ l swap [1..b] [ over push-back ] each
+ swap [ dup pop-front drop ] times
+ dup pop-front swap pop-back
+ ] [ empty-deque? ] must-fail-with
+] times
-! Copyright (C) 2008 Slava Pestov.
+! Copyright (C) 2008, 2023 Slava Pestov and Raghu Ranganathan.
! See https://factorcode.org/license.txt for BSD license.
USING: arrays math kernel accessors sequences sequences.private
deques search-deques hashtables ;
dup prev>> [ drop ] [ swap front>> >>prev ] if
] [ dup front>> >>back ] if* drop ; inline
+: clear-if-empty ( list -- )
+ dup deque-empty? [ dup clear-deque ] when drop ;
+
: push-front/new ( elt list -- )
unroll-factor 1 - >>front-pos
[ <front-node> ] change-front
: pop-front/existing ( list front -- )
[ dup front-pos>> ] [ data>> ] bi* [ 0 ] 2dip set-nth-unsafe
[ 1 + ] change-front-pos
- drop ; inline
+ clear-if-empty ; inline
M: unrolled-list pop-front*
dup front>> [ empty-unrolled-list ] unless*
: pop-back/existing ( list back -- )
[ [ 1 - ] change-back-pos ] dip
[ dup back-pos>> ] [ data>> ] bi* [ 0 ] 2dip set-nth-unsafe
- drop ; inline
+ clear-if-empty ; inline
M: unrolled-list pop-back*
dup back>> [ empty-unrolled-list ] unless*