]> gitweb.factorcode.org Git - factor.git/blob - basis/boxes/boxes.factor
25f2b963b414cba8cbcb345b628ddf844ad15609
[factor.git] / basis / boxes / boxes.factor
1 ! Copyright (C) 2008, 2010 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: kernel accessors ;
4 IN: boxes
5
6 TUPLE: box value occupied ;
7
8 : <box> ( -- box ) box new ;
9
10 ERROR: box-full box ;
11
12 : >box ( value box -- )
13     dup occupied>>
14     [ box-full ] [ t >>occupied value<< ] if ; inline
15
16 ERROR: box-empty box ;
17
18 : check-box ( box -- box )
19     dup occupied>> [ box-empty ] unless ; inline
20
21 <PRIVATE
22
23 : box-unsafe> ( box -- value )
24     [ f ] change-value f >>occupied drop ; inline
25
26 PRIVATE>
27
28 : box> ( box -- value )
29     check-box box-unsafe> ; inline
30
31 : ?box ( box -- value/f ? )
32     dup occupied>> [ box-unsafe> t ] [ drop f f ] if ; inline
33
34 : if-box? ( box quot -- )
35     [ ?box ] dip [ drop ] if ; inline