]> gitweb.factorcode.org Git - factor.git/blob - basis/cocoa/enumeration/enumeration.factor
765fb65ef2a43c527ebaa13c3075c219d83ad6ae
[factor.git] / basis / cocoa / enumeration / enumeration.factor
1 USING: kernel cocoa cocoa.types alien.c-types locals math sequences
2 vectors fry libc ;
3 IN: cocoa.enumeration
4
5 : NS-EACH-BUFFER-SIZE 16 ; inline
6
7 : (with-enumeration-buffers) ( quot -- )
8     "NSFastEnumerationState" heap-size swap '[
9         NS-EACH-BUFFER-SIZE "id" heap-size * [
10             NS-EACH-BUFFER-SIZE @
11         ] with-malloc
12     ] with-malloc ; inline
13
14 :: (NSFastEnumeration-each) ( object quot: ( elt -- ) state stackbuf count -- )
15     object state stackbuf count -> countByEnumeratingWithState:objects:count:
16     dup zero? [ drop ] [
17         state NSFastEnumerationState-itemsPtr [ stackbuf ] unless*
18         '[ , void*-nth quot call ] each
19         object quot state stackbuf count (NSFastEnumeration-each)
20     ] if ; inline recursive
21
22 : NSFastEnumeration-each ( object quot -- )
23     [ (NSFastEnumeration-each) ] (with-enumeration-buffers) ; inline
24
25 : NSFastEnumeration-map ( object quot -- vector )
26     NS-EACH-BUFFER-SIZE <vector>
27     [ '[ @ , push ] NSFastEnumeration-each ] keep ; inline
28
29 : NSFastEnumeration>vector ( object -- vector )
30     [ ] NSFastEnumeration-map ;