-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMappingGenerator.h
More file actions
168 lines (132 loc) · 6.01 KB
/
MappingGenerator.h
File metadata and controls
168 lines (132 loc) · 6.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#pragma once
#include "Mapping.h"
#include <vector>
#include <memory>
#include <mutex>
#include <atomic>
#include <string>
#include <fstream>
#include <cstdint>
#include <unordered_map>
#include <deque>
#include <chrono>
// Forward declaration for JSON support
namespace nlohmann { class json; }
// Forward declaration for friend class
class VoynichDecoder;
class MappingGenerator {
friend class VoynichDecoder;
public:
// Block states for tracking
enum class BlockState {
PENDING, // Block assigned to thread, work in progress
COMPLETED // Block completed by thread
};
// Block information with enhanced tracking
struct BlockInfo {
uint64_t blockIndex;
BlockState state;
int assignedThreadId; // -1 if not assigned
std::chrono::system_clock::time_point assignedTime;
std::chrono::system_clock::time_point completedTime;
BlockInfo() : blockIndex(0), state(BlockState::PENDING), assignedThreadId(-1) {}
BlockInfo(uint64_t idx, int threadId) : blockIndex(idx), state(BlockState::PENDING), assignedThreadId(threadId) {
assignedTime = std::chrono::system_clock::now();
}
};
// Overall generator state
struct GeneratorState {
uint64_t nextBlockToGenerate; // Next block index to create
uint64_t oldestTrackedBlock; // Lowest block index still in sliding window
uint64_t totalBlocksGenerated; // Total blocks created so far
uint64_t totalBlocksCompleted; // Total blocks completed
bool isComplete; // Whether all combinations have been generated
GeneratorState() : nextBlockToGenerate(0), oldestTrackedBlock(0),
totalBlocksGenerated(0), totalBlocksCompleted(0), isComplete(false) {}
};
// Configuration for the generator
struct GeneratorConfig {
size_t blockSize; // Number of mappings per block (default: 1,000,000)
std::string stateFilePath; // Path to JSON state file
bool enableStateFile; // Whether to use persistent state
GeneratorConfig() : blockSize(1000000), stateFilePath("mapping_generator_state.json"),
enableStateFile(true) {}
};
private:
mutable std::mutex generatorMutex; // Protects all generator operations
GeneratorState state; // Current generator state
GeneratorConfig config; // Generator configuration
// Dynamic window of block tracking (ordered by block index)
std::deque<BlockInfo> blockWindow; // Blocks in memory for tracking
std::unordered_map<int, uint64_t> threadToBlock; // threadId -> current block index
// Total possible combinations (27! permutations)
static constexpr uint64_t TOTAL_COMBINATIONS = 10888869450418352160ULL; // 27!
// Internal mapping generation
std::vector<std::unique_ptr<Mapping>> generateBlock(uint64_t blockIndex);
bool generateSingleMapping(uint64_t globalIndex, std::unique_ptr<Mapping>& mapping);
// Combinatorial mathematics
std::vector<int> indexToPermutation(uint64_t index) const;
uint64_t factorial(int n) const;
// JSON state management
bool loadStateFromJson();
bool saveStateToJson() const;
std::string blockStateToString(BlockState state) const;
BlockState stringToBlockState(const std::string& str) const;
// Enhanced block management
BlockInfo* findBlockInWindow(uint64_t blockIndex);
uint64_t createNewBlockForThread(int threadId);
void cleanupCompletedBlocks();
void completeBlockForThread(int threadId);
void completeCurrentBlock(int threadId); // Friend access only
// Dynamic window management
void addBlockToWindow(uint64_t blockIndex, int threadId);
void removeCompletedSequentialBlocks();
void loadPendingBlocksFromState();
void loadBlockWindowFromJson();
public:
explicit MappingGenerator(const GeneratorConfig& config = GeneratorConfig());
~MappingGenerator();
// Get next block for a thread (thread-safe) - returns full block
std::vector<std::unique_ptr<Mapping>> getNextBlock(int threadId);
// Check if generation is complete
bool isGenerationComplete() const;
// Get current statistics
GeneratorState getCurrentState() const;
// Get total possible combinations
static uint64_t getTotalCombinations() { return TOTAL_COMBINATIONS; }
// Get progress percentage
double getProgressPercentage() const;
// Reset generator to beginning (clears state file if enabled)
void reset();
// Force save current state to file
bool saveCurrentState() const;
// Get estimated remaining mappings
uint64_t getRemainingMappings() const;
// Enhanced status information
struct BlockStatus {
size_t blockSize;
uint64_t nextBlockToGenerate;
uint64_t oldestTrackedBlock;
size_t activeBlocks; // Blocks in PENDING state
size_t completedBlocks; // Total completed blocks
size_t windowSize; // Current window size
};
BlockStatus getBlockStatus() const;
// Get current block assignment for a thread
struct ThreadBlockInfo {
uint64_t blockIndex;
bool hasActiveBlock;
BlockState blockState;
std::chrono::system_clock::time_point assignedTime;
};
ThreadBlockInfo getThreadBlockInfo(int threadId) const;
// Get all blocks in window (for debugging/monitoring)
std::vector<BlockInfo> getWindowSnapshot() const;
// Complete a block for testing purposes
void completeBlockForTesting(int threadId);
};
// JSON serialization helpers
void to_json(nlohmann::json& j, const MappingGenerator::BlockInfo& block);
void from_json(const nlohmann::json& j, MappingGenerator::BlockInfo& block);
void to_json(nlohmann::json& j, const MappingGenerator::GeneratorState& state);
void from_json(const nlohmann::json& j, MappingGenerator::GeneratorState& state);