Zip and Skip Tries
Loading...
Searching...
No Matches
synthetic.cpp
Go to the documentation of this file.
1
29#include "synthetic.hpp"
30
31#include <algorithm>
32#include <random>
33#include <string>
34#include <vector>
35
44static const std::string ALPHABET = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~";
45
59std::string get_random_word(size_t length) noexcept
60{
61 // Use a static random device for initialization of the generator
62 static std::random_device rd;
63 // Uncomment for deterministic generation with fixed seed:
64 // static std::mt19937 gen(0);
65 // Use random seed for non-deterministic generation
66 static std::mt19937 gen(rd());
67
68 // Create uniform distribution for selecting characters from the alphabet
69 std::uniform_int_distribution<size_t> dis(0, ALPHABET.size() - 1);
70
71 // Pre-allocate string of requested length
72 std::string str(length, '\0');
73 // Fill the string with randomly selected characters from the alphabet
74 std::generate_n(str.begin(), length, [&] { return ALPHABET[dis(gen)]; });
75
76 return str;
77}
78
100std::vector<std::string> get_random_words(size_t length, size_t num_words, double mean_lcp_length) noexcept
101{
102 // Use Poisson distribution to generate varying LCP lengths with specified mean
103 static std::random_device rd;
104 // Uncomment for deterministic generation with fixed seed:
105 // static std::mt19937 gen(0);
106 // Use random seed for non-deterministic generation
107 static std::mt19937 gen(rd());
108
109 // Create distribution for common prefix lengths
110 std::poisson_distribution<size_t> dis(mean_lcp_length);
111
112 // Initialize vector with completely random words of specified length
113 std::vector<std::string> words(num_words, get_random_word(length));
114
115 // Calculate required signature length to ensure uniqueness
116 // The length must satisfy: alphabet_size^signature_length > num_words
117 // Using logarithm properties: signature_length > log(num_words)/log(alphabet_size)
118 unsigned signature_length = std::ceil(std::log(num_words) / std::log(ALPHABET.size()));
119
120 for (size_t i = 0; i < num_words; ++i)
121 {
122 auto& word = words[i];
123
124 // Create a unique signature at the end of each word
125 // This encodes the index i as a base-ALPHABET.size() number
126 size_t i_copy = i;
127 for (size_t j = 0; j < signature_length; ++j)
128 {
130 i_copy /= ALPHABET.size();
131 }
132
133 // Generate a random LCP length from the Poisson distribution
134 size_t difference = dis(gen);
135
136 // Skip modification if the difference would affect the unique signature
138 {
139 continue;
140 }
141
142 // Generate a new random substring to replace part of the word
143 // This creates varying common prefix lengths between words
145 std::copy(random_word.begin(), random_word.end(), word.begin() + difference);
146 }
147
148 return words;
149}
T * alloc_to_device(size_t size)
Allocates memory on the CUDA device.
std::string get_random_word(size_t length) noexcept
Implementation of get_random_word function.
Definition synthetic.cpp:59
std::vector< std::string > get_random_words(size_t length, size_t num_words, double mean_lcp_length) noexcept
Implementation of get_random_words function.
static const std::string ALPHABET
The alphabet used for generating random strings.
Definition synthetic.cpp:44
Provides utilities for generating synthetic string datasets for benchmark testing.