Skip to content

Instantly share code, notes, and snippets.

@joanromano
joanromano / Monoqueue.swift
Created September 15, 2019 07:27
An array based implementation of a Monoqueue
struct Monoqueue<T: Comparable> {
var array = [(value: T, count: Int)]()
var max: T {
return array.first!.value
}
mutating func push(_ value: T) {
var count = 0
while !array.isEmpty, array.last!.value < value {
@joanromano
joanromano / SegmentTree.swift
Created September 8, 2019 10:18
An array based implementation of a segment tree
extension Int {
fileprivate func nextPowerOf2() -> Int {
guard self != 0 else {
return 1
}
return 1 << (bitWidth - (self - 1).leadingZeroBitCount)
}
}
struct SegmentTree<T: Comparable> {
@joanromano
joanromano / PriorityQueue.swift
Created September 8, 2019 10:17
An array based implementation of a PriorityQueue
struct PriorityQueue<T> {
private var array: [T] = []
private let priority: (T, T) -> Bool
var count: Int { return array.count }
var isEmpty: Bool { return array.isEmpty }
var top: T? { return array.first }
init(_ values: [T], priority: @escaping (T, T) -> Bool) {
@joanromano
joanromano / Trie.swift
Created September 8, 2019 10:15
A basic trie implementation in Swift supporting prefix and pattern searches
class TrieNode {
var hash: [Character:TrieNode] = [:]
var endOfWord: Bool = false
}
class Trie {
var root = TrieNode()
init() {}
@joanromano
joanromano / Bisect.swift
Created September 8, 2019 10:10
Bisection searching algorithms
extension Array where Element: Comparable {
// This implementations are inspired in https://github.com/python/cpython/blob/master/Lib/bisect.py
/// Return the index where to insert value in the receiver, assuming the receiver is sorted
///
/// - Parameters:
/// - value: The position of the text in current context
/// - min: The lower bound where to perform the search
/// - max: The upper bound where to perform the search
/// - Returns: An index such that all elements in self[:i] have element < value, and all elements in
struct HeightedUnion {
typealias QuickUnionRootHeight = (root: Int, height: Int)
private var ids: [Int]
// Complexity: O(n)
init(_ N: Int) {
ids = Array(0..<N)
}
@joanromano
joanromano / gist:20f1732dd1dbb2dea5d7
Last active February 7, 2016 15:20
Recursive greatest common divider
func greatestCommonDivisor(first: Int, second: Int) -> Int {
return internalPrivateGreatestCommonDivisor(first, second, min(first, second))
}
private func internalPrivateGreatestCommonDivisor(first: Int, second: Int, divider: Int) -> Int {
if divider == 0 {
return 0
}
@joanromano
joanromano / gist:332eacddeb93aeb61b5e
Created June 1, 2015 09:17
Date formatter for ironhack course
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"yyyy-MM-dd'T'HH:mm:ss";
@joanromano
joanromano / gist:c12b476f087e22903c51
Created September 17, 2014 15:08
RAC UITextfields
@weakify(self)
[[self.firstTextField rac_signalForControlEvents:UIControlEventEditingDidEndOnExit]
subscribeNext:^(id x) {
@strongify(self)
[self.secondTextField becomeFirstResponder];
}];
@joanromano
joanromano / gist:7304803
Created November 4, 2013 16:02
Setting a UIImageView's image without using cornerRadius and masksToBounds
- (void)setImage:(UIImage *)image withCornerRadius:(CGFloat)cornerRadius
{
UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 1.0);
[[UIBezierPath bezierPathWithRoundedRect:self.bounds cornerRadius:cornerRadius] addClip];
[image drawInRect:self.bounds];
self.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}