|
2 | 2 | arraymap |
3 | 3 | ============ |
4 | 4 |
|
5 | | -`arraymap` provides Dictionary-like lookup from NumPy array values to integer position. |
| 5 | +The ArrayMap library provides dictionary-like lookup from NumPy array values to their integer positions. The hash table design and C implementation is based on [AutoMap](https://github.com/brandtbucher/automap), with extensive additions for direct support of NumPy arrays. |
6 | 6 |
|
7 | 7 |
|
8 | | -Examples |
9 | | --------- |
10 | | - |
11 | | -`arraymap` objects are sort of like "inverse sequences". They come in two |
12 | | -variants: |
13 | | - |
14 | | -### FrozenAutoMap |
15 | | - |
16 | | -```py |
17 | | ->>> from arraymap import FrozenAutoMap |
18 | | -``` |
19 | | - |
20 | | -`FrozenAutoMap` objects are immutable. They can be constructed from any iterable |
21 | | -of hashable, unique keys. |
22 | | - |
23 | | - |
24 | | -```py |
25 | | ->>> a = FrozenAutoMap("AAA") |
26 | | -Traceback (most recent call last): |
27 | | - File "<stdin>", line 1, in <module> |
28 | | -ValueError: 'A' |
29 | | ->>> a = FrozenAutoMap("ABC") |
30 | | ->>> a |
31 | | -arraymap.FrozenAutoMap(['A', 'B', 'C']) |
32 | | -``` |
33 | | - |
34 | | -The values are integers, incrementing according to the order of the original |
35 | | -keys: |
36 | | - |
37 | | -```py |
38 | | ->>> a["A"] |
39 | | -0 |
40 | | ->>> a["C"] |
41 | | -2 |
42 | | ->>> a["X"] |
43 | | -Traceback (most recent call last): |
44 | | - File "<stdin>", line 1, in <module> |
45 | | -KeyError: 'X' |
46 | | -``` |
47 | | - |
48 | | -The full `Mapping` interface is provided: |
49 | | - |
50 | | -```py |
51 | | ->>> [*a.keys()] |
52 | | -['A', 'B', 'C'] |
53 | | ->>> [*a.values()] |
54 | | -[0, 1, 2] |
55 | | ->>> [*a.items()] |
56 | | -[('A', 0), ('B', 1), ('C', 2)] |
57 | | ->>> a.get("X", 42) |
58 | | -42 |
59 | | ->>> "B" in a |
60 | | -True |
61 | | ->>> [*a] |
62 | | -['A', 'B', 'C'] |
63 | | -``` |
64 | | - |
65 | | -They may also be combined with each other using the `|` operator: |
66 | | - |
67 | | -```py |
68 | | ->>> b = FrozenAutoMap(range(5)) |
69 | | ->>> c = FrozenAutoMap(range(5, 10)) |
70 | | ->>> b | c |
71 | | -arraymap.FrozenAutoMap([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) |
72 | | ->>> b |= c # Note that b is reassigned, not mutated! |
73 | | ->>> b |
74 | | -arraymap.FrozenAutoMap([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) |
75 | | -``` |
76 | | - |
77 | | -### AutoMap |
78 | | - |
79 | | -```py |
80 | | ->>> from arraymap import AutoMap |
81 | | -``` |
82 | | - |
83 | | -Unlike `FrozenAutoMap` objects, `AutoMap` objects can grow; new keys may be |
84 | | -added, but existing ones may not be deleted or changed. |
85 | | - |
86 | | -```py |
87 | | ->>> d = AutoMap("ABC") |
88 | | ->>> d |
89 | | -arraymap.AutoMap(['A', 'B', 'C']) |
90 | | ->>> d |= "DEF" # Here, d *is* mutated! |
91 | | ->>> d |
92 | | -arraymap.AutoMap(['A', 'B', 'C', 'D', 'E', 'F']) |
93 | | -``` |
94 | | - |
95 | | -They also have `add` and `update` methods for adding new keys: |
96 | | - |
97 | | -```py |
98 | | ->>> e = AutoMap(["I", "II", "III"]) |
99 | | ->>> e.add("IV") |
100 | | ->>> e |
101 | | -arraymap.AutoMap(['I', 'II', 'III', 'IV']) |
102 | | ->>> e.update(["V", "VI", "VII"]) |
103 | | ->>> e |
104 | | -arraymap.AutoMap(['I', 'II', 'III', 'IV', 'V', 'VI', 'VII']) |
105 | | -``` |
106 | 8 |
|
0 commit comments