1 | /* |
1 | /* |
2 | * This file is part of Deliantra, the Roguelike Realtime MMORPG. |
2 | * This file is part of Deliantra, the Roguelike Realtime MMORPG. |
3 | * |
3 | * |
4 | * Copyright (©) 2010 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
4 | * Copyright (©) 2010,2011 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
5 | * |
5 | * |
6 | * Deliantra is free software: you can redistribute it and/or modify it under |
6 | * Deliantra is free software: you can redistribute it and/or modify it under |
7 | * the terms of the Affero GNU General Public License as published by the |
7 | * the terms of the Affero GNU General Public License as published by the |
8 | * Free Software Foundation, either version 3 of the License, or (at your |
8 | * Free Software Foundation, either version 3 of the License, or (at your |
9 | * option) any later version. |
9 | * option) any later version. |
… | |
… | |
36 | template<int N, typename T = uint8_t> |
36 | template<int N, typename T = uint8_t> |
37 | struct permutation |
37 | struct permutation |
38 | { |
38 | { |
39 | T pmap[N]; |
39 | T pmap[N]; |
40 | |
40 | |
41 | template<class generator> |
41 | template<class random_generator> |
42 | void seed (generator &rng) |
42 | void seed (random_generator &rng); |
43 | { |
|
|
44 | for (int i = 0; i < N; ++i) |
|
|
45 | pmap[i] = i; |
|
|
46 | |
|
|
47 | // fisher-yates to randomly perturb |
|
|
48 | for (int i = N; --i; ) |
|
|
49 | ::swap (pmap[i], pmap[rng (i + 1)]); |
|
|
50 | } |
|
|
51 | |
43 | |
52 | T operator ()(T v) |
44 | T operator ()(T v) |
53 | { |
45 | { |
54 | return pmap[v & (N - 1)]; |
46 | return pmap[v & (N - 1)]; |
55 | } |
47 | } |