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