Skip to content

Instantly share code, notes, and snippets.

@codelynx
Created May 16, 2016 18:33
Show Gist options
  • Select an option

  • Save codelynx/cdb3e36f9bd965e23963330e90699f3d to your computer and use it in GitHub Desktop.

Select an option

Save codelynx/cdb3e36f9bd965e23963330e90699f3d to your computer and use it in GitHub Desktop.
Class version of: Array, Set, Dictionary
//
// ZCollection.swift
// ZKit
//
// Created by Kaz Yoshikawa on 2015/01/12.
// Copyright (c) 2015 Kaz Yoshikawa. All rights reserved.
//
// This software may be modified and distributed under the terms
// of the MIT license.
//
import Foundation
// Array, Set, Dictionary
//
// Array, Set, Dictionary is struct in Swift. But you may want class version of those
// in some cases. Following classes are the wrapper of Array, Set, Dictionary.
// MARK: - ZArray
class ZArray<T>: CollectionType {
typealias Index = Int
private var _array: Array<T>
init() {
_array = Array<T>()
}
init(_ array: [T]) {
_array = array
}
var count: Int {
return _array.count
}
func append(element: T) {
_array.append(element)
}
func insert(element: T, atIndex index: Int) {
_array.insert(element, atIndex: index)
}
func removeAll() {
_array.removeAll()
}
func removeAtIndex(index: Int) {
_array.removeAtIndex(index)
}
func removeLast() {
_array.removeLast()
}
// collection type
subscript(index: Int) -> T {
return _array[index]
}
var startIndex: Index { return _array.startIndex }
var endIndex: Index { return _array.endIndex }
}
// MARK: - ZSet
class ZSet<T: Hashable>: CollectionType {
typealias Index = SetIndex<T>
private var _set: Set<T>
init() {
_set = Set<T>()
}
init(_ set: Set<T>) {
_set = set
}
init(_ array: [T]) {
_set = Set<T>(array)
}
func contains(item: T) -> Bool {
return _set.contains(item)
}
func exclusiveOr<S: SequenceType where S.Generator.Element == T>(sequence: S) -> Set<T> {
return _set.exclusiveOr(sequence)
}
func exclusiveOrInPlace<S : SequenceType where S.Generator.Element == T>(sequence: S) {
_set.exclusiveOrInPlace(sequence)
}
func indexOf(item: T) -> SetIndex<T>? {
return _set.indexOf(item)
}
func insert(item: T) {
_set.insert(item)
}
func intersect<S : SequenceType where S.Generator.Element == T>(sequence: S) -> Set<T> {
return _set.intersect(sequence)
}
func intersectInPlace<S : SequenceType where S.Generator.Element == T>(sequence: S) {
return _set.intersectInPlace(sequence)
}
func isDisjointWith<S : SequenceType where S.Generator.Element == T>(sequence: S) -> Bool {
return _set.isDisjointWith(sequence)
}
func isStrictSubsetOf<S : SequenceType where S.Generator.Element == T>(sequence: S) -> Bool {
return _set.isStrictSubsetOf(sequence)
}
func isStrictSupersetOf<S : SequenceType where S.Generator.Element == T>(sequence: S) -> Bool {
return _set.isStrictSupersetOf(sequence)
}
func isSubsetOf<S : SequenceType where S.Generator.Element == T>(sequence: S) -> Bool {
return _set.isSubsetOf(sequence)
}
func isSupersetOf<S : SequenceType where S.Generator.Element == T>(sequence: S) -> Bool {
return _set.isSupersetOf(sequence)
}
func popFirst() -> T? {
return _set.popFirst()
}
func remove(item: T) -> T? {
return _set.remove(item)
}
func removeAll() {
_set.removeAll()
}
func removeAtIndex(index: SetIndex<T>) -> T {
return _set.removeAtIndex(index)
}
func removeFirst() -> T {
return _set.removeFirst()
}
func subtract<S : SequenceType where S.Generator.Element == T>(sequence: S) -> Set<T> {
return _set.subtract(sequence)
}
func subtractInPlace<S : SequenceType where S.Generator.Element == T>(sequence: S) {
_set.subtractInPlace(sequence)
}
func union<S : SequenceType where S.Generator.Element == T>(sequence: S) -> Set<T> {
return _set.union(sequence)
}
func unionInPlace<S : SequenceType where S.Generator.Element == T>(sequence: S) {
_set.union(sequence)
}
subscript (position: SetIndex<T>) -> T {
return _set[position]
}
var startIndex: Index { return _set.startIndex }
var endIndex: Index { return _set.endIndex }
}
// MARK: - ZDictionary
class ZDictionary<K: Hashable, V>: CollectionType {
typealias Element = (K, V)
typealias Index = DictionaryIndex<K, V>
var _dictionary: Dictionary<K, V>
init() {
_dictionary = Dictionary<K, V>()
}
func removeAll() {
_dictionary.removeAll()
}
subscript (key: K) -> V? {
get { return _dictionary[key] }
set { _dictionary[key] = newValue }
}
subscript (position: Index) -> Element {
return _dictionary[position]
}
var count: Int { return _dictionary.count }
var startIndex: Index { return _dictionary.startIndex }
var endIndex: Index { return _dictionary.endIndex }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment