Case insensitive string set - #658
Conversation
Add a type that can be a skeleton for a sorted-set. Useful for un-`Hashable` types or custom comparisons. Add protocol to define custom comparison semantics. Add functions (with helper types) that perform set operations and check for certain combinations of elements. Since the set must always be stored sorted, add conformance to Sequence and Comparable to take advantage.
Relax a method's visibility to be non-private, so it can be called from extension files. Improve the implementation of the self-mutating versions of set operations. Add a method to remove the first element of a set without comparing its value first.
Add _ensureUnique() calls to the element-level mutating set operations.
Introduce CaseInsensitiveStringSet as an internally-sorted list of strings, but any comparisons between strings are case-insensitive. This type conforms to Comparable, CustomDebugStringConvertible, CustomReflectable, CustomStringConvertible, Decodable, Encodable, Hashable, Sequence, and SetAlgebra. There are extra members that are a subset of RangeReplaceableCollection.
|
The inspiration came from the extension Then I realized that I had a different problem. The Standard library's |
Add a new deterministic skip-list data structure. The implementation is generic over a custom protocol providing both the list's element type and ordering predicates. The skip list has operations for insertion, deletion, element search, element re-representation, iteration, and bulk deletion. The skip list uses an inner reference type with copy-on-write semantics.
Replace the storage type of CaseInsensitiveStringSet. Update the implementation of the affected methods and properties.
|
Quick notes:
|
Introduces
CaseInsensitiveStringSet, a sorted set ofStringthat uses case-insensitive comparisons for ordering and equivalence.The API is that
CaseInsensitiveStringSetconforms toComparable,CustomDebugStringConvertible,CustomReflectable,CustomStringConvertible,Decodable,Encodable,Hashable,Sequence, andSetAlgebra. A subset ofRangeReplaceableCollectionprovides common functionality that the previous protocols don't cover.The functions in "SortedSetMerge.swift" evaluate operations combining two sorted sets.
rawSortedMerge(of: and:)vends the merger of the sets, giving an also-sorted result. Those results are expressed as one or two values with their category. Categories are: values only from the first sequence, values only from the second sequence, and values that the two sequences share. A shared vended element gives out the element value from both sources. This function doesn't necessarily need to be used directly, but it implements the actual user-facing functions.sortedMerge(between: and: retaining:)vends a sorted sequence that applies the given set operation to combine the two given source sequences.doesSortedMerger(of: and: haveExclusivesToFirst: hasExclusivesToSecond: haveSharedElements:)checks the count of each result category, returning whether it matches the given pattern. A category can be checked for not being there, being there, or ignored.The
SortedSettype, in "SortedSetToo.swift", uses the above functions to implementSetAlgebrawith an internal skip-list structure.SortedSetis not templated onElementdirectly, but through the protocolOrderable(in "Orderable.swift").Orderabletypes specify theirElementtype and three functions that define ordering. You only have to implement the function that's an analogue to the less-than operation. The equal-to and greater-than operator analogues have default implementations that use the less-than function.CaseInsensitiveStringSetwraps aSortedSetimplementing anOrderableinstantiation that uses case-insensitive comparisons from the Foundation system library. (Thenillocale is used in `String.compare_: options: range: locale:)When being printed, the regular version will print out the canonized casing for each element. (The hash implementation uses this form.) The debug version will print out the string that is actually stored.
Checklist