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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/// Column-oriented storage for SIMD operations
pub struct ColumnStore<T> {
data: Vec<T>,
#[allow(dead_code)]
capacity: usize,
}
impl<T: Clone> ColumnStore<T> {
#[must_use]
pub fn new(capacity: usize) -> Self {
Self {
data: Vec::with_capacity(capacity),
capacity,
}
}
pub fn push(&mut self, item: T) -> NodeKey {
let key = self.data.len() as NodeKey;
self.data.push(item);
key
}
#[must_use]
pub fn get(&self, key: NodeKey) -> Option<&T> {
self.data.get(key as usize)
}
pub fn get_mut(&mut self, key: NodeKey) -> Option<&mut T> {
self.data.get_mut(key as usize)
}
pub fn iter(&self) -> impl Iterator<Item = &T> {
self.data.iter()
}
#[must_use]
pub fn len(&self) -> usize {
self.data.len()
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.data.is_empty()
}
}
/// AST DAG structure for efficient traversal and analysis
pub struct AstDag {
/// Columnar storage for SIMD operations
pub nodes: ColumnStore<UnifiedAstNode>,
/// Language-specific parsers for multi-language support
pub parsers: LanguageParsers,
/// Incremental update tracking
pub dirty_nodes: roaring::RoaringBitmap,
/// Generation counter for cache invalidation
pub generation: std::sync::atomic::AtomicU32,
}
impl Default for AstDag {
fn default() -> Self {
Self::new()
}
}
impl AstDag {
/// Creates a new AST DAG with default configuration.
///
/// Initializes an empty DAG with:
/// - Column store with 10,000 initial node capacity
/// - Empty roaring bitmap for dirty node tracking
/// - Generation counter starting at 0
/// - Default language parsers
///
/// # Performance Characteristics
///
/// - Memory: ~40MB initial allocation for node storage
/// - Insertion: O(1) amortized with occasional reallocation
/// - Lookup: O(1) by node key
/// - Dirty tracking: O(1) insertion/removal with compressed bitmaps
///
/// # Examples
///
/// ```rust
/// use pmat::models::unified_ast::{
/// AstDag, UnifiedAstNode, AstKind, FunctionKind, Language
/// };
///
/// let mut dag = AstDag::new();
///
/// // Initially empty
/// assert_eq!(dag.nodes.len(), 0);
/// assert!(dag.nodes.is_empty());
/// assert_eq!(dag.generation(), 0);
///
/// // Add a node
/// let node = UnifiedAstNode::new(
/// AstKind::Function(FunctionKind::Regular),
/// Language::Rust
/// );
/// let key = dag.add_node(node);
///
/// assert_eq!(dag.nodes.len(), 1);
/// assert_eq!(dag.generation(), 1);
/// assert!(dag.dirty_nodes().any(|k| k == key));
/// ```
#[must_use]
pub fn new() -> Self {
Self {
nodes: ColumnStore::new(10000), // Initial capacity
parsers: LanguageParsers::default(),
dirty_nodes: roaring::RoaringBitmap::new(),
generation: std::sync::atomic::AtomicU32::new(0),
}
}
/// Adds a new node to the DAG and returns its unique key.
///
/// The node is automatically marked as dirty and the generation
/// counter is incremented for cache invalidation.
///
/// # Performance
///
/// - Time: O(1) amortized, O(n) worst case during reallocation
/// - Space: Constant overhead per node
///
/// # Examples
///
/// ```rust
/// use pmat::models::unified_ast::{
/// AstDag, UnifiedAstNode, AstKind, FunctionKind, ClassKind, Language
/// };
///
/// let mut dag = AstDag::new();
/// let initial_gen = dag.generation();
///
/// // Add multiple nodes
/// let func_node = UnifiedAstNode::new(
/// AstKind::Function(FunctionKind::Regular),
/// Language::Rust
/// );
/// let class_node = UnifiedAstNode::new(
/// AstKind::Class(ClassKind::Struct),
/// Language::Rust
/// );
///
/// let func_key = dag.add_node(func_node);
/// let class_key = dag.add_node(class_node);
///
/// // Keys are unique and sequential
/// assert_ne!(func_key, class_key);
/// assert_eq!(dag.nodes.len(), 2);
///
/// // Generation incremented for each addition
/// assert_eq!(dag.generation(), initial_gen + 2);
///
/// // Both nodes are dirty
/// let dirty: Vec<_> = dag.dirty_nodes().collect();
/// assert_eq!(dirty.len(), 2);
/// assert!(dirty.contains(&func_key));
/// assert!(dirty.contains(&class_key));
///
/// // Nodes can be retrieved by key
/// assert!(dag.nodes.get(func_key).expect("internal error").is_function());
/// assert!(dag.nodes.get(class_key).expect("internal error").is_type_definition());
/// ```
pub fn add_node(&mut self, node: UnifiedAstNode) -> NodeKey {
let key = self.nodes.push(node);
self.dirty_nodes.insert(key);
self.generation
.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
key
}
/// Marks a node as clean (processed) by removing it from the dirty set.
///
/// This is typically called after processing a node for incremental
/// analysis to avoid reprocessing unchanged nodes.
///
/// # Performance
///
/// - Time: O(1) for roaring bitmap removal
/// - Space: No additional allocation
///
/// # Examples
///
/// ```rust
/// use pmat::models::unified_ast::{
/// AstDag, UnifiedAstNode, AstKind, FunctionKind, Language
/// };
///
/// let mut dag = AstDag::new();
///
/// let node = UnifiedAstNode::new(
/// AstKind::Function(FunctionKind::Regular),
/// Language::Rust
/// );
/// let key = dag.add_node(node);
///
/// // Node starts dirty
/// assert!(dag.dirty_nodes().any(|k| k == key));
///
/// // Mark as processed
/// dag.mark_clean(key);
///
/// // No longer in dirty set
/// assert!(!dag.dirty_nodes().any(|k| k == key));
///
/// // Node still exists in the DAG
/// assert!(dag.nodes.get(key).is_some());
/// ```
pub fn mark_clean(&mut self, key: NodeKey) {
self.dirty_nodes.remove(key);
}
/// Returns an iterator over all dirty (unprocessed) node keys.
///
/// Dirty nodes are those that have been added or modified since
/// the last processing cycle. This enables efficient incremental
/// analysis by only processing changed nodes.
///
/// # Performance
///
/// - Time: O(1) to create iterator, O(k) to iterate where k = dirty count
/// - Space: No additional allocation (streaming iterator)
///
/// # Examples
///
/// ```rust
/// use pmat::models::unified_ast::{
/// AstDag, UnifiedAstNode, AstKind, FunctionKind, Language
/// };
///
/// let mut dag = AstDag::new();
///
/// // Initially no dirty nodes
/// assert_eq!(dag.dirty_nodes().count(), 0);
///
/// // Add some nodes
/// let keys: Vec<_> = (0..3).map(|_| {
/// dag.add_node(UnifiedAstNode::new(
/// AstKind::Function(FunctionKind::Regular),
/// Language::Rust
/// ))
/// }).collect();
///
/// // All nodes are dirty
/// assert_eq!(dag.dirty_nodes().count(), 3);
///
/// // Process some nodes
/// dag.mark_clean(keys[0]);
/// dag.mark_clean(keys[2]);
///
/// // Only unprocessed nodes remain dirty
/// let dirty: Vec<_> = dag.dirty_nodes().collect();
/// assert_eq!(dirty.len(), 1);
/// assert_eq!(dirty[0], keys[1]);
/// ```
pub fn dirty_nodes(&self) -> impl Iterator<Item = NodeKey> + '_ {
self.dirty_nodes.iter()
}
/// Returns the current generation number for cache invalidation.
///
/// The generation number is incremented each time a node is added
/// to the DAG, providing a monotonic cache invalidation key.
///
/// # Performance
///
/// - Time: O(1) atomic load with relaxed ordering
/// - Thread-safe: Can be called from multiple threads
///
/// # Examples
///
/// ```rust
/// use pmat::models::unified_ast::{
/// AstDag, UnifiedAstNode, AstKind, FunctionKind, Language
/// };
///
/// let mut dag = AstDag::new();
///
/// // Starts at generation 0
/// assert_eq!(dag.generation(), 0);
///
/// // Generation increments with each node addition
/// dag.add_node(UnifiedAstNode::new(
/// AstKind::Function(FunctionKind::Regular),
/// Language::Rust
/// ));
/// assert_eq!(dag.generation(), 1);
///
/// dag.add_node(UnifiedAstNode::new(
/// AstKind::Function(FunctionKind::Method),
/// Language::TypeScript
/// ));
/// assert_eq!(dag.generation(), 2);
///
/// // Marking clean does not change generation
/// dag.mark_clean(0);
/// assert_eq!(dag.generation(), 2);
/// ```
pub fn generation(&self) -> u32 {
self.generation.load(std::sync::atomic::Ordering::Relaxed)
}
}
/// Placeholder for language-specific parsers
#[derive(Default)]
pub struct LanguageParsers {
// TRACKED: Add actual parser implementations
}