-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathupdate_code.py
More file actions
229 lines (198 loc) · 5.9 KB
/
update_code.py
File metadata and controls
229 lines (198 loc) · 5.9 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
import os
import re
utils_code = """use std::ops::{Index, IndexMut};
#[derive(Debug, Clone)]
pub struct RingBuffer<T> {
buffer: Vec<T>,
mask: usize,
head: usize,
tail: usize,
pub count: usize,
}
impl<T: Default + Clone> RingBuffer<T> {
pub fn new() -> Self {
Self::with_capacity(16)
}
pub fn with_capacity(capacity: usize) -> Self {
let pow2 = if capacity == 0 { 16 } else { capacity.next_power_of_two() };
Self {
buffer: vec![T::default(); pow2],
mask: pow2 - 1,
head: 0,
tail: 0,
count: 0,
}
}
#[inline]
pub fn push_back(&mut self, item: T) {
self.buffer[self.head & self.mask] = item;
self.head = self.head.wrapping_add(1);
self.count += 1;
}
#[inline]
pub fn pop_front(&mut self) -> Option<T> {
if self.count == 0 {
None
} else {
let item = self.buffer[self.tail & self.mask].clone();
self.tail = self.tail.wrapping_add(1);
self.count -= 1;
Some(item)
}
}
#[inline]
pub fn push_front(&mut self, item: T) {
self.tail = self.tail.wrapping_sub(1);
self.buffer[self.tail & self.mask] = item;
self.count += 1;
}
#[inline]
pub fn pop_back(&mut self) -> Option<T> {
if self.count == 0 {
None
} else {
self.head = self.head.wrapping_sub(1);
self.count -= 1;
Some(self.buffer[self.head & self.mask].clone())
}
}
#[inline]
pub fn len(&self) -> usize {
self.count
}
#[inline]
pub fn is_empty(&self) -> bool {
self.count == 0
}
#[inline]
pub fn front(&self) -> Option<&T> {
if self.count == 0 {
None
} else {
Some(&self.buffer[self.tail & self.mask])
}
}
#[inline]
pub fn back(&self) -> Option<&T> {
if self.count == 0 {
None
} else {
Some(&self.buffer[self.head.wrapping_sub(1) & self.mask])
}
}
#[inline]
pub fn clear(&mut self) {
self.head = 0;
self.tail = 0;
self.count = 0;
}
pub fn iter(&self) -> RingBufferIter<'_, T> {
RingBufferIter {
rb: self,
index: 0,
}
}
pub fn retain<F>(&mut self, mut f: F)
where
F: FnMut(&T) -> bool,
{
let mut new_count = 0;
let mut i = 0;
while i < self.count {
let idx = self.tail.wrapping_add(i) & self.mask;
if f(&self.buffer[idx]) {
if new_count != i {
let dest_idx = self.tail.wrapping_add(new_count) & self.mask;
self.buffer.swap(idx, dest_idx);
}
new_count += 1;
}
i += 1;
}
self.count = new_count;
self.head = self.tail.wrapping_add(new_count);
}
pub fn get(&self, index: usize) -> Option<&T> {
if index < self.count {
Some(&self.buffer[self.tail.wrapping_add(index) & self.mask])
} else {
None
}
}
}
pub struct RingBufferIter<'a, T> {
rb: &'a RingBuffer<T>,
index: usize,
}
impl<'a, T> Iterator for RingBufferIter<'a, T> {
type Item = &'a T;
fn next(&mut self) -> Option<Self::Item> {
if self.index < self.rb.count {
let actual_idx = self.rb.tail.wrapping_add(self.index) & self.rb.mask;
self.index += 1;
Some(&self.rb.buffer[actual_idx])
} else {
None
}
}
}
impl<'a, T> ExactSizeIterator for RingBufferIter<'a, T> {
fn len(&self) -> usize {
self.rb.count - self.index
}
}
impl<T> Index<usize> for RingBuffer<T> {
type Output = T;
#[inline]
fn index(&self, index: usize) -> &Self::Output {
assert!(index < self.count, "index out of bounds");
&self.buffer[self.tail.wrapping_add(index) & self.mask]
}
}
impl<T> IndexMut<usize> for RingBuffer<T> {
#[inline]
fn index_mut(&mut self, index: usize) -> &mut Self::Output {
assert!(index < self.count, "index out of bounds");
let mask = self.mask;
let actual = self.tail.wrapping_add(index) & mask;
&mut self.buffer[actual]
}
}
"""
with open("quantwave-core/src/utils.rs", "w") as f:
f.write(utils_code)
with open("quantwave-core/src/lib.rs", "r") as f:
lib_code = f.read()
if "pub mod utils;" not in lib_code:
lib_code = "pub mod utils;\n" + lib_code
with open("quantwave-core/src/lib.rs", "w") as f:
f.write(lib_code)
with open("quantwave-core/src/traits.rs", "r") as f:
traits_code = f.read()
new_trait_method = """ /// Process a batch of inputs eagerly.
/// This allows implementations to override with vectorized logic,
/// while falling back to scalar processing by default.
fn next_batch(&mut self, inputs: &[Input]) -> Vec<Self::Output>
where
Input: Copy,
{
let mut outputs = Vec::with_capacity(inputs.len());
for &input in inputs {
outputs.push(self.next(input));
}
outputs
}
}"""
traits_code = traits_code.replace(" fn next(&mut self, input: Input) -> Self::Output;\n}", " fn next(&mut self, input: Input) -> Self::Output;\n\n" + new_trait_method)
with open("quantwave-core/src/traits.rs", "w") as f:
f.write(traits_code)
for root, _, files in os.walk("quantwave-core/src/"):
for file in files:
if file.endswith(".rs"):
filepath = os.path.join(root, file)
with open(filepath, "r") as f:
content = f.read()
if "use std::collections::VecDeque;" in content:
content = content.replace("use std::collections::VecDeque;", "use crate::utils::RingBuffer as VecDeque;")
with open(filepath, "w") as f:
f.write(content)