You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
132 lines
3.5 KiB
132 lines
3.5 KiB
5 years ago
|
/*
|
||
|
* 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>
|
||
|
|
||
|
// init array
|
||
|
|
||
|
void nsarray_init_constant_FP() {
|
||
|
NSArray* array = [[NSArray alloc] init];
|
||
|
|
||
|
for (int i = 0; i < array.count; i++) {
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void nsarray_init_with_array_linear_FP(NSArray* array) {
|
||
|
NSArray* ref_array = [[NSArray alloc] initWithArray:array];
|
||
|
|
||
|
for (int i = 0; i < ref_array.count; i++) {
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void nsarray_init_with_array_constant_FP() {
|
||
|
NSArray* array = [[NSArray alloc] init];
|
||
|
nsarray_init_with_array_linear_FP(array);
|
||
|
}
|
||
|
|
||
|
void nsarray_init_with_array_copy_linear_FP(NSArray* array) {
|
||
|
NSArray* copy_array = [[NSArray alloc] initWithArray:array copyItems:YES];
|
||
|
for (int i = 0; i < copy_array.count; i++) {
|
||
|
}
|
||
|
}
|
||
|
|
||
|
NSArray* nsarray_init_with_objects_constant() {
|
||
|
NSString* strings[3];
|
||
|
strings[0] = @"First";
|
||
|
strings[1] = @"Second";
|
||
|
strings[2] = @"Third";
|
||
|
|
||
|
return [NSArray arrayWithObjects:strings count:2];
|
||
|
}
|
||
|
|
||
|
// derive new array
|
||
|
|
||
|
NSArray* nsarray_add_object_constant(id obj) {
|
||
|
NSArray* array = [[NSArray alloc] init];
|
||
|
return [array arrayByAddingObject:obj];
|
||
|
}
|
||
|
|
||
|
NSArray* nsarray_add_objects_from_array_linear_FN(NSArray* append_array) {
|
||
|
NSArray* array = [[NSArray alloc] init];
|
||
|
return [array arrayByAddingObjectsFromArray:append_array];
|
||
|
}
|
||
|
|
||
|
// query element
|
||
|
|
||
|
void nsarray_access_constant() {
|
||
|
NSArray* array = @[ @1.0f, @2.0f, @3.0f, @4.0f, @5.0f, @6.0f, @7.0f, @8.0f ];
|
||
|
for (int i = 0; i < 4; i++) {
|
||
|
[array objectAtIndex:i];
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void nsarray_access_linear_FP(NSArray* array) {
|
||
|
id obj;
|
||
|
for (int i = 0; i < array.count; i++) {
|
||
|
obj = array[i];
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void nsarray_contains_object_linear_FN(NSArray* array) {
|
||
|
[array containsObject:@1];
|
||
|
}
|
||
|
|
||
|
id nsarray_first_object_constant(NSArray* array) { return array.firstObject; }
|
||
|
|
||
|
id nsarray_last_object_constant(NSArray* array) { return array.lastObject; }
|
||
|
|
||
|
// find element
|
||
|
NSInteger nsarray_binary_search_log_FN(NSArray* sorted_array) {
|
||
|
NSNumber* target = @5;
|
||
|
return [sorted_array indexOfObject:target
|
||
|
inSortedRange:NSMakeRange(0, sorted_array.count)
|
||
|
options:NSBinarySearchingFirstEqual
|
||
|
usingComparator:^(id lhs, id rhs) {
|
||
|
return [lhs compare:rhs];
|
||
|
}];
|
||
|
}
|
||
|
|
||
|
// sort array
|
||
|
|
||
|
NSArray* nsarray_sort_using_descriptors_constant() {
|
||
|
NSArray* array = @[ @"Grapes", @"Apples", @"Oranges" ];
|
||
|
NSSortDescriptor* sd = [[NSSortDescriptor alloc] initWithKey:nil
|
||
|
ascending:YES];
|
||
|
return [array sortedArrayUsingDescriptors:@[ sd ]];
|
||
|
}
|
||
|
|
||
|
NSArray* nsarray_sort_using_descriptors_nlogn_FN(NSArray* array) {
|
||
|
NSSortDescriptor* sd = [[NSSortDescriptor alloc] initWithKey:nil
|
||
|
ascending:YES];
|
||
|
return [array sortedArrayUsingDescriptors:@[ sd ]];
|
||
|
}
|
||
|
|
||
|
// iterate through array
|
||
|
|
||
|
void nsarray_iterate_linear_FN(NSArray* array) {
|
||
|
NSInteger sum = 0;
|
||
|
for (id obj in array) {
|
||
|
sum += (NSInteger)obj;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void nsarray_enumerator_linear_FN(NSArray* array) {
|
||
|
NSEnumerator* enumerator = [array objectEnumerator];
|
||
|
|
||
|
id obj;
|
||
|
NSInteger sum = 0;
|
||
|
|
||
|
while (obj = [enumerator nextObject]) {
|
||
|
sum += (NSInteger)obj;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// compare array
|
||
|
boolean_t nsarray_is_equal_to_array_linear_FN(NSArray* array1,
|
||
|
NSArray* array2) {
|
||
|
return [array1 isEqualToArray:array2];
|
||
|
}
|