Skip to content

Instantly share code, notes, and snippets.

View LucianoPAlmeida's full-sized avatar
💭
Daniel 11:32

Luciano Almeida LucianoPAlmeida

💭
Daniel 11:32
View GitHub Profile
@LucianoPAlmeida
LucianoPAlmeida / COW.swift
Last active March 27, 2021 07:55
Copy-on-Write
import Foundation
func print(address o: UnsafeRawPointer ) {
print(String(format: "%p", Int(bitPattern: o)))
}
var array1: [Int] = [0, 1, 2, 3]
var array2 = array1
//Print with just assign
@LucianoPAlmeida
LucianoPAlmeida / COW-Impl.swift
Created November 19, 2017 01:18
Swift example of How to implement COW for custom value types.
final class Ref<T> {
var val : T
init(_ v : T) {val = v}
}
struct Box<T> {
var ref : Ref<T>
init(_ x : T) { ref = Ref(x) }
var value: T {
//Example of a runtime check stubbing
class User {
}
enum ResultType<T> {
case success(T)
case error(Error)
}
import UIKit
class User {
}
enum ResultType<T> {
case success(T)
case error(Error)
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
int main(void)
{
int x = INT_MAX;
x += 1; // Integer overflow here
printf("x = %i\n", x);
return 0;
}
@LucianoPAlmeida
LucianoPAlmeida / CPFValidator.swift
Last active August 4, 2018 01:47
CPF Validator in swift
struct CPFValidator {
var cpf: String
// Based on:
// https://pt.stackoverflow.com/questions/187106/valida%C3%A7%C3%A3o-cpf-e-cnpj
func validate() -> Bool {
let numbers = cpf.compactMap { (char) -> Int? in
return Int(char)
}
guard numbers.count == 11 && Set(numbers).count != 1 else { return false }
@LucianoPAlmeida
LucianoPAlmeida / BFS.swift
Created April 16, 2018 01:44
Understanding and implementing a BFS algorithm.
class Node: CustomStringConvertible {
var tagged: Bool
var name: String = ""
var subnodes: [Node] = []
init(tagged: Bool, name: String = "") {
self.tagged = tagged
self.name = name
}
class Event {
var count: Int = 0
let lock = NSLock()
func start() {
lock.lock()
count += 1
lock.unlock()
}
import Foundation
struct Node {
var childs: [String] = []
var lock = NSRecursiveLock()
init() {}
mutating func clearChilds() {
import Foundation
struct Node {
var childs: [String] = []
init() {}
mutating func clearChilds() {
if childs.isEmpty {
childs.removeAll()