Skip to content
This repository has been archived by the owner on May 19, 2018. It is now read-only.

Commit

Permalink
Merge pull request #47 from robrix/swift-1.2
Browse files Browse the repository at this point in the history
[BETA] Swift 1.2
  • Loading branch information
robrix committed Feb 12, 2015
2 parents 18b3d0e + 64ffcd5 commit 9674661
Show file tree
Hide file tree
Showing 12 changed files with 81 additions and 493 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@

[![Build status](https://api.travis-ci.org/robrix/Set.svg)](https://travis-ci.org/robrix/Set)

This is a Swift microframework which implements a Dictionary-backed Set.
This is a Swift microframework which implements a Dictionary-backed Multiset.

## Use

```swift
// Union
Set(1, 2, 3) + Set(3, 4, 5) // == Set(1, 2, 3, 4, 5)
Multiset(1, 2, 3) + Multiset(3, 4, 5) // == Multiset(1, 2, 3, 3, 4, 5)

// Difference
Set(1, 2, 3) - Set(2, 3) // == Set(1)
Multiset(1, 2, 3) - Multiset(2, 3) // == Multiset(1)

// Intersection
Set(1, 2, 3) & Set(3, 4, 5) // == Set(3)
Multiset(1, 2, 3) & Multiset(3, 4, 5) // == Multiset(3)
```

See [`Set.swift`][Set.swift] for more details.
See [`Multiset.swift`][Multiset.swift] for more details.

## Integration

Expand All @@ -32,4 +32,4 @@ See [`Set.swift`][Set.swift] for more details.

- [Greg Titus wrote a Set in Swift which inspired this](https://twitter.com/gregtitus/status/476420154230726656).

[Set.swift]: https://github.com/robrix/Set/blob/master/Set/Set.swift
[Multiset.swift]: https://github.com/robrix/Set/blob/master/Set/Multiset.swift
66 changes: 21 additions & 45 deletions Set.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,16 @@
objects = {

/* Begin PBXBuildFile section */
4ACE19971A3F7C1400A42022 /* SetInclusionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4ACE19961A3F7C1400A42022 /* SetInclusionTests.swift */; };
4ACE19991A3F7CA600A42022 /* SetHigherOrderFunctionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4ACE19981A3F7CA600A42022 /* SetHigherOrderFunctionTests.swift */; };
4AF6FA891A3EBBD500DDC8EE /* SetInitializerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4AF6FA881A3EBBD500DDC8EE /* SetInitializerTests.swift */; };
4ACE19991A3F7CA600A42022 /* MultisetHigherOrderFunctionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4ACE19981A3F7CA600A42022 /* MultisetHigherOrderFunctionTests.swift */; };
4AF6FA891A3EBBD500DDC8EE /* MultisetInitializerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4AF6FA881A3EBBD500DDC8EE /* MultisetInitializerTests.swift */; };
5405C50A1A87AEF2005142E4 /* Set.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 5405C4FF1A87AEF1005142E4 /* Set.framework */; };
5405C5181A87AEFB005142E4 /* Set.swift in Sources */ = {isa = PBXBuildFile; fileRef = D439CFC21957572E005BD842 /* Set.swift */; };
5405C5191A87AEFB005142E4 /* Unit.swift in Sources */ = {isa = PBXBuildFile; fileRef = D40D2667195F0C1100BC535B /* Unit.swift */; };
5405C51A1A87AEFE005142E4 /* Set.h in Headers */ = {isa = PBXBuildFile; fileRef = D439CFAB19575720005BD842 /* Set.h */; settings = {ATTRIBUTES = (Public, ); }; };
5405C51B1A87AF08005142E4 /* SetHigherOrderFunctionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4ACE19981A3F7CA600A42022 /* SetHigherOrderFunctionTests.swift */; };
5405C51C1A87AF08005142E4 /* SetInclusionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4ACE19961A3F7C1400A42022 /* SetInclusionTests.swift */; };
5405C51D1A87AF08005142E4 /* SetInitializerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4AF6FA881A3EBBD500DDC8EE /* SetInitializerTests.swift */; };
5405C51E1A87AF08005142E4 /* SetOperationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D439CFB819575721005BD842 /* SetOperationTests.swift */; };
5405C51F1A87AF08005142E4 /* SetPrintableTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95B1A51E1A529DF700A7CEE4 /* SetPrintableTests.swift */; };
95B1A51F1A529DF700A7CEE4 /* SetPrintableTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95B1A51E1A529DF700A7CEE4 /* SetPrintableTests.swift */; };
D40D2668195F0C1100BC535B /* Unit.swift in Sources */ = {isa = PBXBuildFile; fileRef = D40D2667195F0C1100BC535B /* Unit.swift */; };
5405C51B1A87AF08005142E4 /* MultisetHigherOrderFunctionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4ACE19981A3F7CA600A42022 /* MultisetHigherOrderFunctionTests.swift */; };
5405C51D1A87AF08005142E4 /* MultisetInitializerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4AF6FA881A3EBBD500DDC8EE /* MultisetInitializerTests.swift */; };
5405C51F1A87AF08005142E4 /* MultisetPrintableTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95B1A51E1A529DF700A7CEE4 /* MultisetPrintableTests.swift */; };
95B1A51F1A529DF700A7CEE4 /* MultisetPrintableTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95B1A51E1A529DF700A7CEE4 /* MultisetPrintableTests.swift */; };
D439CFAC19575720005BD842 /* Set.h in Headers */ = {isa = PBXBuildFile; fileRef = D439CFAB19575720005BD842 /* Set.h */; settings = {ATTRIBUTES = (Public, ); }; };
D439CFB219575721005BD842 /* Set.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D439CFA619575720005BD842 /* Set.framework */; };
D439CFB919575721005BD842 /* SetOperationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D439CFB819575721005BD842 /* SetOperationTests.swift */; };
D439CFC31957572E005BD842 /* Set.swift in Sources */ = {isa = PBXBuildFile; fileRef = D439CFC21957572E005BD842 /* Set.swift */; };
D4EAE24B1A8835C100931323 /* Multiset.swift in Sources */ = {isa = PBXBuildFile; fileRef = D4EAE24A1A8835C100931323 /* Multiset.swift */; };
D4EAE24D1A883EDD00931323 /* MultisetSequenceTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D4EAE24C1A883EDD00931323 /* MultisetSequenceTests.swift */; };
D4EAE24F1A8841BE00931323 /* MultisetCountTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D4EAE24E1A8841BE00931323 /* MultisetCountTests.swift */; };
Expand Down Expand Up @@ -61,20 +53,16 @@
/* End PBXContainerItemProxy section */

