Repository navigation
Expand file tree
/
Copy pathrr-optimization.js
More file actions
178 lines (149 loc) · 5.44 KB
/
Copy pathrr-optimization.js
File metadata and controls
178 lines (149 loc) · 5.44 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
// ============================================================================
// R/R (REMEMBER/RECALL) OPTIMIZATION MODULE
// ============================================================================
/**
* Finds the longest pattern of 2+ lines that repeats 3+ times
* @param {Array} lines - Array of logic lines
* @returns {Object|null} Best pattern found or null
*/
export function findBestPattern(lines) {
if (lines.length < 6) return null; // Need at least 6 lines for 2-line pattern × 3 occurrences
const patternMap = new Map();
// Check patterns of 2+ lines
for (let patternLength = 2; patternLength <= lines.length / 3; patternLength++) {
for (let startIndex = 0; startIndex <= lines.length - patternLength; startIndex++) {
const pattern = lines.slice(startIndex, startIndex + patternLength).join('_');
if (!patternMap.has(pattern)) {
// Count occurrences of this pattern
let count = 0;
for (let i = 0; i <= lines.length - patternLength; i++) {
const candidate = lines.slice(i, i + patternLength).join('_');
if (candidate === pattern) {
count++;
i += patternLength - 1; // Skip overlapping occurrences
}
}
if (count >= 3) {
patternMap.set(pattern, {
lines: lines.slice(startIndex, startIndex + patternLength),
count,
length: patternLength
});
}
}
}
}
if (patternMap.size === 0) return null;
// Find the longest pattern (most lines)
let bestPattern = null;
let maxLength = 0;
for (const [pattern, info] of patternMap) {
if (info.length > maxLength) {
maxLength = info.length;
bestPattern = info;
}
}
console.log('R/R: Found best pattern -', bestPattern.length, 'lines,', bestPattern.count, 'occurrences');
return bestPattern;
}
/**
* Applies R/R optimization to logic string
* @param {string} logicString - The logic string to optimize
* @returns {Object} Optimized logic and savings info
*/
export function applyRROptimization(logicString) {
console.log('R/R OPTIMIZATION START');
console.log('Input logic length:', logicString.length);
// Parse logic into lines
const lines = logicString.split('_').filter(line => line.trim());
console.log('Total lines:', lines.length);
// Find best pattern
const pattern = findBestPattern(lines);
if (!pattern) {
console.log('R/R: No qualifying patterns found');
return {
optimizedLogic: logicString,
originalLength: logicString.length,
optimizedLength: logicString.length,
savings: 0,
patternUsed: null
};
}
// Extract the last line and its flag
const lastLine = pattern.lines[pattern.lines.length - 1];
const flagMatch = lastLine.match(/^([A-Z]:)/);
const originalFlag = flagMatch ? flagMatch[1] : '';
if (!originalFlag) {
console.log('R/R: No flag found on last line of pattern');
return {
optimizedLogic: logicString,
originalLength: logicString.length,
optimizedLength: logicString.length,
savings: 0,
patternUsed: null
};
}
// Create remember pattern (replace last line's flag with K:)
const rememberLines = [...pattern.lines];
const lastLineIndex = rememberLines.length - 1;
rememberLines[lastLineIndex] = rememberLines[lastLineIndex].replace(originalFlag, 'K:');
const rememberPattern = rememberLines.join('_');
console.log('R/R: Replaced flag', originalFlag, 'with K: on last line');
console.log('R/R: Remember pattern:', rememberPattern);
// Create recall replacement
const recallReplacement = `${originalFlag}{recall}`;
console.log('R/R: Recall replacement:', recallReplacement);
// Build optimized logic
const originalPatternString = pattern.lines.join('_');
let optimizedLogic = rememberPattern + '_';
let remainingLines = [...lines];
let index = 0;
while (index < remainingLines.length) {
// Check if this position starts with our pattern
if (index + pattern.length <= remainingLines.length) {
const candidate = remainingLines.slice(index, index + pattern.length).join('_');
if (candidate === originalPatternString) {
// Replace with recall
optimizedLogic += recallReplacement;
index += pattern.length;
// Add underscore if not at end
if (index < remainingLines.length) {
optimizedLogic += '_';
}
} else {
// Keep original line
optimizedLogic += remainingLines[index];
index++;
// Add underscore if not at end
if (index < remainingLines.length) {
optimizedLogic += '_';
}
}
} else {
// Not enough lines for pattern, keep remaining
optimizedLogic += remainingLines.slice(index).join('_');
break;
}
}
// Remove trailing underscore
optimizedLogic = optimizedLogic.replace(/_$/, '');
const originalLength = logicString.length;
const optimizedLength = optimizedLogic.length;
const savings = originalLength - optimizedLength;
console.log('R/R: Original length:', originalLength);
console.log('R/R: Optimized length:', optimizedLength);
console.log('R/R: Savings:', savings, 'characters');
return {
optimizedLogic,
originalLength,
optimizedLength,
savings,
patternUsed: {
lines: pattern.lines,
count: pattern.count,
originalFlag,
rememberPattern,
recallReplacement
}
};
}