liveBook · Manning

Comparable interface

As explained in Classic Computer Science Problems in Swift: Essential techniques for practicing programmers

Comparable is a Swift standard-library protocol for types whose values can be ordered relative to one another.

Comparable is a Swift standard-library protocol for values that can be ordered relative to one another. A generic Node adopts it so search algorithms can rank nodes. section 2.2

Ordering search nodes

Node<T> conforms to Comparable and implements < by comparing cost + heuristic. A node with the lower combined value sorts before the other node. The same type also conforms to Hashable, while == compares node identity.

This ordering prepares Node for reuse in A*, where a priority queue can select the most promising node according to its cost and heuristic. section 2.2

How Classic Computer Science Problems in Swift: Essential techniques for practicing programmers covers it

How deep it goes
Defines comparison for a concrete generic Node type but does not survey Comparable requirements beyond the shown operators.
Examples
Uses a generic Node whose < operator compares cost plus heuristic and whose == operator compares reference identity.
What it assumes
Assumes familiarity with generic types, Node values, cost, heuristic, and Hashable conformance.
What you can do after
Apply it via Swift operator overloads to rank custom search nodes for priority-based algorithms.
How it connects
Connects Node, Hashable, A*, priority queues, cost, and heuristic values.

Also explained in

Compare how 3 books explain Comparable interface →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Classic Computer Science Problems in Swift: Essential techniques for practicing programmers. The section links point to the parts of the book it draws on.