Commit Graph
42 Commits
Author SHA1 Message Date
Karoy Lorentey 8ce8a6bc86 [PersistentSet] Review, test, fix, and document set relation predicates 2022-10-19 15:20:17 -07:00
Karoy Lorentey f843bd40bf [PersistentSet] Add update(_:at:) 2022-10-19 15:20:17 -07:00
Karoy Lorentey 2e9447fb1c [_CollectionsTestSupport] Richer support for combinatorial testing 2022-10-13 15:45:08 -07:00
Karoy Lorentey 6286cd0b99 Restore support for building with Swift 5.5
This is mostly about adding more type declarations to deal with type checker limitations in the older compiler.
This also includes workarounds that allow better support for CodingKeyRepresentable in PersistentDictionary, and it fixes a couple of small oversights on various #else branches.
2022-10-10 18:07:49 -07:00
Karoy Lorentey 0b86bb11d2 [PersistentSet] Flesh out API surface
Add customary overloads for high-level Set operations.
2022-10-09 15:26:32 -07:00
Karoy Lorentey 4604c1bc38 [test] Add SetAPIChecker protocol
This is useful for validating the API surface of set-like types,
ensuring that we haven’t forgotten to implement members that
are part of users’ baseline expectations.

To ensure maximum utility, this protocol refines neither Collection
nor SetAlgebra.
2022-10-09 13:33:23 -07:00
Karoy Lorentey f71ebd9494 [PersistentDictionary] Implement structural merge operations
Merging is best done in-place, as merging a non-empty dictionary will
always mutate the dictionary, so we don’t have to tiptoe around
uniqueness issues.

(The union operation on PersistentSet is vaguely similar, but not
quite — unlike with merge, union is able directly link shared subtrees
into the result. However, a similar optimization may be worthwhile for
symmetricDifference.)
2022-09-30 19:55:04 -07:00
Karoy Lorentey 81acdb7f8d [PersistentSet] Implement structural symmetricDifference
I think this may be the last missing non-trivial algorithm for this module.
2022-09-29 17:33:10 -07:00
Karoy Lorentey 876177dbb5 [PersistentSet] Test & fix symmetricDifference 2022-09-28 18:38:14 -07:00
Karoy Lorentey 562b21e70f [PersistentCollections] filter, intersection, subtracting: Don’t build a result node until we know for sure we won’t just clone self 2022-09-28 14:45:44 -07:00
Karoy Lorentey 4a4c4a75fd [PersistentSet] Add tests for union; fix issues 2022-09-27 23:02:40 -07:00
Karoy Lorentey 03a4791766 [PersistentSet] Add tests for set predicates; fix issues 2022-09-27 14:18:40 -07:00
Karoy Lorentey a887a77ef5 [PersistentSet] Add tests for intersection & subtracting; fix issues 2022-09-27 14:18:40 -07:00
Karoy Lorentey 6437aea75a [PersistentCollections] Improve test coverage & fixtures 2022-09-27 14:18:40 -07:00
Karoy Lorentey 826e22c91e [PersistentDictionary] Implement structural filter & compactMapValues
Plus add tests covering these as well and mapValues.
2022-09-24 17:09:36 -07:00
Karoy Lorentey 958fd47539 [PersistentSet] Start drafting the new type 2022-09-21 10:45:20 -07:00
Karoy Lorentey c355812e53 [PersistentDictionary] Small cleanups 2022-09-21 10:45:20 -07:00
Karoy Lorentey b7bfc52664 [test] Add Dictionary API checker protocol
This can be used to verify that a new dictionary implementation
provides all expected members.
2022-09-19 21:25:07 -07:00
Karoy Lorentey a2491aac45 [PersistentDictionary] Improved copy & resize operations
Don’t copy the current node and then insert a new item into it.
Instead, create the copy with the new item already inserted.

Ditto for new collisions.
2022-09-19 21:25:07 -07:00
Karoy Lorentey d9cff57f0c [test][PersistentDictionary] Clean up fixture initialization 2022-09-19 21:25:07 -07:00
Karoy Lorentey 9da672fdf8 [PersistentDictionary] Fix index(forKey:) test 2022-09-19 21:25:07 -07:00
Karoy Lorentey 57deed5f0f [PersistentDictionary] Implement in-place mutations
# Conflicts:
#	Tests/PersistentCollections2Tests/PersistentMap Fixtures.swift
2022-09-17 17:20:34 -07:00
Karoy Lorentey bf3617e379 [PersistentCollections] Work on testing a bit; add fixtures
The new fixtures provide an easier way to construct sample
hash trees with specific structure, to run tests on them.

Fixtures are able to determine the expected ordering for each
sample tree, which comes super useful for verifying collection
conformances.
2022-09-15 20:56:50 -07:00
Karoy Lorentey 3e38184f2a [PersistentCollections] Optimize _Node.==
If the key implements hashing properly, then this would be a clever
way to try another hash seed to differentiate between colliding keys.

