Skip to content

Instantly share code, notes, and snippets.

View theoknock's full-sized avatar

James Alan Bush theoknock

View GitHub Profile
@theoknock
theoknock / implementation.m
Last active January 3, 2022 06:35
Iterating a set of bits (bit mask, bit field). Useful for indexing arrays using bit values without actually using bit values as index values
typedef NS_OPTIONS(uint8_t, CaptureDeviceConfigurationControlPropertyBit) {
CaptureDeviceConfigurationControlPropertyBitTorchLevel = 1<< 0,
CaptureDeviceConfigurationControlPropertyBitLensPosition = 1<< 1,
CaptureDeviceConfigurationControlPropertyBitExposureDuration = 1<< 2,
CaptureDeviceConfigurationControlPropertyBitISO = 1<< 3,
CaptureDeviceConfigurationControlPropertyBitZoomFactor = 1<< 4,
};
typedef CaptureDeviceConfigurationControlPropertyBit CaptureDeviceConfigurationControlPropertyBitMask;
CaptureDeviceConfigurationControlPropertyBitMask mask = (CaptureDeviceConfigurationControlPropertyBitTorchLevel |
@theoknock
theoknock / button_group.m
Last active January 4, 2022 05:54
Button group (aka radio buttons). Creates an array of reusable UIButton objects, which, in this case, are retrievable by an enumerated property that corresponds to the index in the array. This allows each button to access the entire group of buttons, even in their respective event handlers (so, for example, if one button is selected, it could de…
static void (^print_debug)(const char *) = ^ (const char * str) {
static int counter;
printf("\n%d\t%s\n", ++counter, str);
};
static const UIButton * (^buttons[5])(void);
static const UIButton * (^(^(^button_group)(CaptureDeviceConfigurationControlPropertyBitMask))(CaptureDeviceConfigurationControlProperty))(void) = ^ (CaptureDeviceConfigurationControlPropertyBitMask property_bit_mask) {
print_debug("INIT BUTTONS");
for (int property_tag = 0; property_tag < 5; property_tag++) {
__block UIButton * (^button)(void);
@theoknock
theoknock / animation.m
Created January 5, 2022 09:43
Passing a block to the CADisplayLink displayLinkWithTarget: parameter. This allows for an animation to be fully contained within a single block (here, animation()), which can be inserted into any object it animates, and that has access to it
void (^(^(^animation)(void))(ControlState))(void) = ^{
static CGFloat starting_radius, ending_radius, radius_step, radius_val;
return ^{
starting_radius = radius;
ending_radius = CGRectGetMinX(control_view.layer.bounds);
radius_step = 0.0005; //(ending_radius - radius);
radius_val = starting_radius;
void (^eventHandlerBlock)(void) = ^{
radius_val += radius_step;
for (CaptureDeviceConfigurationControlProperty property = CaptureDeviceConfigurationControlPropertyTorchLevel; property < CaptureDeviceConfigurationControlPropertyNone; property++) {
@theoknock
theoknock / button_array.h
Last active April 19, 2022 21:09
Thread-safe NSObject array iteration/enumeration
#import <objc/runtime.h>
static __strong id _Nonnull buttons[5];
static __strong const id * _Nonnull (*buttons_t)[5] = &buttons;
static void (^(^map)(id * _Nonnull, const unsigned long))(const void *(^__strong)(const unsigned long)) = ^ (id * _Nonnull obj_collection, const unsigned long index_count) {
__block unsigned long (^recursive_block)(unsigned long);
return ^ (const void * (^enumeration)(const unsigned long)) {
(recursive_block = ^ unsigned long (unsigned long counter) {
obj_collection[counter] = (__bridge id)((__bridge const void * _Nonnull)CFBridgingRelease(enumeration(counter)));
@theoknock
theoknock / touch_handler.h
Created January 16, 2022 14:07
Touch handling that reuses the same call regardless of touch phase, and also reuses the same UITouch object passed to touchesBegan throughout the entire touch sequence. Uses bitwise operators to simultaneously detect a given phase and set a button property.
static void (^(^(^touch_handler_init)(UIView *))(UITouch *))(void) = ^ (UIView * view) {
CGRect contextRect = view.bounds;
float minX = (float)CGRectGetMinX(contextRect);
float midX = (float)CGRectGetMidX(contextRect);
float mdnX = (float)(((int)midX & (int)minX) + (((int)midX ^ (int)minX) >> 1)); // Average of two floats
.
.
.
return ^ (UITouch * touch) {
@theoknock
theoknock / bitwise_block_invoke.m
Last active February 1, 2022 17:11
Predicated block invocation. Per Wikipedia (predication, computer science): “[I]nstead of using a conditional branch to select an instruction or a sequence of instructions to execute based on the predicate that controls whether the branch occurs, the instructions to be executed are associated with that predicate, so that they will be executed, o…
// Uses XOR to determine whether 'number' is even or odd and...
// ...executes the corresponding block
// Note: the catch is, in order for the expression to be evaluated, the blocks must return long. Any arbitrary value will do.
const long(^ const even_number)(void) = ^ long(void) {
printf("even_number\n");
return 2;
};
const long(^ const odd_number)(void) = ^ long(void) {
@theoknock
theoknock / add_arrays.m
Last active January 27, 2022 23:10
Using vDSP and Objective-C to add arrays of floats. This is the Objective-C version of the Swift example(s) for using vDSP for vector processing.
static void (^add_float_arrays)(void) = ^{
float a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
float b[] = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100};
float c[] = {[0 ... 9] = NAN};
vDSP_vadd(a, 1, b, 1, c, 1, 10);
for (int i = 0; i < 10; i++) printf("%f\n", c[i]);
/*
Console output:
@theoknock
theoknock / bitwise_branching.m
Last active February 5, 2022 21:52
Bitwise branching
static long (^(^animate)(long))(void(^__strong)(long *)) = ^ (long duration) {
__block typeof(CADisplayLink *) display_link;
__block long frames = duration;
return ^ long (void (^__strong animator)(long *)) {
display_link = [CADisplayLink displayLinkWithTarget:^{
frames >>= 01;
return
((frames & 01) &&
^ long {
@theoknock
theoknock / find_set_bit_position.m
Last active March 4, 2022 18:44
Get the integer position of the set bit in a bit vector (field)
// Example:
// 000100 (the value of bit vector x) returns 16 (use (x << 4) & 1UL to verify)
// To find the position of the set bit that returned 16,
// use floor(log2(16)) to get 4.
// Creates bit vector j and sets the ith bit out of 5…
// …returns the value of bit vector j, which is i raised to the power of two (2, 4, 8, 16, 32)…
// …returns the position of the set bit in bit vector j, which should coincide with i
//
for (int i = 1; i < 5; i++) {
@theoknock
theoknock / ViewController.m
Last active March 14, 2022 05:28
Composition of Predicate Blocks
//
// ViewController.m
// PredicateFunctionsExercise
//
// Created by James Alan Bush on 3/12/22.
//
#import "ViewController.h"
@import simd;