Mercurial > repos > public > sbplib_julia
comparison src/StaticDicts/StaticDicts.jl @ 741:94941a062124 feature/static_dict
Add tests for constructing empty dicts. Change constructors accordingly
author | Jonatan Werpers <jonatan@werpers.com> |
---|---|
date | Wed, 17 Mar 2021 22:08:59 +0100 |
parents | e716602f1d62 |
children | ffb71bdb4486 |
comparison
equal
deleted
inserted
replaced
740:e7e06bf394fb | 741:94941a062124 |
---|---|
21 to the constructor. | 21 to the constructor. |
22 """ | 22 """ |
23 struct StaticDict{K,V,N} <: AbstractDict{K,V} | 23 struct StaticDict{K,V,N} <: AbstractDict{K,V} |
24 pairs::NTuple{N,Pair{K,V}} | 24 pairs::NTuple{N,Pair{K,V}} |
25 | 25 |
26 function StaticDict{K,V,N}(pairs::Tuple) where {K,V,N} | 26 function StaticDict{K,V}(pairs::Vararg{Pair,N}) where {K,V,N} |
27 if !allunique(first.(pairs)) | 27 if !allunique(first.(pairs)) |
28 throw(DomainError(pairs, "keys must be unique")) | 28 throw(DomainError(pairs, "keys must be unique")) |
29 end | 29 end |
30 return new{K,V,N}(pairs) | 30 return new{K,V,N}(pairs) |
31 end | 31 end |
32 end | 32 end |
33 | 33 |
34 function StaticDict(pairs::Vararg{Pair}) | 34 function StaticDict(pairs::Vararg{Pair}) |
35 K = typejoin(firsttype.(pairs)...) | 35 K = typejoin(firsttype.(pairs)...) |
36 V = typejoin(secondtype.(pairs)...) | 36 V = typejoin(secondtype.(pairs)...) |
37 N = length(pairs) | 37 return StaticDict{K,V}(pairs...) |
38 return StaticDict{K,V,N}(pairs) | |
39 end | 38 end |
40 | 39 |
41 StaticDict(pairs::NTuple{N,Pair} where N) = StaticDict(pairs...) | 40 StaticDict(pairs::NTuple{N,Pair} where N) = StaticDict(pairs...) |
42 | 41 |
43 function Base.get(d::StaticDict, key, default) | 42 function Base.get(d::StaticDict, key, default) |