-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdistrict_key.cpp
More file actions
89 lines (71 loc) · 1.87 KB
/
Copy pathdistrict_key.cpp
File metadata and controls
89 lines (71 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include "district_key.h"
#include "year.h"
#include "util.h"
namespace tba{
District_key::District_key(std::string const& s){
Year{atoi(s.c_str())};//check that starts with a year
//then a 2-3 letter code.
if( !(s.size()>=6 && s.size()<=7) ){
throw std::invalid_argument{[&](){
std::ostringstream ss;
ss<<"Expected District_key, found:"<<s;
return ss.str();
}()};
}
assert(s.size()<buf.size());
bzero(&buf[0],buf.size());
memcpy(&buf[0],s.c_str(),s.size());
}
District_key::District_key(const char *s){
if(!s){
throw std::invalid_argument("null");
}
*this=District_key(std::string(s));
}
std::optional<District_key> District_key::parse(std::string const& s){
//obviously not an efficient way to do this.
try{
return District_key(s);
}catch(...){
return std::nullopt;
}
}
std::string District_key::get()const{
return std::string(&buf[0]);
}
std::string District_key::location_part()const{
auto s=get();
return s.substr(4,s.size());
}
District_key rand(District_key const*){
std::stringstream ss;
ss<<rand((Year*)0);
for(auto _:range(2)){
ss<<char('a'+std::rand()%26);
}
return District_key(ss.str());
}
std::strong_ordering District_key::operator<=>(District_key const& a)const{
//This is not understood by clang++ 20.1.8 or g++ 15
//so leave it out.
//static_assert(buf.size()==8);
auto x=*(uint64_t*)&buf[0];
auto y=*(uint64_t*)&a.buf[0];
return x<=>y;
}
bool District_key::operator==(District_key const& a)const{
return (*this<=>a)==std::strong_ordering::equal;
}
std::ostream& operator<<(std::ostream& o,District_key const& a){
return o<<a.get();
}
bool operator==(District_key const& a,std::string const& b){
return a.get()==b;
}
District_key decode(JSON_value in,const District_key *){
return District_key{decode(in,(std::string*)nullptr)};
}
District_key decode(std::string const& in,District_key const*){
return District_key(in);
}
}