This repository was archived by the owner on Jan 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 180
Array function to filter for unique elements #332
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| namespace Microsoft.Quantum.Arrays { | ||
| /// # Summary | ||
| /// Returns a new array that has no equal adjacent elements. | ||
| /// | ||
| /// # Description | ||
| /// Given some array of elements and a function to test equality, this | ||
| /// function returns a new array in which the relative order of elements | ||
| /// is kept, but all adjacent elements which are equal are filtered to | ||
| /// just a single element. | ||
| /// | ||
| /// # Type Parameters | ||
| /// ## 'T | ||
| /// The type of each element of `array`. | ||
| /// | ||
| /// # Input | ||
| /// ## equal | ||
| /// A function that compares two elements such that `a` is considered to | ||
| /// be equal to `b` if `equal(a, b)` is `true`. | ||
| /// ## array | ||
| /// The array to be filtered for unique elements. | ||
| /// | ||
| /// # Output | ||
| /// Array with no equal adjacent elements. | ||
| /// | ||
| /// # Remarks | ||
| /// If there are multiple elements that are equal but not next to each other, | ||
| /// there will be multiple occurrences in the output array. Use this function | ||
| /// together with `Sorted` to get an array with overall unique elements. | ||
| /// | ||
| /// # Example | ||
| /// ```Q# | ||
| /// let unique1 = Unique(EqualI, [1, 1, 3, 3, 2, 5, 5, 5, 7]); | ||
| /// // same as [1, 3, 2, 5, 7] | ||
| /// let unique2 = Unique(EqualI, [2, 2, 1, 1, 2, 2, 1, 1]); | ||
| /// // same as [2, 1, 2, 1]; | ||
| /// let unique3 = Unique(EqualI, Sorted(LessThanOrEqualI, [2, 2, 1, 1, 2, 2, 1, 1])); | ||
| /// // same as [1, 2]; | ||
| /// ``` | ||
| function Unique<'T>(equal : (('T, 'T) -> Bool), array : 'T[]) : 'T[] { | ||
| if (Length(array) == 0) { | ||
| return new 'T[0]; | ||
| } | ||
|
|
||
| mutable unique = ConstantArray(Length(array), Head(array)); | ||
| mutable count = 1; | ||
|
|
||
| for (elem in Rest(array)) { | ||
| if (not equal(elem, unique[count - 1])) { | ||
| set unique w/= count <- elem; | ||
| set count += 1; | ||
| } | ||
| } | ||
|
|
||
| return unique[0..count - 1]; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| namespace Microsoft.Quantum.Arrays { | ||
| open Microsoft.Quantum.Diagnostics; | ||
| open Microsoft.Quantum.Logical; | ||
|
|
||
| @Test("QuantumSimulator") | ||
| operation UniqueInts() : Unit { | ||
| AllEqualityFactI(Unique(EqualI, [0, 0, 1, 2, 2, 3, 4, 5, 5, 8, 42, 42, 39]), [0, 1, 2, 3, 4, 5, 8, 42, 39], "Data is not unique"); | ||
| AllEqualityFactI(Unique(EqualI, [0, 1, 1, 0, 0, 1, 1, 0]), [0, 1, 0, 1, 0], "Data is not unique"); | ||
| AllEqualityFactI(Unique(EqualI, Sorted(LessThanOrEqualI, [2, 2, 1, 1, 2, 2, 1, 1])), [1, 2], "Sorted data is not unique"); | ||
| } | ||
|
|
||
| @Test("QuantumSimulator") | ||
| operation UniqueDoubles() : Unit { | ||
| let unique = Unique(EqualD, [1.1, 1.1, 2.2, 2.2, 2.2, 3.3, 0.5, 42.0]); | ||
| EqualityFactI(Length(unique), 5, "Unexpected length of unique data"); | ||
| Fact(unique[0] == 1.1, "Unexpected element in unique data"); | ||
| Fact(unique[1] == 2.2, "Unexpected element in unique data"); | ||
| Fact(unique[2] == 3.3, "Unexpected element in unique data"); | ||
| Fact(unique[3] == 0.5, "Unexpected element in unique data"); | ||
| Fact(unique[4] == 42.0, "Unexpected element in unique data"); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.