Skip to content

Commit 47f54e9

Browse files
authored
Merge pull request #1253 from trcrsired/next
Add a new container: bitvec
2 parents d5c1789 + 5ec4d2d commit 47f54e9

File tree

5 files changed

+1352
-0
lines changed

5 files changed

+1352
-0
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#include <fast_io.h>
2+
#include <fast_io_device.h>
3+
#include <fast_io_dsal/queue.h>
4+
#include <fast_io_dsal/vector.h>
5+
#include <fast_io_dsal/bitvec.h>
6+
#include <fast_io_driver/timer.h>
7+
8+
struct node
9+
{
10+
std::size_t to, weight;
11+
};
12+
13+
using namespace fast_io::io;
14+
15+
int main()
16+
{
17+
::fast_io::timer timer(u8"spfa_optimize_fastio_bitvec");
18+
fast_io::ibuf_file ibf("graph.txt");
19+
std::size_t m, n;
20+
scan(ibf, m, n);
21+
::fast_io::vector<::fast_io::vector<node>> graph(n);
22+
std::size_t const average{(m / n + 1) * 13 / 10};
23+
for (auto &v : graph)
24+
{
25+
v.reserve(average);
26+
}
27+
for (std::size_t i{}; i != m; ++i)
28+
{
29+
std::size_t a, b, w;
30+
scan(ibf, a, b, w);
31+
graph[a].push_back({b, w});
32+
}
33+
::fast_io::vector<std::size_t> relax(n, SIZE_MAX);
34+
::fast_io::bitvec occupied(n);
35+
::fast_io::queue<std::size_t> queue;
36+
occupied.set_front();
37+
for (queue.push(relax.front() = 0); !queue.is_empty(); queue.pop())
38+
{
39+
auto front{queue.front()};
40+
auto minimum_weight{relax[front]};
41+
for (auto e : graph[front])
42+
{
43+
if (minimum_weight + e.weight < relax[e.to])
44+
{
45+
relax[e.to] = minimum_weight + e.weight;
46+
if (!occupied.test(e.to))
47+
{
48+
occupied.set(e.to);
49+
queue.push(e.to);
50+
}
51+
}
52+
}
53+
occupied.reset(front);
54+
}
55+
fast_io::obuf_file obf("spfa.txt");
56+
if (relax.back() == SIZE_MAX)
57+
{
58+
print(obf, "no answer\n");
59+
}
60+
else
61+
{
62+
println(obf, relax.back());
63+
}
64+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include <cstdint>
2+
#include <fast_io_dsal/bitvec.h>
3+
4+
extern "C" int LLVMFuzzerTestOneInput(uint8_t const *data, size_t size)
5+
{
6+
fast_io::bitvec bv;
7+
8+
// Push all bits from input
9+
for (size_t i = 0; i != size; ++i)
10+
{
11+
bv.push_back((data[i] & 1) != 0);
12+
}
13+
14+
if (bv.size() > 8)
15+
{
16+
// Save original bits 1..7
17+
bool saved[7];
18+
for (size_t i = 1; i != 8; ++i)
19+
{
20+
saved[i - 1] = bv.test(i);
21+
}
22+
23+
// Mutate front bit
24+
bv.set_front_unchecked(true);
25+
26+
// Check if bits 1..7 changed (they SHOULD NOT)
27+
for (size_t i = 1; i != 8; ++i)
28+
{
29+
if (bv.test(i) != saved[i - 1])
30+
{
31+
__builtin_trap(); // crash → libFuzzer finds bug
32+
}
33+
}
34+
}
35+
36+
return 0;
37+
}

include/fast_io_dsal/bitvec.h

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#pragma once
2+
3+
#if !defined(__cplusplus)
4+
#error "You must be using a C++ compiler"
5+
#endif
6+
7+
#include <version>
8+
#include <type_traits>
9+
#include <concepts>
10+
#include <limits>
11+
#include <cstdint>
12+
#include <cstddef>
13+
#include <new>
14+
#include <initializer_list>
15+
#include <bit>
16+
#include <compare>
17+
#include <algorithm>
18+
#include "impl/misc/push_macros.h"
19+
#include "impl/misc/push_warnings.h"
20+
#include "../fast_io_core_impl/freestanding/impl.h"
21+
#include "../fast_io_core_impl/terminate.h"
22+
#include "../fast_io_core_impl/intrinsics/msvc/impl.h"
23+
#include "../fast_io_core_impl/allocation/impl.h"
24+
#include "../fast_io_core_impl/asan_support.h"
25+
26+
#if defined(_MSC_VER) && !defined(__clang__)
27+
#include <cstring>
28+
#endif
29+
30+
#include "impl/freestanding.h"
31+
#include "impl/common.h"
32+
#include "impl/bitvec.h"
33+
34+
#if ((__STDC_HOSTED__ == 1 && (!defined(_GLIBCXX_HOSTED) || _GLIBCXX_HOSTED == 1) && \
35+
!defined(_LIBCPP_FREESTANDING)) || \
36+
defined(FAST_IO_ENABLE_HOSTED_FEATURES))
37+
38+
namespace fast_io
39+
{
40+
41+
using bitvec = ::fast_io::containers::bitvec<::fast_io::native_global_allocator>;
42+
43+
namespace tlc
44+
{
45+
using bitvec = ::fast_io::containers::bitvec<::fast_io::native_thread_local_allocator>;
46+
} // namespace tlc
47+
48+
} // namespace fast_io
49+
50+
#endif
51+
52+
#include "impl/misc/pop_macros.h"
53+
#include "impl/misc/pop_warnings.h"

0 commit comments

Comments
 (0)