Skip to content

Instantly share code, notes, and snippets.

@nhatlee
nhatlee / AtomicInteger.swift
Last active May 24, 2018 13:08 — forked from nestserau/AtomicInteger.swift
Atomic way to operate on integers in Swift. Inspired by Java's AtomicInteger
/// This is free and unencumbered software released into the public domain.
///
/// Anyone is free to copy, modify, publish, use, compile, sell, or
/// distribute this software, either in source code form or as a compiled
/// binary, for any purpose, commercial or non-commercial, and by any
/// means.
///
/// In jurisdictions that recognize copyright laws, the author or authors
/// of this software dedicate any and all copyright interest in the
/// software to the public domain. We make this dedication for the benefit
@nhatlee
nhatlee / PSPDFThreadSafeMutableDictionary.m
Created December 11, 2017 06:42 — forked from steipete/PSPDFThreadSafeMutableDictionary.m
Simple implementation of a thread safe mutable dictionary. In most cases, you want NSCache instead, but it can be useful in situations where you want to manually control what is evicted from the cache in low memory situations.**Warning:** I only use this for setting/getting keys. Enumeration is not thread safe here and will still throw exception…
//
// PSPDFThreadSafeMutableDictionary.m
//
// Copyright (c) 2013 Peter Steinberger, PSPDFKit GmbH. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
@nhatlee
nhatlee / PMThreadSafeMutableArray.m
Created January 19, 2018 06:20
Thread safe for NSMutableArray
//
// PMThreadSafeMutableArray.m
//
// Created by nhatlee on 1/19/18.
// Copyright © 2018 nhatlee. All rights reserved.
//
#import "PMThreadSafeMutableArray.h"
#import <libkern/OSAtomic.h>
@nhatlee
nhatlee / genpass.swift
Created July 8, 2018 04:51 — forked from brennanMKE/genpass.swift
Generate a password with Swift
#!/usr/bin/env xcrun swift
import Foundation
public typealias CharactersArray = [Character]
public typealias CharactersHash = [CharactersGroup : CharactersArray]
public enum CharactersGroup {
case Letters
case Numbers
@nhatlee
nhatlee / UIView+Extension.swift
Created July 16, 2018 11:24 — forked from bishalg/UIView+Extension.swift
Swift UIView Extension for Inspectable CornerRadius, BorderWidth and BoarderColors etc
//
// UIView+Extension.swift
//
// Created by Bishal Ghimire on 4/30/16.
// Copyright © 2016 Bishal Ghimire. All rights reserved.
//
import UIKit
//
@nhatlee
nhatlee / ExpressionBalanced.Swift
Created August 1, 2018 06:35
ExpressionBalanced
import UIKit
let openBrackets: [Character] = ["(", "{", "["]
let closedBrackets: [Character] = [")", "}", "]"]
func ~=<T: Equatable>(pattern: [T], value: T) -> Bool {
return pattern.contains(value)
}
func isExpressionBalanced(_ expression: String) -> Bool {
@nhatlee
nhatlee / array_structure.swift
Created August 1, 2018 07:04 — forked from austinzheng/array_structure.swift
A horrendous example of custom pattern matching in Swift
// Playground - noun: a place where people can play
import Cocoa
enum Wildcard : NilLiteralConvertible {
case Single
case FromBeginning
case ToEnd
case Range(Int)
case Literal(Int)
import UIKit
import RxSwift
import RxCocoa
let requiredUserNameLength = 5
let requiredPasswordLength = 5
let limitUserNameLength = 20
class ValidationViewController: UIViewController {
protocol DogBreed {
typealias Level = Int
var breedName: String { get set }
var size: Level { get set }
var health: Level { get set }
var adaptability: Level { get set }
var intelligence: Level { get set }
var dogType: DogType { get set }
@nhatlee
nhatlee / gist:3459ee5b971b9a6fe2576b594ad113b1
Created November 29, 2018 04:02 — forked from Nyx0uf/gist:217d97f81f4889f4445a
UIImage scale using vImage
-(UIImage*)mmg_imageScaledToFitSize:(CGSize)fitSize
{
// Create a vImage_Buffer from the CGImage
CGImageRef sourceRef = self.CGImage;
vImage_Buffer srcBuffer;
vImage_CGImageFormat format = {
.bitsPerComponent = 8,
.bitsPerPixel = 32,
.colorSpace = NULL,
.bitmapInfo = (CGBitmapInfo)kCGImageAlphaFirst,