]> gitweb.factorcode.org Git - factor.git/blob - basis/cocoa/enumeration/enumeration.factor
move some allocation words that don't really have much to do with c types out of...
[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 kernel classes.struct cocoa cocoa.runtime cocoa.types alien.data
4 locals math sequences vectors fry libc destructors specialized-arrays ;
5 SPECIALIZED-ARRAY: id
6 IN: cocoa.enumeration
7
8 CONSTANT: NS-EACH-BUFFER-SIZE 16
9
10 : with-enumeration-buffers ( quot -- )
11     '[
12         NSFastEnumerationState malloc-struct &free
13         NS-EACH-BUFFER-SIZE id malloc-array &free
14         NS-EACH-BUFFER-SIZE
15         @
16     ] with-destructors ; inline
17
18 :: (NSFastEnumeration-each) ( object quot: ( elt -- ) state stackbuf count -- )
19     object state stackbuf count -> countByEnumeratingWithState:objects:count: :> items-count
20     items-count 0 = [
21         state itemsPtr>> [ items-count id <c-direct-array> ] [ stackbuf ] if* :> items
22         items-count iota [ items nth quot call ] each
23         object quot state stackbuf count (NSFastEnumeration-each)
24     ] unless ; inline recursive
25
26 : NSFastEnumeration-each ( object quot -- )
27     [ (NSFastEnumeration-each) ] with-enumeration-buffers ; inline
28
29 : NSFastEnumeration-map ( object quot -- vector )
30     NS-EACH-BUFFER-SIZE <vector>
31     [ '[ @ _ push ] NSFastEnumeration-each ] keep ; inline
32
33 : NSFastEnumeration>vector ( object -- vector )
34     [ ] NSFastEnumeration-map ;