Mercurial > repos > public > sbplib_julia
comparison src/StaticDicts/StaticDicts.jl @ 720:172c55c4cf2e feature/static_dict
Dissalow duplicate keys
author | Jonatan Werpers <jonatan@werpers.com> |
---|---|
date | Tue, 16 Mar 2021 22:35:40 +0100 |
parents | 2f8c67c5979e |
children | a789e789e30f |
comparison
equal
deleted
inserted
replaced
719:2f8c67c5979e | 720:172c55c4cf2e |
---|---|
14 A simple static dictonary. Performs lookup using linear search with ==-comparison | 14 A simple static dictonary. Performs lookup using linear search with ==-comparison |
15 of keys. No hashing is used. | 15 of keys. No hashing is used. |
16 """ | 16 """ |
17 struct StaticDict{K,V,N} <: AbstractDict{K,V} | 17 struct StaticDict{K,V,N} <: AbstractDict{K,V} |
18 pairs::NTuple{N,Pair{K,V}} | 18 pairs::NTuple{N,Pair{K,V}} |
19 | |
20 # TBD: Why doesn't `pairs::NTuple{N,Pair{K,V}}` work? | |
21 function StaticDict{K,V,N}(pairs::Tuple) where {K,V,N} | |
22 if !allunique(first.(pairs)) | |
23 throw(ArgumentError("keys must be unique (for now)")) | |
24 end | |
25 | |
26 return new{K,V,N}(pairs) | |
27 end | |
19 end | 28 end |
20 | 29 |
21 function StaticDict(pairs::Vararg{Pair}) | 30 function StaticDict(pairs::Vararg{Pair}) |
22 K = typejoin(firsttype.(pairs)...) | 31 K = typejoin(firsttype.(pairs)...) |
23 V = typejoin(secondtype.(pairs)...) | 32 V = typejoin(secondtype.(pairs)...) |