|
1 | 1 | @testitem "iterators.jl" begin |
2 | 2 | @testset "SubsetGrayCode iterates over all weight k subsets of {1,..,n} (single iterator)" begin |
3 | | - len = 15 |
4 | | - weight = 7 |
5 | | - sgc = CodingTheory.SubsetGrayCode(len, weight) |
| 3 | + len = 7 |
| 4 | + weight = 5 |
| 5 | + sgc = CodingTheory.SubsetGrayCode(len, weight, UInt(21), UInt(0)) |
6 | 6 | all_subsets_gray = fill(fill(0, weight), length(sgc)) |
7 | 7 | subset = collect(1:sgc.k) |
8 | 8 | state = (subset, 1, fill(-1, 3)) |
|
16 | 16 | sort!(all_subsets_gray) |
17 | 17 | all_subsets_hecke = CodingTheory.Oscar.subsets(collect(1:len), weight) |
18 | 18 | sort!(all_subsets_hecke) |
19 | | - @test length(all_subsets_gray) == 6435 |
20 | | - @test all_subsets_gray == all_subsets_hecke |
21 | | - end |
22 | | - |
23 | | - @testset "SubsetGrayCode iterates over all weight k subsets of {1,..,n} (split iterator)" begin |
24 | | - len = 15 |
25 | | - weight = 7 |
26 | | - num_threads = 3 # split the iterator into 3 parts |
27 | | - itrs = CodingTheory._subset_gray_codes_from_num_threads(len, weight, num_threads) |
28 | | - @test length(itrs) == num_threads |
29 | | - initial_subset_vecs = fill(fill(0, weight), length(itrs)) |
30 | | - bin = extended_binomial(len, weight) |
31 | | - for j in eachindex(itrs) |
32 | | - itr = itrs[j] |
33 | | - @test length(itr) == fld(bin, num_threads) |
34 | | - subset_vec = zeros(Int, itr.k) |
35 | | - CodingTheory._subset_unrank!(itr.init_rank, UInt(itr.n), subset_vec) |
36 | | - initial_subset_vecs[j] = subset_vec |
37 | | - end |
38 | | - |
39 | | - all_subsets_gray = fill(fill(0, weight), bin) |
40 | | - |
41 | | - for j in 0:length(itrs)-1 |
42 | | - sgc = itrs[j + 1] |
43 | | - subset_vec = initial_subset_vecs[j + 1] |
44 | | - state = (subset_vec, 1, fill(-1, 3)) |
45 | | - for i in 1:length(sgc) |
46 | | - all_subsets_gray[j * fld(bin, num_threads) + i] = deepcopy(subset_vec) |
47 | | - next = iterate(sgc, state) |
48 | | - isnothing(next) && break |
49 | | - (_, state) = next |
50 | | - (subset_vec, _, _) = state |
51 | | - end |
52 | | - end |
53 | | - sort!(all_subsets_gray) |
54 | | - all_subsets_hecke = CodingTheory.Oscar.subsets(collect(1:len), weight) |
55 | | - sort!(all_subsets_hecke) |
56 | | - @test length(all_subsets_gray) == 6435 |
| 19 | + @test length(all_subsets_gray) == 21 |
57 | 20 | @test all_subsets_gray == all_subsets_hecke |
58 | 21 | end |
59 | 22 |
|
|
79 | 42 | k1 = UInt(3) |
80 | 43 | n1 = UInt(5) |
81 | 44 | rank1 = CodingTheory._subset_rank(subset1, k1) |
82 | | - @test rank1 == 0 |
| 45 | + @test rank1 == 1 |
83 | 46 | result1 = zeros(Int, k1) |
84 | | - CodingTheory._subset_unrank!(rank1, n1, result1) |
| 47 | + CodingTheory._subset_unrank!(UInt128(rank1), n1, result1) |
85 | 48 | @test result1 == subset1 |
86 | 49 |
|
87 | 50 | subset2 = UInt.([1, 3, 5]) |
88 | 51 | k2 = UInt(3) |
89 | 52 | n2 = UInt(5) |
90 | 53 | rank2 = CodingTheory._subset_rank(subset2, k2) |
91 | | - @test rank2 == 7 |
| 54 | + @test rank2 == 8 |
92 | 55 | result2 = zeros(Int, k2) |
93 | | - CodingTheory._subset_unrank!(rank2, n2, result2) |
| 56 | + CodingTheory._subset_unrank!(UInt128(rank2), n2, result2) |
94 | 57 | @test result2 == subset2 |
95 | 58 |
|
96 | 59 | k3 = UInt(3) |
97 | 60 | n3 = UInt(5) |
98 | 61 | result3 = zeros(Int, k3) |
99 | | - results = Set() |
| 62 | + results = Set{Vector{UInt64}}() |
100 | 63 | bin = binomial(n3, k3) |
101 | | - for i::BigInt in collect(0: bin - 1) |
102 | | - CodingTheory._subset_unrank!(i, n3, result3) |
103 | | - pushOrDel!(results, deepcopy(result3)) |
| 64 | + for i::BigInt in collect(1: bin) |
| 65 | + CodingTheory._subset_unrank!(UInt128(i), n3, result3) |
| 66 | + pushOrDel!(results, deepcopy(Vector{UInt64}(result3))) |
104 | 67 | end |
105 | 68 | all_subsets_hecke = Set(CodingTheory.Hecke.subsets(collect(1:n3), Int(k3))) |
106 | 69 | @test results == all_subsets_hecke |
|
0 commit comments