Skip to content

Instantly share code, notes, and snippets.

View k06a's full-sized avatar
🚀
DeFi dreamer

Anton Bukov k06a

🚀
DeFi dreamer
View GitHub Profile
@k06a
k06a / UIImage+ResizableFallback.m
Last active December 13, 2015 21:18 — forked from arbales/UIImage_ResizableFallback.m
Portable version of resizable UIImage constructor
@interface UIImage (ResizeableFallback)
- (UIImage *)resizableImageWithInsets:(UIEdgeInsets *)insets;
@end
@implementation
- (UIImage *)resizableImageWithInsets:(UIEdgeInsets *)insets
{
if ([UIImage respondsToSelector:@selector(resizableImageWithCapInsets:)])
return [self resizableImageWithCapInsets: insets];
return [self stretchableImageWithLeftCapWidth:insets.left topCapWidth:insets.top];
}
@k06a
k06a / NSNull+SelfOrNil.h
Last active January 5, 2016 18:55
Returns nil if equal to [NSNull null] else return self
//
// Usage: `[item.self anyMethod]` is valid for `item` equals to `[NSNull null]`
//
@interface NSNull (SelfOrNil)
@end
@k06a
k06a / log2
Created August 18, 2013 12:44
Fast log2 by reversing and then counting trailing bits - see run result at http://codepad.org/MQeUVaiK
// Copyright 2013 (c) Anton Bukov - https://github.com/k06a
// Inspired by http://graphics.stanford.edu/~seander/bithacks.html
#define P0(a) (a)
#define P1(a) (((P0(a) & 0x55555555) << 1) | ((P0(a) & 0xAAAAAAAA) >> 1))
#define P2(a) (((P1(a) & 0x33333333) << 2) | ((P1(a) & 0xCCCCCCCC) >> 2))
#define P3(a) (((P2(a) & 0x0F0F0F0F) << 4) | ((P2(a) & 0xF0F0F0F0) >> 4))
#define P4(a) (((P3(a) & 0x00FF00FF) << 8) | ((P3(a) & 0xFF00FF00) >> 8))
#define P5(a) (((P4(a) & 0x0000FFFF) << 16) | ((P4(a) & 0xFFFF0000) >> 16))
@k06a
k06a / BinSearch
Last active December 21, 2015 06:08
Binary search template function with searching nearest greater.
template<typename TI, typename TV>
int BinSearch(TI begin, TI end, TV value)
{
int minIndex = 0;
int maxIndex = end - begin - 1;
while (minIndex <= maxIndex)
{
int midIndex = (minIndex + maxIndex) / 2;
TI comparison = begin + midIndex;
@k06a
k06a / kick.c
Last active January 4, 2016 19:09
Central and non-central kick
//
// Here is some illustration of central kick:
// https://commons.wikimedia.org/wiki/File:Elastischer_stoß3.gif
//
// V -V
// [m = 2]---> <---[m = 1]
// [m = 2] [m = 1]
// [m = 2][m = 1]
// [m = 2] [m = 1]
// <-[m = 2] [m = 1]----->
@interface NSIndexPath (SharedIndexPath)
@end
@implementation NSIndexPath (SharedIndexPath)
+ (instancetype)sharedIndexPathForRow:(NSInteger)row inSection:(NSInteger)section
{
static NSIndexPath *arr[10][10] = {nil};
if (arr[row][section] == nil)
arr[row][section] = [NSIndexPath indexPathForRow:row inSection:section];
return arr[row][section];
}
func bind1<T1,V>(f: (T1)->V, t1: T1) -> ()->V
{
return { () -> V in f(t1) }
}
func bind1<T1,T2,V>(f: (T1,T2)->V, t1: T1) -> (T2)->V
{
return { t2 -> V in f(t1,t2) }
}
prefix func !<T>(f: (T)->Bool) -> (T)->Bool
{
return { !f($0) }
}
func &&<T>(f1: (T)->Bool, f2: (T)->Bool) -> (T)->Bool
{
return { f1($0) && f2($0) }
}
@k06a
k06a / SwiftHaskellElem
Created August 30, 2014 23:01
Swift implementation of Haskell elem function
func elem<T: Equatable>(item: @autoclosure ()->T) -> (@autoclosure ()->[T]) -> Bool {
return { (arr: @autoclosure ()->[T]) in find(arr(), item()) != nil }
}
let found = elem (2) ([1,2,3]) // Hope to avoid braces in future
Before iOS 8, you do this:
```
[backgroundViewController setModalPresentationStyle:UIModalPresentationCurrentContext];
[backgroundViewController presentViewController:_myMoreAppsViewController animated:NO completion:nil];
```
in iOS 8, you have to do this:
```
backgroundViewController.providesPresentationContextTransitionStyle = YES;
backgroundController.definesPresentationContext = YES;
[overlayViewController setModalPresentationStyle:UIModalPresentationOverCurrentContext];