/* Begin PBXFileReference section */
4ACE19961A3F7C1400A42022 /* SetInclusionTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SetInclusionTests.swift; sourceTree = "<group>"; };
4ACE19981A3F7CA600A42022 /* SetHigherOrderFunctionTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SetHigherOrderFunctionTests.swift; sourceTree = "<group>"; };
4AF6FA881A3EBBD500DDC8EE /* SetInitializerTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SetInitializerTests.swift; sourceTree = "<group>"; };
4ACE19981A3F7CA600A42022 /* MultisetHigherOrderFunctionTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MultisetHigherOrderFunctionTests.swift; sourceTree = "<group>"; };
4AF6FA881A3EBBD500DDC8EE /* MultisetInitializerTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MultisetInitializerTests.swift; sourceTree = "<group>"; };
5405C4FF1A87AEF1005142E4 /* Set.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Set.framework; sourceTree = BUILT_PRODUCTS_DIR; };
5405C5091A87AEF2005142E4 /* Set-iOSTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "Set-iOSTests.xctest"; sourceTree = BUILT_PRODUCTS_DIR; };
95B1A51E1A529DF700A7CEE4 /* SetPrintableTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SetPrintableTests.swift; sourceTree = "<group>"; };
D40D2667195F0C1100BC535B /* Unit.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Unit.swift; sourceTree = "<group>"; };
95B1A51E1A529DF700A7CEE4 /* MultisetPrintableTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MultisetPrintableTests.swift; sourceTree = "<group>"; };
D439CFA619575720005BD842 /* Set.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Set.framework; sourceTree = BUILT_PRODUCTS_DIR; };
D439CFAA19575720005BD842 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
D439CFAB19575720005BD842 /* Set.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Set.h; sourceTree = "<group>"; };
D439CFB119575720005BD842 /* SetTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; name = SetTests.xctest; path = "Set-MacTests.xctest"; sourceTree = BUILT_PRODUCTS_DIR; };
D439CFB119575720005BD842 /* Set-MacTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "Set-MacTests.xctest"; sourceTree = BUILT_PRODUCTS_DIR; };
D439CFB719575721005BD842 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
D439CFB819575721005BD842 /* SetOperationTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SetOperationTests.swift; sourceTree = "<group>"; };
D439CFC21957572E005BD842 /* Set.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Set.swift; sourceTree = "<group>"; };
D4EAE24A1A8835C100931323 /* Multiset.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Multiset.swift; sourceTree = "<group>"; };
D4EAE24C1A883EDD00931323 /* MultisetSequenceTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MultisetSequenceTests.swift; sourceTree = "<group>"; };
D4EAE24E1A8841BE00931323 /* MultisetCountTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MultisetCountTests.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -133,7 +121,7 @@
isa = PBXGroup;
children = (
D439CFA619575720005BD842 /* Set.framework */,
D439CFB119575720005BD842 /* SetTests.xctest */,
D439CFB119575720005BD842 /* Set-MacTests.xctest */,
5405C4FF1A87AEF1005142E4 /* Set.framework */,
5405C5091A87AEF2005142E4 /* Set-iOSTests.xctest */,
);
Expand All @@ -144,9 +132,7 @@
isa = PBXGroup;
children = (
D439CFAB19575720005BD842 /* Set.h */,
D439CFC21957572E005BD842 /* Set.swift */,
D4EAE24A1A8835C100931323 /* Multiset.swift */,
D40D2667195F0C1100BC535B /* Unit.swift */,
D4EAE2521A88592F00931323 /* Hashing.swift */,
D4EAE2541A885A6F00931323 /* Printing.swift */,
D439CFA919575720005BD842 /* Supporting Files */,
Expand All @@ -165,15 +151,13 @@
D439CFB519575721005BD842 /* SetTests */ = {
isa = PBXGroup;
children = (
4ACE19981A3F7CA600A42022 /* SetHigherOrderFunctionTests.swift */,
4ACE19961A3F7C1400A42022 /* SetInclusionTests.swift */,
4AF6FA881A3EBBD500DDC8EE /* SetInitializerTests.swift */,
D439CFB819575721005BD842 /* SetOperationTests.swift */,
95B1A51E1A529DF700A7CEE4 /* SetPrintableTests.swift */,
D4EAE2561A88604A00931323 /* MultisetAlgebraTests.swift */,
D4EAE2501A88441000931323 /* MultisetCollectionTests.swift */,
D4EAE24E1A8841BE00931323 /* MultisetCountTests.swift */,
4ACE19981A3F7CA600A42022 /* MultisetHigherOrderFunctionTests.swift */,
D4EAE2581A8867E200931323 /* MultisetInclusionTests.swift */,
4AF6FA881A3EBBD500DDC8EE /* MultisetInitializerTests.swift */,
95B1A51E1A529DF700A7CEE4 /* MultisetPrintableTests.swift */,
D4EAE24C1A883EDD00931323 /* MultisetSequenceTests.swift */,
D439CFB619575721005BD842 /* Supporting Files */,
);
Expand Down Expand Up @@ -279,7 +263,7 @@
);
name = "Set-MacTests";
productName = SetTests;
productReference = D439CFB119575720005BD842 /* SetTests.xctest */;
productReference = D439CFB119575720005BD842 /* Set-MacTests.xctest */;
productType = "com.apple.product-type.bundle.unit-test";
};
/* End PBXNativeTarget section */
Expand Down Expand Up @@ -363,9 +347,7 @@
buildActionMask = 2147483647;
files = (
D4EAE25E1A88F8D600931323 /* Multiset.swift in Sources */,
5405C5181A87AEFB005142E4 /* Set.swift in Sources */,
D4EAE25F1A88F8D600931323 /* Hashing.swift in Sources */,
5405C5191A87AEFB005142E4 /* Unit.swift in Sources */,
D4EAE2601A88F8D600931323 /* Printing.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand All @@ -374,15 +356,13 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
5405C51D1A87AF08005142E4 /* SetInitializerTests.swift in Sources */,
5405C51D1A87AF08005142E4 /* MultisetInitializerTests.swift in Sources */,
D4EAE2621A88F8DE00931323 /* MultisetCollectionTests.swift in Sources */,
5405C51F1A87AF08005142E4 /* SetPrintableTests.swift in Sources */,
5405C51F1A87AF08005142E4 /* MultisetPrintableTests.swift in Sources */,
D4EAE2641A88F8DE00931323 /* MultisetInclusionTests.swift in Sources */,
5405C51B1A87AF08005142E4 /* SetHigherOrderFunctionTests.swift in Sources */,
5405C51B1A87AF08005142E4 /* MultisetHigherOrderFunctionTests.swift in Sources */,
D4EAE2611A88F8DE00931323 /* MultisetAlgebraTests.swift in Sources */,
5405C51E1A87AF08005142E4 /* SetOperationTests.swift in Sources */,
D4EAE2631A88F8DE00931323 /* MultisetCountTests.swift in Sources */,
5405C51C1A87AF08005142E4 /* SetInclusionTests.swift in Sources */,
D4EAE2651A88F8DE00931323 /* MultisetSequenceTests.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand All @@ -392,9 +372,7 @@
buildActionMask = 2147483647;
files = (
D4EAE24B1A8835C100931323 /* Multiset.swift in Sources */,
D439CFC31957572E005BD842 /* Set.swift in Sources */,
D4EAE2531A88592F00931323 /* Hashing.swift in Sources */,
D40D2668195F0C1100BC535B /* Unit.swift in Sources */,
D4EAE2551A885A6F00931323 /* Printing.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand All @@ -403,16 +381,14 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
4ACE19991A3F7CA600A42022 /* SetHigherOrderFunctionTests.swift in Sources */,
95B1A51F1A529DF700A7CEE4 /* SetPrintableTests.swift in Sources */,
D439CFB919575721005BD842 /* SetOperationTests.swift in Sources */,
4ACE19991A3F7CA600A42022 /* MultisetHigherOrderFunctionTests.swift in Sources */,
95B1A51F1A529DF700A7CEE4 /* MultisetPrintableTests.swift in Sources */,
D4EAE24F1A8841BE00931323 /* MultisetCountTests.swift in Sources */,
D4EAE24D1A883EDD00931323 /* MultisetSequenceTests.swift in Sources */,
D4EAE2591A8867E200931323 /* MultisetInclusionTests.swift in Sources */,
4ACE19971A3F7C1400A42022 /* SetInclusionTests.swift in Sources */,
D4EAE2571A88604A00931323 /* MultisetAlgebraTests.swift in Sources */,
D4EAE2511A88441000931323 /* MultisetCollectionTests.swift in Sources */,
4AF6FA891A3EBBD500DDC8EE /* SetInitializerTests.swift in Sources */,
4AF6FA891A3EBBD500DDC8EE /* MultisetInitializerTests.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
39 changes: 12 additions & 27 deletions Set/Multiset.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
public struct Multiset<Element: Hashable>: ArrayLiteralConvertible, ExtensibleCollectionType, Hashable, Printable, DebugPrintable {
// MARK: Constructors

/// Constructs a `Set` with the elements of `sequence`.
/// Constructs a `Multiset` with the elements of `sequence`.
public init<S: SequenceType where S.Generator.Element == Element>(_ sequence: S) {
self.init(values: [:])
extend(sequence)
}

/// Constructs a `Set` from a variadic parameter list.
/// Constructs a `Multiset` from a variadic parameter list.
public init(_ elements: Element...) {
self.init(elements)
}
Expand Down Expand Up @@ -56,25 +56,17 @@ public struct Multiset<Element: Hashable>: ArrayLiteralConvertible, ExtensibleCo
return values[element] ?? 0
}

/// Retrieve an arbitrary element & insert with empty subscript.
public subscript(v: ()) -> Element {
get { return values[values.startIndex].0 }
set { insert(newValue) }
}

/// Inserts `element` into the receiver.
public mutating func insert(element: Element) {
values[element] = (values[element] ?? 0) + 1
}

/// Removes `element` from the receiver.
public mutating func remove(element: Element) {
if let value = values[element] {
if value > 0 {
values[element] = value - 1
} else {
values.removeValueForKey(element)
}
if let value = values[element] where value > 0 {
values[element] = value - 1
} else {
values.removeValueForKey(element)
}
}

Expand Down Expand Up @@ -113,7 +105,7 @@ public struct Multiset<Element: Hashable>: ArrayLiteralConvertible, ExtensibleCo
}


// MARK: Set inclusion functions
// MARK: Inclusion functions

/// True iff the receiver is a subset of (is included in) `set`.
public func subset(set: Multiset) -> Bool {
Expand All @@ -136,7 +128,6 @@ public struct Multiset<Element: Hashable>: ArrayLiteralConvertible, ExtensibleCo
}



// MARK: Higher-order functions

/// Returns a new set including only those elements `x` where `includeElement(x)` is true.
Expand All @@ -155,7 +146,7 @@ public struct Multiset<Element: Hashable>: ArrayLiteralConvertible, ExtensibleCo
}

/// Combines each element of the receiver with an accumulator value using `combine`, starting with `initial`.
public func reduce<Into>(initial: Into, combine: (Into, Element) -> Into) -> Into {
public func reduce<Into>(initial: Into, _ combine: (Into, Element) -> Into) -> Into {
return Swift.reduce(self, initial, combine)
}

Expand Down Expand Up @@ -203,8 +194,6 @@ public struct Multiset<Element: Hashable>: ArrayLiteralConvertible, ExtensibleCo
let (element, count) = values[index.from]
if index.delta > (count - 1) {
return self[MultisetIndex(from: index.from.successor(), delta: index.delta - count, max: self.count)]
} else if index.delta < -(count - 1) {
return self[MultisetIndex(from: index.from.predecessor(), delta: index.delta + count, max: self.count)]
} else {
return element
}
Expand All @@ -224,7 +213,7 @@ public struct Multiset<Element: Hashable>: ArrayLiteralConvertible, ExtensibleCo
}
}

/// Appends `element` onto the `Set`.
/// Appends `element` onto the `Multiset`.
public mutating func append(element: Element) {
insert(element)
}
Expand Down Expand Up @@ -272,7 +261,7 @@ public struct Multiset<Element: Hashable>: ArrayLiteralConvertible, ExtensibleCo
}

/// Counts indexed by value.
private var values: [Element: Int]
private var values: Dictionary<Element, Int>
}


Expand Down Expand Up @@ -313,12 +302,8 @@ public func == <Element> (a: Multiset<Element>, b: Multiset<Element>) -> Bool {


/// The index for values of a multiset.
public struct MultisetIndex<Element: Hashable>: BidirectionalIndexType, Comparable {
// MARK: BidirectionalIndexType

public func predecessor() -> MultisetIndex {
return MultisetIndex(from: from, delta: delta - 1, max: max)
}
public struct MultisetIndex<Element: Hashable>: ForwardIndexType, Comparable {
// MARK: ForwardIndexType

public func successor() -> MultisetIndex {
return MultisetIndex(from: from, delta: delta + 1, max: max)
Expand Down
Loading

0 comments on commit 9674661

Please sign in to comment.