]> gitweb.factorcode.org Git - factor.git/blob - basis/cocoa/enumeration/enumeration.factor
Merge branch 'master' of git://factorcode.org/git/factor
[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: kernel cocoa cocoa.types alien.c-types locals math
4 sequences vectors fry libc destructors
5 specialized-arrays.direct.alien ;
6 IN: cocoa.enumeration
7
8 CONSTANT: NS-EACH-BUFFER-SIZE 16
9
10 : with-enumeration-buffers ( quot -- )
11     [
12         [
13             "NSFastEnumerationState" malloc-object &free
14             NS-EACH-BUFFER-SIZE "id" heap-size * malloc-object &free
15             NS-EACH-BUFFER-SIZE
16         ] dip call
17     ] with-destructors ; inline
18
19 :: (NSFastEnumeration-each) ( object quot: ( elt -- ) state stackbuf count -- )
20     object state stackbuf count -> countByEnumeratingWithState:objects:count:
21     dup 0 = [ drop ] [
22         state NSFastEnumerationState-itemsPtr [ stackbuf ] unless*
23         swap <direct-void*-array> quot each
24         object quot state stackbuf count (NSFastEnumeration-each)
25     ] if ; inline recursive
26
27 : NSFastEnumeration-each ( object quot -- )
28     [ (NSFastEnumeration-each) ] with-enumeration-buffers ; inline
29
30 : NSFastEnumeration-map ( object quot -- vector )
31     NS-EACH-BUFFER-SIZE <vector>
32     [ '[ @ _ push ] NSFastEnumeration-each ] keep ; inline
33
34 : NSFastEnumeration>vector ( object -- vector )
35     [ ] NSFastEnumeration-map ;