-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPathOptimizer.h
More file actions
282 lines (230 loc) · 9.28 KB
/
PathOptimizer.h
File metadata and controls
282 lines (230 loc) · 9.28 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
// Author: Erkhembileg Ariunbold
// Project: ArchiveManager
// Date: 2025.06.06
#pragma once
#include <vector>
#include <string>
#include <unordered_map>
#include <queue>
#include <limits>
#include <algorithm>
#include <filesystem>
struct FileNode {
std::string path;
size_t size;
int compressionType; // 0=text, 1=image, 2=video, 3=binary, etc.
FileNode(const std::string& p, size_t s) : path(p), size(s) {
// Determine compression type based on extension
std::string ext = path.substr(path.find_last_of('.') + 1);
std::transform(ext.begin(), ext.end(), ext.begin(), ::tolower);
if (ext == "txt" || ext == "log" || ext == "xml" || ext == "json" || ext == "csv") {
compressionType = 0; // Text files - high compression ratio
} else if (ext == "jpg" || ext == "jpeg" || ext == "png" || ext == "gif") {
compressionType = 1; // Images - already compressed
} else if (ext == "mp4" || ext == "avi" || ext == "mkv" || ext == "mp3") {
compressionType = 2; // Media - already compressed
} else {
compressionType = 3; // Binary/other
}
}
};
struct CompressionEdge {
size_t to;
double weight;
double compressionBenefit;
CompressionEdge(size_t t, double w, double cb) : to(t), weight(w), compressionBenefit(cb) {}
};
class PathOptimizer {
private:
std::vector<FileNode> nodes;
std::vector<std::vector<CompressionEdge>> graph;
// Calculate compression benefit when files are adjacent in zip
double CalculateCompressionBenefit(const FileNode& current, const FileNode& next) {
// Same type files benefit from being grouped together
double typeBonus = (current.compressionType == next.compressionType) ? 0.3 : 0.0;
// Size similarity helps with compression dictionary
double sizeFactor = 1.0 - std::abs((double)current.size - (double)next.size) /
std::max((double)current.size, (double)next.size);
sizeFactor *= 0.2; // Weight this factor
// Text files benefit most from being grouped
double compressionMultiplier = 1.0;
if (current.compressionType == 0 && next.compressionType == 0) {
compressionMultiplier = 1.5; // Text files compress better when grouped
} else if (current.compressionType == 1 || next.compressionType == 1) {
compressionMultiplier = 0.5; // Images don't compress much more
}
return (typeBonus + sizeFactor) * compressionMultiplier;
}
// Weight represents the "cost" of having these files adjacent (lower = better)
double CalculateTransitionWeight(const FileNode& from, const FileNode& to) {
double benefit = CalculateCompressionBenefit(from, to);
// Convert benefit to cost (higher benefit = lower cost)
return 1.0 - benefit;
}
public:
void AddFile(const std::string& path, size_t size) {
nodes.emplace_back(path, size);
}
void Clear() {
nodes.clear();
graph.clear();
}
void BuildCompressionGraph() {
size_t n = nodes.size();
if (n == 0) return;
graph.clear();
graph.resize(n);
// Build complete graph where each node connects to every other node
for (size_t i = 0; i < n; ++i) {
for (size_t j = 0; j < n; ++j) {
if (i != j) {
double weight = CalculateTransitionWeight(nodes[i], nodes[j]);
double benefit = CalculateCompressionBenefit(nodes[i], nodes[j]);
graph[i].emplace_back(j, weight, benefit);
}
}
}
}
// Use Dijkstra to find optimal file ordering for compression
std::vector<size_t> FindOptimalCompressionOrder() {
size_t n = nodes.size();
if (n == 0) return {};
if (n == 1) return {0};
// Find best starting node (largest text file or largest file overall)
size_t startNode = 0;
size_t maxTextSize = 0;
size_t maxOverallSize = 0;
for (size_t i = 0; i < n; ++i) {
if (nodes[i].compressionType == 0 && nodes[i].size > maxTextSize) {
maxTextSize = nodes[i].size;
startNode = i;
}
if (nodes[i].size > maxOverallSize) {
maxOverallSize = nodes[i].size;
if (maxTextSize == 0) startNode = i; // Fallback to largest file
}
}
std::vector<size_t> order;
std::vector<bool> visited(n, false);
size_t current = startNode;
order.push_back(current);
visited[current] = true;
// Use modified Dijkstra approach: greedy selection of next best node
while (order.size() < n) {
double bestWeight = std::numeric_limits<double>::infinity();
size_t bestNext = SIZE_MAX;
// Find unvisited node with minimum weight from current node
for (const auto& edge : graph[current]) {
if (!visited[edge.to] && edge.weight < bestWeight) {
bestWeight = edge.weight;
bestNext = edge.to;
}
}
if (bestNext != SIZE_MAX) {
order.push_back(bestNext);
visited[bestNext] = true;
current = bestNext;
} else {
// Find any remaining unvisited node
for (size_t i = 0; i < n; ++i) {
if (!visited[i]) {
order.push_back(i);
visited[i] = true;
current = i;
break;
}
}
}
}
return order;
}
// Alternative: Use full Dijkstra's algorithm to find minimum spanning tree approach
std::vector<size_t> FindOptimalOrderDijkstra() {
size_t n = nodes.size();
if (n <= 1) return n == 1 ? std::vector<size_t>{0} : std::vector<size_t>{};
// Use Dijkstra to build minimum spanning tree for file ordering
std::vector<double> dist(n, std::numeric_limits<double>::infinity());
std::vector<size_t> parent(n, SIZE_MAX);
std::vector<bool> inMST(n, false);
// Priority queue: {weight, node}
std::priority_queue<std::pair<double, size_t>,
std::vector<std::pair<double, size_t>>,
std::greater<std::pair<double, size_t>>> pq;
// Start with largest text file or largest file
size_t start = 0;
for (size_t i = 0; i < n; ++i) {
if (nodes[i].compressionType == 0 && nodes[i].size > nodes[start].size) {
start = i;
}
}
dist[start] = 0.0;
pq.push({0.0, start});
std::vector<std::pair<size_t, size_t>> mstEdges; // {from, to}
while (!pq.empty()) {
double d = pq.top().first;
size_t u = pq.top().second;
pq.pop();
if (inMST[u]) continue;
inMST[u] = true;
if (parent[u] != SIZE_MAX) {
mstEdges.push_back({parent[u], u});
}
// Update distances to adjacent nodes
for (const auto& edge : graph[u]) {
size_t v = edge.to;
if (!inMST[v] && edge.weight < dist[v]) {
dist[v] = edge.weight;
parent[v] = u;
pq.push({edge.weight, v});
}
}
}
// Convert MST to linear ordering using DFS
std::vector<size_t> order;
std::vector<bool> visited(n, false);
std::vector<std::vector<size_t>> adjList(n);
// Build adjacency list from MST edges
for (const auto& edge : mstEdges) {
adjList[edge.first].push_back(edge.second);
adjList[edge.second].push_back(edge.first);
}
// DFS to create linear order
std::function<void(size_t)> dfs = [&](size_t node) {
visited[node] = true;
order.push_back(node);
for (size_t neighbor : adjList[node]) {
if (!visited[neighbor]) {
dfs(neighbor);
}
}
};
dfs(start);
return order;
}
std::vector<FileNode> GetOptimizedFileOrder() {
if (nodes.empty()) return {};
BuildCompressionGraph();
// Try both approaches and pick the one with better estimated compression
auto order1 = FindOptimalCompressionOrder();
auto order2 = FindOptimalOrderDijkstra();
// Use the simpler greedy approach for now
auto bestOrder = order1;
std::vector<FileNode> result;
result.reserve(bestOrder.size());
for (size_t idx : bestOrder) {
if (idx < nodes.size()) {
result.push_back(nodes[idx]);
}
}
return result;
}
// Calculate estimated compression ratio for current order
double EstimateCompressionRatio(const std::vector<size_t>& order) {
if (order.size() <= 1) return 1.0;
double totalBenefit = 0.0;
for (size_t i = 0; i < order.size() - 1; ++i) {
totalBenefit += CalculateCompressionBenefit(nodes[order[i]], nodes[order[i + 1]]);
}
return 1.0 + totalBenefit / order.size(); // Higher is better
}
};