However, if Key has proper hashing, then collision nodes are expected
to be tiny, so it’s better to simply do naive comparisons. If the Key
type has broken (i.e., partial) hashing, then loading colliding items
into a dictionary would just make == even slower.

So we’re better off comparing collidingh items via nested loops — so
let’s just unconditionally do that.
2022-09-15 20:56:50 -07:00
Karoy Lorentey c9ca59fbb9 [PersistentCollections] Improved storage representation
- Switch to using `ManagedBuffer` to represent node storage. This has
  useful performance benefits, but it significantly complicates access
  to node data. We no longer have direct access to the underlying raw
  storage: everything needs to go through closures passed to
  `ManagedBuffer.withUnsafeMutablePointers`.

- Introduce `struct _Node` and `struct _Node.UnsafeHandle` to
  represent a (strong) reference to a hash tree node, and a
  convenient handle for node storage, respectively. Use these instead
  of directly accessing the ManagedBuffer object, whenever feasible.

- Implement separate paths for in-place mutations vs copy-on-write
  operation, optimizing both flavors.

- Go to great lengths to eliminate unnecessary retain-release traffic,
  whenever possible.

- Push subtree counts into parent nodes by moving the corresponding
  field from the node header into the `_Node` struct. (This results in
  a cool ~20x speedup for jumping around in the tree by integer
  offsets.)

- Implement a type-punned empty root singleton, to allow creating
  empty trees without allocating anything.
2022-09-13 23:48:54 -07:00
Karoy Lorentey 3441a48045 [PersistentDictionary] rootNode → root 2022-09-12 12:47:04 -07:00
Karoy Lorentey 3bc9b42739 [PersistentCollections] Standard invariant checks 2022-09-12 12:47:04 -07:00
Karoy Lorentey ca19ecb5b3 [PersistentDictionary] Don’t copy collision node contents into arrays 2022-09-12 12:47:04 -07:00
Karoy Lorentey afdb471af7 [PersistentCollections] “trie”, “subNode” → “child”, “data” → “item”
Also, replace the use of the word “trie” with “prefix trie” (or “hash tree”) throughout the documentation & comments.
2022-09-12 12:47:04 -07:00
Karoy Lorentey 14f90620c8 [PersistentCollections] Introduce _Bucket, _Bitmap, _HashPath, and _HashValue
These provide nicer tools for slicing up hash values and dealing with node bitmaps.

_Bucket is a logical entry in the hash table inside a node.
_Bitmap is a set of bucket values, represented by a bitset.
_HashPath facilitates slicing up a hash value into a series of bucket values.
_HashValue represents a hash value.

This is still just mostly superficial refactoring, with little to no functional changes.
2022-09-09 00:47:10 -07:00
Karoy LorenteyandMichael Steindorfer aa428d0489 Update spelling of publication dates
Co-authored-by: Michael Steindorfer <michael@steindorfer.name>
2022-09-08 16:04:18 -07:00
Karoy Lorentey 750559ea9e [PersistentDictionary] PersistentDictionaryIndex → PersistentDictionary.Index 2022-09-08 00:55:48 -07:00
Karoy Lorentey 6f93a628e8 [PersistentDictionary] BitmapIndexedDictionaryNode → PersistentDictionary._ Node, Header → _NodeHeader
Also move Capacity, Bitmap under _NodeHeader.

Start thematically organizing _Node members.
2022-09-07 21:06:19 -07:00
Karoy Lorentey 2597ff23cd [PersistentDictionary] Apply leading underscore rule to common functions 2022-09-07 21:06:19 -07:00
Karoy Lorentey 7e78002909 [PersistentCollections] Move BitmapSmokeTests into its own file 2022-09-07 19:36:15 -07:00
Karoy Lorentey 1a4993dfda [PersistentCollections] Reformat
Follow the Standard Library coding style:

- Hard 80 character line limit
- Use two spaces as the indentation unit
- Overlong lines broken up by adding line breaks at the following places
    1. After opening delimiters & before closing delimiters
    2. Between each item in a list
    3. Extracting subexpressions etc as needed
2022-09-07 19:16:41 -07:00
Michael Steindorfer fbb8aeda08 [Capsule] Remove non-applicable tests 2022-09-06 21:01:42 +02:00
Michael Steindorfer f930801d37 [Capsule] Expand index(forKey:) tests 2022-09-06 20:40:28 +02:00
Michael Steindorfer 031b762053 [Capsule] Fix test case argument label
Necessary due to removal of unchecked initializer variants.
2022-09-06 18:57:10 +02:00
Michael Steindorfer 49283d3251 [Capsule] Remove unchecked variant of initializer 2022-09-06 16:37:23 +02:00
Michael Steindorfer 26b97f95d2 [Capsule] Unify insertion/deletion methods
Note, always tracking discardable result negatively impacts batch
use cases.
2022-09-06 16:11:17 +02:00
Michael Steindorfer 5bc17ffe84 [Capsule] Rename package to PersistentCollections 2022-09-06 13:22:10 +02:00