|
|
|
/*
|
|
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
|
|
|
*/
|
|
|
|
#import <Foundation/Foundation.h>
|
|
|
|
|
[cost] add NSArray iterator
Summary:
As title.
This diff is co-authored by SungKeun Cho and me.
facebook
This diff is co-authored by skcho and me.
Original comments from skcho
"For the record:
1. Rory and I tried to write models for ObjC iterator.
2. We could not use Java's iterator semantics: In Java's, `hasNext` returns the size of collection, rather than a boolean, and which is used as a control variable. On the other hand, in ObjC, it calls only `nextObject`, not calling `hasNext`, and the return value of which is being checked as `null`.
3. We added an artificial field `objc_iterator_offset` to keep the index of the iterator, and the models added in this diff are handling that integer value.
A problem is that `array.objc_iterator_offset` is not included in the control variables, since the condition of the loop is `nextObject() != null` that does not include the iterator offset. We need to make `array.objc_iterator_offset` as a control variable, by changing the part collecting control variables.
"
Reviewed By: ezgicicek, skcho
Differential Revision: D22944278
fbshipit-source-id: 7e71b79c1
4 years ago
|
|
|
NSInteger block_multiply_array_linear(NSArray* array) {
|
|
|
|
NSInteger (^sum_array)(NSArray*) = ^(NSArray* array) {
|
|
|
|
NSInteger n = 0;
|
|
|
|
for (id value in array) {
|
|
|
|
n += [value integerValue];
|
|
|
|
}
|
|
|
|
return n;
|
|
|
|
};
|
|
|
|
|
|
|
|
return sum_array(array);
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef void (^BlockA)(void);
|
|
|
|
void loop_linear(int x) {
|
|
|
|
for (int i = 0; i < x; i++) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void runBlockA(BlockA block) { block(); }
|
|
|
|
|
|
|
|
void doBlockA_linear(int a) {
|
|
|
|
BlockA block = ^{
|
|
|
|
loop_linear(a);
|
|
|
|
};
|
|
|
|
runBlockA(block);
|
|
|
|
}
|
|
|
|
|
|
|
|
void doBlockA_direct_block_linear(int a) {
|
|
|
|
runBlockA(^{
|
|
|
|
loop_linear(a);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void wrapper_runBlockA(BlockA block) { runBlockA(block); }
|
|
|
|
|
|
|
|
void call_wrapper_runBlockA_linear(int a) {
|
|
|
|
wrapper_runBlockA(^{
|
|
|
|
loop_linear(a);
|
|
|
|
});
|
|
|
|
}
|