Quick order

Does Swift have an ordered collection type? And if not, what are my options if I want to use one?

The standard library is Setdisordered, as the documentation clearly shows:

Arrays are ordered collections of values. Sets are unordered collections of unique values. Dictionaries are unordered collections of key-value associations.

However, there are many data structures suitable for implementing ordered sets (and dictionaries), in particular balanced binary trees, such as red-black trees .

As an example of this, c ++ stl has ordered sets and maps and allows you to perform range queries on them using the lower and upper bounds.

I know that the members of a set can be sorted into an array, but I am after a data structure with O(log(n))insert, delete and query.

+8
source share
1 answer

Swift does not have its own ordered set type. If you work on iOS, you can use NSOrderedSetin Swift. If not, you have the option to write your own ordered set data structure.

: Swift OrderedSet . , .

+8

Source: https://habr.com/ru/post/1686778/


All Articles