2016-12-15 05:11:51 +08:00
|
|
|
/*
|
|
|
|
ISC License
|
|
|
|
|
2018-10-17 04:20:32 +08:00
|
|
|
Copyright (c) 2018, Antonio SJ Musumeci <trapexit@spawn.link>
|
2016-12-15 05:11:51 +08:00
|
|
|
|
|
|
|
Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
purpose with or without fee is hereby granted, provided that the above
|
|
|
|
copyright notice and this permission notice appear in all copies.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2017-06-30 23:15:20 +08:00
|
|
|
#pragma once
|
2016-12-15 05:11:51 +08:00
|
|
|
|
|
|
|
#include "khash.h"
|
2018-10-17 04:20:32 +08:00
|
|
|
#include "fasthash.h"
|
2016-12-15 05:11:51 +08:00
|
|
|
|
2018-10-17 04:20:32 +08:00
|
|
|
KHASH_SET_INIT_INT64(hashset);
|
2018-10-14 11:48:30 +08:00
|
|
|
|
2018-10-17 04:20:32 +08:00
|
|
|
class HashSet
|
2016-12-15 05:11:51 +08:00
|
|
|
{
|
|
|
|
public:
|
2018-10-17 04:20:32 +08:00
|
|
|
HashSet()
|
2016-12-15 05:11:51 +08:00
|
|
|
{
|
2018-10-17 04:20:32 +08:00
|
|
|
_set = kh_init(hashset);
|
2016-12-15 05:11:51 +08:00
|
|
|
}
|
|
|
|
|
2018-10-17 04:20:32 +08:00
|
|
|
~HashSet()
|
2016-12-15 05:11:51 +08:00
|
|
|
{
|
2018-10-17 04:20:32 +08:00
|
|
|
kh_destroy(hashset,_set);
|
2016-12-15 05:11:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
inline
|
|
|
|
int
|
2018-10-17 04:20:32 +08:00
|
|
|
put(const char *str_)
|
2016-12-15 05:11:51 +08:00
|
|
|
{
|
|
|
|
int rv;
|
2018-10-17 04:20:32 +08:00
|
|
|
uint64_t h;
|
2016-12-15 05:11:51 +08:00
|
|
|
khint_t key;
|
|
|
|
|
2018-10-17 04:20:32 +08:00
|
|
|
h = fasthash64(str_,strlen(str_),0x7472617065786974);
|
|
|
|
|
|
|
|
key = kh_put(hashset,_set,h,&rv);
|
2016-12-15 05:11:51 +08:00
|
|
|
if(rv == 0)
|
|
|
|
return 0;
|
|
|
|
|
2018-10-17 04:20:32 +08:00
|
|
|
kh_key(_set,key) = h;
|
2016-12-15 05:11:51 +08:00
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2018-10-17 04:20:32 +08:00
|
|
|
inline
|
|
|
|
int
|
|
|
|
size(void)
|
|
|
|
{
|
|
|
|
return kh_size(_set);
|
|
|
|
}
|
|
|
|
|
2016-12-15 05:11:51 +08:00
|
|
|
private:
|
2018-10-17 04:20:32 +08:00
|
|
|
khash_t(hashset) *_set;
|
2016-12-15 05:11:51 +08:00
|
|
|
};
|