|
1 | 1 | # pysorteddict |
2 | 2 |
|
3 | 3 | Enriches Python with `SortedDict`, a sorted dictionary: a dictionary in which the keys are always in ascending order. |
| 4 | +They are not sorted when queried; they are genuinely stored such that iterating over them yields a monotonically |
| 5 | +increasing sequence. Needless to say, a given `SortedDict` instance only admits keys of a single type with a strict |
| 6 | +ordering defined. |
4 | 7 |
|
5 | 8 | pysorteddict is implemented entirely in C++. `SortedDict` provides a Python interface to `std::map`. |
6 | 9 |
|
@@ -29,3 +32,45 @@ desired, though. |
29 | 32 | [[<br>Changelog](changelog)]{.card} |
30 | 33 | [[<br>Development](development)]{.card} |
31 | 34 | ]{.card-container} |
| 35 | + |
| 36 | +## Why pysorteddict? |
| 37 | + |
| 38 | +There are many sorted dictionary implementations for Python. Foremost among them is Sorted Containers, a mature library |
| 39 | +which has seen use in real-world applications. (It also provides sorted list and set implementations, but those aren't |
| 40 | +in the scope of pysorteddict.) So why use the sorted dictionary from pysorteddict instead of Sorted Containers? |
| 41 | + |
| 42 | +pysorteddict has some rather attractive features which Sorted Containers does not have. |
| 43 | + |
| 44 | +### Strongly Typed |
| 45 | + |
| 46 | +Keys are not automatically converted between compatible types. For instance, although `0 == 0.0` in Python, a sorted |
| 47 | +dictionary with integer keys will reject `0.0`. |
| 48 | + |
| 49 | +The sorted dictionary from Sorted Containers allows mixing integer and float keys. |
| 50 | + |
| 51 | +### Strict |
| 52 | + |
| 53 | +Conceptually different keys are considered different. For instance, although `0 == False` in Python, a sorted |
| 54 | +dictionary with integer keys will not fetch the value mapped to `0` when queried with `False`. |
| 55 | + |
| 56 | +The sorted dictionary from Sorted Containers inherits Python's default behaviour, treating `0` as `False` and `1` as |
| 57 | +`True`. |
| 58 | + |
| 59 | +### Correct |
| 60 | + |
| 61 | +NaN is unconditionally rejected as a key. |
| 62 | + |
| 63 | +The sorted dictionary from Sorted Containers accepts NaN, resulting in an order of keys which cannot truly be |
| 64 | +considered sorted. Subsequent reorderings will place NaN in arbitrary positions. |
| 65 | + |
| 66 | +### Stable Under Mutation During Iteration |
| 67 | + |
| 68 | +Modifications to a sorted dictionary are allowed while iterating over it, and the results are consistent and |
| 69 | +well-defined. |
| 70 | + |
| 71 | +The sorted dictionary from Sorted Containers also allows modifications while iterating over it, but the results are |
| 72 | +clearly wrong: for instance, an iterator over it will keep yielding keys even after the dictionary is cleared. |
| 73 | + |
| 74 | +### Robust |
| 75 | + |
| 76 | +Computations are relegated to the robust C++ sorted dictionary. |
0 commit comments