]> gitweb.factorcode.org Git - factor.git/blob - basis/cocoa/enumeration/enumeration.factor
Switch to https urls
[factor.git] / basis / cocoa / enumeration / enumeration.factor
1 ! Copyright (C) 2008 Joe Groff.
2 ! See https://factorcode.org/license.txt for BSD license.
3 USING: accessors alien.data assocs classes.struct cocoa
4 cocoa.runtime cocoa.types destructors hashtables kernel libc
5 sequences specialized-arrays vectors ;
6 IN: cocoa.enumeration
7 SPECIALIZED-ARRAY: id
8 IN: cocoa.enumeration
9
10 CONSTANT: NS-EACH-BUFFER-SIZE 16
11
12 : with-enumeration-buffers ( quot -- )
13     '[
14         NSFastEnumerationState malloc-struct &free
15         NS-EACH-BUFFER-SIZE id malloc-array &free
16         NS-EACH-BUFFER-SIZE
17         @
18     ] with-destructors ; inline
19
20 :: (NSFastEnumeration-each) ( ... object quot: ( ... elt -- ) state stackbuf count -- ... )
21     object state stackbuf count -> countByEnumeratingWithState:objects:count: :> items-count
22     items-count 0 = [
23         state itemsPtr>> [ items-count id <c-direct-array> ] [ stackbuf ] if* :> items
24         items-count <iota> [ items nth quot call ] each
25         object quot state stackbuf count (NSFastEnumeration-each)
26     ] unless ; inline recursive
27
28 : NSFastEnumeration-each ( ... object quot: ( ... elt -- ... ) -- ... )
29     [ (NSFastEnumeration-each) ] with-enumeration-buffers ; inline
30
31 : NSFastEnumeration-map ( ... object quot: ( ... elt -- ... newelt ) -- ... vector )
32     NS-EACH-BUFFER-SIZE <vector>
33     [ '[ @ _ push ] NSFastEnumeration-each ] keep ; inline
34
35 : NSFastEnumeration>vector ( object -- vector )
36     [ ] NSFastEnumeration-map ;
37
38 : NSFastEnumeration>hashtable ( ... object quot: ( ... elt -- ... key value ) -- ... vector )
39     NS-EACH-BUFFER-SIZE <hashtable>
40     [ '[ @ swap _ set-at ] NSFastEnumeration-each ] keep ; inline