-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
1,195 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
add_executable(instant main.cc instant.h) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// SPDX-License-Identifier: GPL-2.0-or-later | ||
/* | ||
* Author: Abby Cin | ||
* Mail: [email protected] | ||
* Create Time: 2023-10-13 10:20:58 | ||
*/ | ||
|
||
#ifndef INSTANT_20231013102058 | ||
#define INSTANT_20231013102058 | ||
|
||
#include <chrono> | ||
|
||
namespace nm | ||
{ | ||
class Instant { | ||
public: | ||
static Instant now() | ||
{ | ||
return { std::chrono::steady_clock::now() }; | ||
} | ||
|
||
auto elapse_usec() | ||
{ | ||
return diff().count() / 1e3; | ||
} | ||
|
||
auto elapse_ms() | ||
{ | ||
return diff().count() / 1e6; | ||
} | ||
|
||
auto elapse_sec() | ||
{ | ||
return diff().count() / 1e9; | ||
} | ||
|
||
auto elapse_min() | ||
{ | ||
return elapse_sec() / 60.0; | ||
} | ||
|
||
private: | ||
using timepoint = std::chrono::steady_clock::time_point; | ||
Instant(timepoint now) : tp_ { now } | ||
{ | ||
} | ||
|
||
auto diff() -> std::chrono::duration<double, std::nano> | ||
{ | ||
return std::chrono::steady_clock::now() - tp_; | ||
} | ||
|
||
timepoint tp_; | ||
}; | ||
} | ||
|
||
#endif // INSTANT_20231013102058 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// SPDX-License-Identifier: GPL-2.0-or-later | ||
/* | ||
* Author: Abby Cin | ||
* Mail: [email protected] | ||
* Create Time: 2023-10-13 10:21:10 | ||
*/ | ||
|
||
#include "instant.h" | ||
#include <thread> | ||
|
||
void die_on_false(bool ok) | ||
{ | ||
if (!ok) | ||
abort(); | ||
} | ||
|
||
int main() | ||
{ | ||
using namespace std::chrono_literals; | ||
{ | ||
auto e = nm::Instant::now(); | ||
std::this_thread::sleep_for(10us); | ||
die_on_false(e.elapse_usec() > 10); | ||
} | ||
{ | ||
auto e = nm::Instant::now(); | ||
std::this_thread::sleep_for(10ms); | ||
die_on_false(e.elapse_ms() >= 10); | ||
} | ||
{ | ||
auto e = nm::Instant::now(); | ||
std::this_thread::sleep_for(2s); | ||
die_on_false(e.elapse_sec() >= 2); | ||
} | ||
{ | ||
auto e = nm::Instant::now(); | ||
std::this_thread::sleep_for(61s); | ||
die_on_false(e.elapse_min() > 1); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
add_executable(swiss_set_test swiss_set_test.cc swisstable.h) | ||
target_include_directories(swiss_set_test PRIVATE ${PROJECT_SOURCE_DIR}) | ||
|
||
add_executable(swiss_map_test swiss_map_test.cc) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
pointer stability is not guaranteed | ||
|
||
a good hash function is required (see [absl::Hash](https://abseil.io/docs/cpp/guides/hash)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// SPDX-License-Identifier: GPL-2.0-or-later | ||
/* | ||
* Author: Abby Cin | ||
* Mail: [email protected] | ||
* Create Time: 2023-10-16 13:23:04 | ||
*/ | ||
|
||
#ifndef SWISS_MAP_H_20231016132304 | ||
#define SWISS_MAP_H_20231016132304 | ||
|
||
#include "swisstable.h" | ||
#include <initializer_list> | ||
|
||
namespace nm | ||
{ | ||
template<typename K, typename V> | ||
class MapPolicy { | ||
public: | ||
static_assert(!std::is_reference_v<K>, "forbid reference as key"); | ||
using key_type = std::remove_cvref_t<K>; | ||
using value_type = std::pair<key_type, V>; | ||
|
||
static const key_type &key(const value_type &v) | ||
{ | ||
return v.first; | ||
} | ||
}; | ||
|
||
template<typename Key, | ||
typename Val, | ||
typename Hash = SwissHash, | ||
typename Eq = SwissEq> | ||
class SwissMap : public detail::Swiss<MapPolicy<Key, Val>, Hash, Eq> { | ||
using Base = detail::Swiss<MapPolicy<Key, Val>, Hash, Eq>; | ||
|
||
public: | ||
using Base::Base; | ||
using Base::begin; | ||
using Base::end; | ||
using Base::insert; | ||
using Base::erase; | ||
using Base::reserve; | ||
using Base::find; | ||
using iterator = Base::iterator; | ||
|
||
SwissMap(std::initializer_list<typename Base::value_type> il) : Base {} | ||
{ | ||
for (auto &x : il) | ||
insert(x); | ||
} | ||
|
||
template<typename K, typename V> | ||
iterator emplace(K &&k, V &&v) | ||
{ | ||
return insert({ std::forward<K>(k), std::forward<V>(v) }); | ||
} | ||
|
||
Val &operator[](const Key &k) | ||
{ | ||
auto it = find(k); | ||
if (it == end()) | ||
it = emplace(k, Val {}); | ||
return it->second; | ||
} | ||
}; | ||
} | ||
|
||
#endif // SWISS_MAP_H_20231016132304 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// SPDX-License-Identifier: GPL-2.0-or-later | ||
/* | ||
* Author: Abby Cin | ||
* Mail: [email protected] | ||
* Create Time: 2023-10-16 19:51:17 | ||
*/ | ||
|
||
#include "swiss_map.h" | ||
|
||
int main() | ||
{ | ||
{ | ||
nm::SwissMap<int, int> m { { 5, 6 } }; | ||
|
||
m.emplace(1, 2); | ||
m.emplace(2, 3); | ||
m.emplace(3, 4); | ||
|
||
for (auto [k, v] : m) | ||
printf("%d => %d\n", k, v); | ||
auto it = m.find(1); | ||
printf("=> %d %d\n", it->first, it->second); | ||
|
||
m.erase(1); | ||
printf("contains 1? %s\n", m.contains(1) ? "yes" : "no"); | ||
for (auto [k, v] : m) | ||
printf("%d => %d\n", k, v); | ||
|
||
printf("before clear load_factor %f\n", m.load_factor()); | ||
m.clear(); | ||
for (auto [k, v] : m) | ||
printf("%d => %d\n", k, v); | ||
printf("after clear load_factor %f\n", m.load_factor()); | ||
} | ||
|
||
{ | ||
using namespace std::string_literals; | ||
nm::SwissMap<std::string, int> m { { "e"s, 4 } }; | ||
|
||
m.emplace("a"s, 1); | ||
m.emplace("b"s, 2); | ||
m.emplace("c"s, 3); | ||
|
||
m.insert({ "d"s, 4 }); | ||
|
||
auto it = m.find("c"s); | ||
auto moha = "moha"s; | ||
|
||
it->second = 666; | ||
|
||
m[moha] = 1926; | ||
printf("moha => %d\n", m[moha]); | ||
|
||
m[moha] = 233; | ||
|
||
for (auto &[k, v] : m) | ||
printf("%s => %d\n", k.c_str(), v); | ||
} | ||
} |
Oops, something went wrong.