sklears-utils 0.1.2

Shared utilities for sklears: validation, data generation, helpers
Documentation
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
//! Auto-generated module
//!
//! 🤖 Generated with [SplitRS](https://github.com/cool-japan/splitrs)

use crate::{UtilsError, UtilsResult};
use scirs2_core::ndarray::Array2;
use scirs2_core::numeric::Zero;
use std::collections::{HashMap, HashSet, VecDeque};
use std::fmt;
use std::hash::Hash;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::{Arc, Mutex, RwLock};

/// Thread-safe queue using VecDeque
#[derive(Clone, Debug)]
pub struct ConcurrentQueue<T> {
    pub(crate) inner: Arc<Mutex<VecDeque<T>>>,
}
impl<T: Clone> ConcurrentQueue<T> {
    /// Create a new concurrent queue
    pub fn new() -> Self {
        Self {
            inner: Arc::new(Mutex::new(VecDeque::new())),
        }
    }
    /// Push an element to the back of the queue
    pub fn push_back(&self, item: T) -> UtilsResult<()> {
        let mut queue = self
            .inner
            .lock()
            .map_err(|_| UtilsError::InvalidParameter("Lock poisoned".to_string()))?;
        queue.push_back(item);
        Ok(())
    }
    /// Push an element to the front of the queue
    pub fn push_front(&self, item: T) -> UtilsResult<()> {
        let mut queue = self
            .inner
            .lock()
            .map_err(|_| UtilsError::InvalidParameter("Lock poisoned".to_string()))?;
        queue.push_front(item);
        Ok(())
    }
    /// Pop an element from the front of the queue
    pub fn pop_front(&self) -> UtilsResult<Option<T>> {
        let mut queue = self
            .inner
            .lock()
            .map_err(|_| UtilsError::InvalidParameter("Lock poisoned".to_string()))?;
        Ok(queue.pop_front())
    }
    /// Pop an element from the back of the queue
    pub fn pop_back(&self) -> UtilsResult<Option<T>> {
        let mut queue = self
            .inner
            .lock()
            .map_err(|_| UtilsError::InvalidParameter("Lock poisoned".to_string()))?;
        Ok(queue.pop_back())
    }
    /// Get the current size of the queue
    pub fn len(&self) -> UtilsResult<usize> {
        let queue = self
            .inner
            .lock()
            .map_err(|_| UtilsError::InvalidParameter("Lock poisoned".to_string()))?;
        Ok(queue.len())
    }
    /// Check if the queue is empty
    pub fn is_empty(&self) -> UtilsResult<bool> {
        let queue = self
            .inner
            .lock()
            .map_err(|_| UtilsError::InvalidParameter("Lock poisoned".to_string()))?;
        Ok(queue.is_empty())
    }
}
/// Thread-safe wrapper around HashMap
#[derive(Clone, Debug)]
pub struct ConcurrentHashMap<K, V> {
    pub(crate) inner: Arc<RwLock<HashMap<K, V>>>,
}
impl<K: Clone + Eq + Hash, V: Clone> ConcurrentHashMap<K, V> {
    /// Create a new concurrent hashmap
    pub fn new() -> Self {
        Self {
            inner: Arc::new(RwLock::new(HashMap::new())),
        }
    }
    /// Insert a key-value pair
    pub fn insert(&self, key: K, value: V) -> UtilsResult<Option<V>> {
        let mut map = self
            .inner
            .write()
            .map_err(|_| UtilsError::InvalidParameter("Lock poisoned".to_string()))?;
        Ok(map.insert(key, value))
    }
    /// Get a value by key
    pub fn get(&self, key: &K) -> UtilsResult<Option<V>> {
        let map = self
            .inner
            .read()
            .map_err(|_| UtilsError::InvalidParameter("Lock poisoned".to_string()))?;
        Ok(map.get(key).cloned())
    }
    /// Remove a key-value pair
    pub fn remove(&self, key: &K) -> UtilsResult<Option<V>> {
        let mut map = self
            .inner
            .write()
            .map_err(|_| UtilsError::InvalidParameter("Lock poisoned".to_string()))?;
        Ok(map.remove(key))
    }
    /// Check if key exists
    pub fn contains_key(&self, key: &K) -> UtilsResult<bool> {
        let map = self
            .inner
            .read()
            .map_err(|_| UtilsError::InvalidParameter("Lock poisoned".to_string()))?;
        Ok(map.contains_key(key))
    }
    /// Get the number of entries
    pub fn len(&self) -> UtilsResult<usize> {
        let map = self
            .inner
            .read()
            .map_err(|_| UtilsError::InvalidParameter("Lock poisoned".to_string()))?;
        Ok(map.len())
    }
    /// Check if the map is empty
    pub fn is_empty(&self) -> UtilsResult<bool> {
        let map = self
            .inner
            .read()
            .map_err(|_| UtilsError::InvalidParameter("Lock poisoned".to_string()))?;
        Ok(map.is_empty())
    }
}
/// Iterator for RingBuffer
pub struct RingBufferIter<'a, T> {
    pub(crate) buffer: &'a RingBuffer<T>,
    pub(crate) index: usize,
}
/// Binary tree node
#[derive(Clone, Debug)]
pub struct TreeNode<T> {
    pub value: T,
    pub left: Option<Box<TreeNode<T>>>,
    pub right: Option<Box<TreeNode<T>>>,
}
impl<T> TreeNode<T> {
    /// Create a new tree node
    pub fn new(value: T) -> Self {
        Self {
            value,
            left: None,
            right: None,
        }
    }
    /// Create a new tree node with children
    pub fn with_children(
        value: T,
        left: Option<Box<TreeNode<T>>>,
        right: Option<Box<TreeNode<T>>>,
    ) -> Self {
        Self { value, left, right }
    }
}
/// Weighted graph representation
#[derive(Clone, Debug)]
pub struct WeightedGraph<T, W> {
    pub(crate) vertices: Vec<T>,
    pub(crate) edges: Vec<Vec<(usize, W)>>,
    pub(crate) vertex_map: HashMap<T, usize>,
}
impl<T: Clone + Eq + Hash, W: Clone + PartialOrd> WeightedGraph<T, W> {
    /// Create a new empty weighted graph
    pub fn new() -> Self {
        Self {
            vertices: Vec::new(),
            edges: Vec::new(),
            vertex_map: HashMap::new(),
        }
    }
    /// Add a vertex to the graph
    pub fn add_vertex(&mut self, vertex: T) -> usize {
        if let Some(&idx) = self.vertex_map.get(&vertex) {
            return idx;
        }
        let idx = self.vertices.len();
        self.vertices.push(vertex.clone());
        self.edges.push(Vec::new());
        self.vertex_map.insert(vertex, idx);
        idx
    }
    /// Add a weighted edge between two vertices
    pub fn add_edge(&mut self, from: &T, to: &T, weight: W) -> UtilsResult<()> {
        let from_idx = self
            .vertex_map
            .get(from)
            .ok_or_else(|| UtilsError::InvalidParameter("From vertex not found".to_string()))?;
        let to_idx = self
            .vertex_map
            .get(to)
            .ok_or_else(|| UtilsError::InvalidParameter("To vertex not found".to_string()))?;
        self.edges[*from_idx].push((*to_idx, weight));
        Ok(())
    }
    /// Get the minimum spanning tree using Kruskal's algorithm
    pub fn minimum_spanning_tree(&self) -> UtilsResult<Vec<(usize, usize, W)>>
    where
        W: Clone + PartialOrd + Copy,
    {
        if self.vertices.is_empty() {
            return Ok(Vec::new());
        }
        let mut edges = Vec::new();
        for (from, adj_list) in self.edges.iter().enumerate() {
            for &(to, weight) in adj_list {
                edges.push((weight, from, to));
            }
        }
        edges.sort_by(|a, b| a.0.partial_cmp(&b.0).unwrap_or(std::cmp::Ordering::Equal));
        let mut parent = (0..self.vertices.len()).collect::<Vec<_>>();
        let mut mst = Vec::new();
        fn find(parent: &mut Vec<usize>, x: usize) -> usize {
            if parent[x] != x {
                parent[x] = find(parent, parent[x]);
            }
            parent[x]
        }
        fn union(parent: &mut Vec<usize>, x: usize, y: usize) {
            let px = find(parent, x);
            let py = find(parent, y);
            if px != py {
                parent[px] = py;
            }
        }
        for (weight, from, to) in edges {
            if find(&mut parent, from) != find(&mut parent, to) {
                union(&mut parent, from, to);
                mst.push((from, to, weight));
            }
        }
        Ok(mst)
    }
    /// Get vertices as a reference
    pub fn vertices(&self) -> &[T] {
        &self.vertices
    }
    /// Get edges as a reference
    pub fn edges(&self) -> &[Vec<(usize, W)>] {
        &self.edges
    }
    /// Serialize the weighted graph to a string representation
    pub fn serialize(&self) -> String
    where
        T: fmt::Display,
        W: fmt::Display,
    {
        let mut result = String::new();
        result.push_str("WeightedGraph {\n");
        result.push_str(&format!("  vertices: {} nodes\n", self.vertices.len()));
        let total_edges: usize = self.edges.iter().map(|edges| edges.len()).sum();
        result.push_str(&format!("  edges: {total_edges} weighted connections\n"));
        result.push_str("  adjacency_list: {\n");
        for (i, vertex) in self.vertices.iter().enumerate() {
            result.push_str(&format!("    {vertex}: ["));
            let edge_strs: Vec<String> = self.edges[i]
                .iter()
                .map(|(to_idx, weight)| format!("{}:{}", self.vertices[*to_idx], weight))
                .collect();
            result.push_str(&edge_strs.join(", "));
            result.push_str("]\n");
        }
        result.push_str("  }\n");
        result.push_str("}\n");
        result
    }
    /// Visualize the weighted graph structure
    pub fn visualize(&self) -> String
    where
        T: fmt::Display,
        W: fmt::Display,
    {
        let mut result = String::from("Weighted Graph Visualization:\n");
        if self.vertices.is_empty() {
            result.push_str("  (empty graph)\n");
            return result;
        }
        let total_edges: usize = self.edges.iter().map(|edges| edges.len()).sum();
        result.push_str(&format!("  Vertices: {}\n", self.vertices.len()));
        result.push_str(&format!("  Weighted Edges: {total_edges}\n"));
        result.push_str("  Connections:\n");
        for (i, vertex) in self.vertices.iter().enumerate() {
            if !self.edges[i].is_empty() {
                result.push_str(&format!("    {vertex} -> ["));
                let edge_strs: Vec<String> = self.edges[i]
                    .iter()
                    .map(|(to_idx, weight)| format!("{}(w:{})", self.vertices[*to_idx], weight))
                    .collect();
                result.push_str(&edge_strs.join(", "));
                result.push_str("]\n");
            } else {
                result.push_str(&format!("    {vertex} -> []\n"));
            }
        }
        result
    }
    /// Compare two weighted graphs for structural equality
    pub fn structural_equals(&self, other: &Self) -> bool
    where
        T: PartialEq,
        W: PartialEq + Eq + Hash,
    {
        if self.vertices.len() != other.vertices.len() {
            return false;
        }
        if self.edges.len() != other.edges.len() {
            return false;
        }
        for vertex in &self.vertices {
            if !other.vertices.contains(vertex) {
                return false;
            }
        }
        for (vertex, edges) in self.vertices.iter().zip(self.edges.iter()) {
            if let Some(&other_idx) = other.vertex_map.get(vertex) {
                let other_edges = &other.edges[other_idx];
                if edges.len() != other_edges.len() {
                    return false;
                }
                let self_edge_set: HashSet<_> = edges
                    .iter()
                    .map(|(idx, weight)| (&self.vertices[*idx], weight))
                    .collect();
                let other_edge_set: HashSet<_> = other_edges
                    .iter()
                    .map(|(idx, weight)| (&other.vertices[*idx], weight))
                    .collect();
                if self_edge_set != other_edge_set {
                    return false;
                }
            } else {
                return false;
            }
        }
        true
    }
}
/// Trie statistics for analysis
#[derive(Debug, Default, Clone)]
pub struct TrieStatistics {
    pub node_count: usize,
    pub leaf_count: usize,
    pub internal_count: usize,
    pub word_count: usize,
    pub max_depth: usize,
    pub branch_factor: usize,
}
/// Graph representation using adjacency lists
#[derive(Clone, Debug)]
pub struct Graph<T> {
    pub(crate) vertices: Vec<T>,
    pub(crate) adjacency: Vec<Vec<usize>>,
    pub(crate) vertex_map: HashMap<T, usize>,
}
impl<T: Clone + Eq + Hash> Graph<T> {
    /// Create a new empty graph
    pub fn new() -> Self {
        Self {
            vertices: Vec::new(),
            adjacency: Vec::new(),
            vertex_map: HashMap::new(),
        }
    }
    /// Add a vertex to the graph
    pub fn add_vertex(&mut self, vertex: T) -> usize {
        if let Some(&idx) = self.vertex_map.get(&vertex) {
            return idx;
        }
        let idx = self.vertices.len();
        self.vertices.push(vertex.clone());
        self.adjacency.push(Vec::new());
        self.vertex_map.insert(vertex, idx);
        idx
    }
    /// Add an edge between two vertices
    pub fn add_edge(&mut self, from: &T, to: &T) -> UtilsResult<()> {
        let from_idx = self
            .vertex_map
            .get(from)
            .ok_or_else(|| UtilsError::InvalidParameter("From vertex not found".to_string()))?;
        let to_idx = self
            .vertex_map
            .get(to)
            .ok_or_else(|| UtilsError::InvalidParameter("To vertex not found".to_string()))?;
        self.adjacency[*from_idx].push(*to_idx);
        Ok(())
    }
    /// Add an undirected edge between two vertices
    pub fn add_undirected_edge(&mut self, v1: &T, v2: &T) -> UtilsResult<()> {
        self.add_edge(v1, v2)?;
        self.add_edge(v2, v1)?;
        Ok(())
    }
    /// Get neighbors of a vertex
    pub fn neighbors(&self, vertex: &T) -> UtilsResult<Vec<&T>> {
        let idx = self
            .vertex_map
            .get(vertex)
            .ok_or_else(|| UtilsError::InvalidParameter("Vertex not found".to_string()))?;
        Ok(self.adjacency[*idx]
            .iter()
            .map(|&i| &self.vertices[i])
            .collect())
    }
    /// Get number of vertices
    pub fn num_vertices(&self) -> usize {
        self.vertices.len()
    }
    /// Get number of edges
    pub fn num_edges(&self) -> usize {
        self.adjacency.iter().map(|adj| adj.len()).sum()
    }
    /// Breadth-first search from a starting vertex
    pub fn bfs(&self, start: &T) -> UtilsResult<Vec<&T>> {
        let start_idx = self
            .vertex_map
            .get(start)
            .ok_or_else(|| UtilsError::InvalidParameter("Start vertex not found".to_string()))?;
        let mut visited = vec![false; self.vertices.len()];
        let mut queue = VecDeque::new();
        let mut result = Vec::new();
        queue.push_back(*start_idx);
        visited[*start_idx] = true;
        while let Some(current) = queue.pop_front() {
            result.push(&self.vertices[current]);
            for &neighbor in &self.adjacency[current] {
                if !visited[neighbor] {
                    visited[neighbor] = true;
                    queue.push_back(neighbor);
                }
            }
        }
        Ok(result)
    }
    /// Depth-first search from a starting vertex
    pub fn dfs(&self, start: &T) -> UtilsResult<Vec<&T>> {
        let start_idx = self
            .vertex_map
            .get(start)
            .ok_or_else(|| UtilsError::InvalidParameter("Start vertex not found".to_string()))?;
        let mut visited = vec![false; self.vertices.len()];
        let mut result = Vec::new();
        self.dfs_recursive(*start_idx, &mut visited, &mut result);
        Ok(result)
    }
    fn dfs_recursive<'a>(
        &'a self,
        current: usize,
        visited: &mut Vec<bool>,
        result: &mut Vec<&'a T>,
    ) {
        visited[current] = true;
        result.push(&self.vertices[current]);
        for &neighbor in &self.adjacency[current] {
            if !visited[neighbor] {
                self.dfs_recursive(neighbor, visited, result);
            }
        }
    }
    /// Check if the graph has a cycle (for directed graphs)
    pub fn has_cycle(&self) -> bool {
        let mut visited = vec![false; self.vertices.len()];
        let mut rec_stack = vec![false; self.vertices.len()];
        for i in 0..self.vertices.len() {
            if !visited[i] && self.has_cycle_util(i, &mut visited, &mut rec_stack) {
                return true;
            }
        }
        false
    }
    fn has_cycle_util(
        &self,
        current: usize,
        visited: &mut Vec<bool>,
        rec_stack: &mut Vec<bool>,
    ) -> bool {
        visited[current] = true;
        rec_stack[current] = true;
        for &neighbor in &self.adjacency[current] {
            if (!visited[neighbor] && self.has_cycle_util(neighbor, visited, rec_stack))
                || rec_stack[neighbor]
            {
                return true;
            }
        }
        rec_stack[current] = false;
        false
    }
    /// Convert to adjacency matrix
    pub fn to_adjacency_matrix(&self) -> Array2<u8> {
        let n = self.vertices.len();
        let mut matrix = Array2::zeros((n, n));
        for (i, neighbors) in self.adjacency.iter().enumerate() {
            for &j in neighbors {
                matrix[[i, j]] = 1;
            }
        }
        matrix
    }
    /// Get vertices as a reference
    pub fn vertices(&self) -> &[T] {
        &self.vertices
    }
    /// Get adjacency list as a reference
    pub fn adjacency(&self) -> &[Vec<usize>] {
        &self.adjacency
    }
    /// Serialize the graph to a string representation
    pub fn serialize(&self) -> String
    where
        T: fmt::Display,
    {
        let mut result = String::new();
        result.push_str("Graph {\n");
        result.push_str(&format!("  vertices: {} nodes\n", self.vertices.len()));
        result.push_str(&format!("  edges: {} connections\n", self.num_edges()));
        result.push_str("  adjacency_list: {\n");
        for (i, vertex) in self.vertices.iter().enumerate() {
            result.push_str(&format!("    {vertex}: ["));
            let neighbors: Vec<String> = self.adjacency[i]
                .iter()
                .map(|&idx| self.vertices[idx].to_string())
                .collect();
            result.push_str(&neighbors.join(", "));
            result.push_str("]\n");
        }
        result.push_str("  }\n");
        result.push_str("}\n");
        result
    }
    /// Visualize the graph structure
    pub fn visualize(&self) -> String
    where
        T: fmt::Display,
    {
        let mut result = String::from("Graph Visualization:\n");
        if self.vertices.is_empty() {
            result.push_str("  (empty graph)\n");
            return result;
        }
        result.push_str(&format!("  Vertices: {}\n", self.vertices.len()));
        result.push_str(&format!("  Edges: {}\n", self.num_edges()));
        result.push_str(&format!("  Has cycle: {}\n", self.has_cycle()));
        result.push_str("  Connections:\n");
        for vertex in &self.vertices {
            if let Ok(neighbors) = self.neighbors(vertex) {
                result.push_str(&format!("    {vertex} -> ["));
                let neighbor_strs: Vec<String> = neighbors.iter().map(|n| n.to_string()).collect();
                result.push_str(&neighbor_strs.join(", "));
                result.push_str("]\n");
            }
        }
        result
    }
    /// Compare two graphs for structural equality
    pub fn structural_equals(&self, other: &Self) -> bool
    where
        T: PartialEq,
    {
        if self.vertices.len() != other.vertices.len() {
            return false;
        }
        if self.adjacency.len() != other.adjacency.len() {
            return false;
        }
        for vertex in &self.vertices {
            if !other.vertices.contains(vertex) {
                return false;
            }
        }
        for (vertex, neighbors) in self.vertices.iter().zip(self.adjacency.iter()) {
            if let Some(&other_idx) = other.vertex_map.get(vertex) {
                let other_neighbors = &other.adjacency[other_idx];
                if neighbors.len() != other_neighbors.len() {
                    return false;
                }
                let self_neighbor_vertices: HashSet<_> =
                    neighbors.iter().map(|&idx| &self.vertices[idx]).collect();
                let other_neighbor_vertices: HashSet<_> = other_neighbors
                    .iter()
                    .map(|&idx| &other.vertices[idx])
                    .collect();
                if self_neighbor_vertices != other_neighbor_vertices {
                    return false;
                }
            } else {
                return false;
            }
        }
        true
    }
}
/// Thread-safe ring buffer
#[derive(Clone, Debug)]
pub struct ConcurrentRingBuffer<T> {
    pub(crate) inner: Arc<Mutex<RingBuffer<T>>>,
}
impl<T: Clone> ConcurrentRingBuffer<T> {
    /// Create a new concurrent ring buffer
    pub fn new(capacity: usize) -> Self {
        Self {
            inner: Arc::new(Mutex::new(RingBuffer::new(capacity))),
        }
    }
    /// Push an element to the buffer
    pub fn push(&self, item: T) -> UtilsResult<Option<T>> {
        let mut buffer = self
            .inner
            .lock()
            .map_err(|_| UtilsError::InvalidParameter("Lock poisoned".to_string()))?;
        Ok(buffer.push(item))
    }
    /// Pop the oldest element from the buffer
    pub fn pop(&self) -> UtilsResult<Option<T>> {
        let mut buffer = self
            .inner
            .lock()
            .map_err(|_| UtilsError::InvalidParameter("Lock poisoned".to_string()))?;
        Ok(buffer.pop())
    }
    /// Get the current size of the buffer
    pub fn len(&self) -> UtilsResult<usize> {
        let buffer = self
            .inner
            .lock()
            .map_err(|_| UtilsError::InvalidParameter("Lock poisoned".to_string()))?;
        Ok(buffer.len())
    }
    /// Check if the buffer is empty
    pub fn is_empty(&self) -> UtilsResult<bool> {
        let buffer = self
            .inner
            .lock()
            .map_err(|_| UtilsError::InvalidParameter("Lock poisoned".to_string()))?;
        Ok(buffer.is_empty())
    }
    /// Check if the buffer is full
    pub fn is_full(&self) -> UtilsResult<bool> {
        let buffer = self
            .inner
            .lock()
            .map_err(|_| UtilsError::InvalidParameter("Lock poisoned".to_string()))?;
        Ok(buffer.is_full())
    }
}
/// Binary Search Tree
#[derive(Clone, Debug)]
pub struct BinarySearchTree<T> {
    pub(crate) root: Option<Box<TreeNode<T>>>,
}
impl<T: Clone + PartialOrd> BinarySearchTree<T> {
    /// Create a new empty BST
    pub fn new() -> Self {
        Self { root: None }
    }
    /// Insert a value into the BST
    pub fn insert(&mut self, value: T) {
        self.root = Self::insert_recursive(self.root.take(), value);
    }
    fn insert_recursive(node: Option<Box<TreeNode<T>>>, value: T) -> Option<Box<TreeNode<T>>> {
        match node {
            None => Some(Box::new(TreeNode::new(value))),
            Some(mut node) => {
                if value <= node.value {
                    node.left = Self::insert_recursive(node.left.take(), value);
                } else {
                    node.right = Self::insert_recursive(node.right.take(), value);
                }
                Some(node)
            }
        }
    }
    /// Search for a value in the BST
    pub fn search(&self, value: &T) -> bool {
        Self::search_recursive(&self.root, value)
    }
    fn search_recursive(node: &Option<Box<TreeNode<T>>>, value: &T) -> bool {
        match node {
            None => false,
            Some(node) => {
                if *value == node.value {
                    true
                } else if *value < node.value {
                    Self::search_recursive(&node.left, value)
                } else {
                    Self::search_recursive(&node.right, value)
                }
            }
        }
    }
    /// In-order traversal of the BST
    pub fn inorder(&self) -> Vec<&T> {
        let mut result = Vec::new();
        Self::inorder_recursive(&self.root, &mut result);
        result
    }
    fn inorder_recursive<'a>(node: &'a Option<Box<TreeNode<T>>>, result: &mut Vec<&'a T>) {
        if let Some(node) = node {
            Self::inorder_recursive(&node.left, result);
            result.push(&node.value);
            Self::inorder_recursive(&node.right, result);
        }
    }
    /// Pre-order traversal of the BST
    pub fn preorder(&self) -> Vec<&T> {
        let mut result = Vec::new();
        Self::preorder_recursive(&self.root, &mut result);
        result
    }
    fn preorder_recursive<'a>(node: &'a Option<Box<TreeNode<T>>>, result: &mut Vec<&'a T>) {
        if let Some(node) = node {
            result.push(&node.value);
            Self::preorder_recursive(&node.left, result);
            Self::preorder_recursive(&node.right, result);
        }
    }
    /// Post-order traversal of the BST
    pub fn postorder(&self) -> Vec<&T> {
        let mut result = Vec::new();
        Self::postorder_recursive(&self.root, &mut result);
        result
    }
    fn postorder_recursive<'a>(node: &'a Option<Box<TreeNode<T>>>, result: &mut Vec<&'a T>) {
        if let Some(node) = node {
            Self::postorder_recursive(&node.left, result);
            Self::postorder_recursive(&node.right, result);
            result.push(&node.value);
        }
    }
    /// Get the height of the tree
    pub fn height(&self) -> usize {
        Self::height_recursive(&self.root)
    }
    fn height_recursive(node: &Option<Box<TreeNode<T>>>) -> usize {
        match node {
            None => 0,
            Some(node) => {
                1 + std::cmp::max(
                    Self::height_recursive(&node.left),
                    Self::height_recursive(&node.right),
                )
            }
        }
    }
    /// Get a reference to the root node
    pub fn root(&self) -> &Option<Box<TreeNode<T>>> {
        &self.root
    }
    /// Serialize the tree to a string representation
    pub fn serialize(&self) -> String
    where
        T: fmt::Display,
    {
        Self::serialize_node(&self.root, 0)
    }
    fn serialize_node(node: &Option<Box<TreeNode<T>>>, depth: usize) -> String
    where
        T: fmt::Display,
    {
        match node {
            None => "null".to_string(),
            Some(node) => {
                let indent = "  ".repeat(depth);
                let mut result = format!("{}node: {}\n", indent, node.value);
                if node.left.is_some() || node.right.is_some() {
                    result.push_str(&format!("{indent}left:\n"));
                    result.push_str(&Self::serialize_node(&node.left, depth + 1));
                    result.push_str(&format!("{indent}right:\n"));
                    result.push_str(&Self::serialize_node(&node.right, depth + 1));
                }
                result
            }
        }
    }
    /// Visualize the tree structure
    pub fn visualize(&self) -> String
    where
        T: fmt::Display,
    {
        if self.root.is_none() {
            return "Empty tree".to_string();
        }
        Self::visualize_node(&self.root, "", true)
    }
    fn visualize_node(node: &Option<Box<TreeNode<T>>>, prefix: &str, is_last: bool) -> String
    where
        T: fmt::Display,
    {
        match node {
            None => String::new(),
            Some(node) => {
                let mut result = String::new();
                let connector = if is_last { "└── " } else { "├── " };
                result.push_str(&format!("{}{}{}\n", prefix, connector, node.value));
                let extension = if is_last { "    " } else { "│   " };
                let new_prefix = format!("{prefix}{extension}");
                if node.left.is_some() || node.right.is_some() {
                    if node.right.is_some() {
                        result.push_str(&Self::visualize_node(
                            &node.right,
                            &new_prefix,
                            node.left.is_none(),
                        ));
                    }
                    if node.left.is_some() {
                        result.push_str(&Self::visualize_node(&node.left, &new_prefix, true));
                    }
                }
                result
            }
        }
    }
    /// Compare two trees for structural equality
    pub fn structural_equals(&self, other: &Self) -> bool
    where
        T: PartialEq,
    {
        Self::nodes_equal(&self.root, &other.root)
    }
    fn nodes_equal(node1: &Option<Box<TreeNode<T>>>, node2: &Option<Box<TreeNode<T>>>) -> bool
    where
        T: PartialEq,
    {
        match (node1, node2) {
            (None, None) => true,
            (Some(n1), Some(n2)) => {
                n1.value == n2.value
                    && Self::nodes_equal(&n1.left, &n2.left)
                    && Self::nodes_equal(&n1.right, &n2.right)
            }
            _ => false,
        }
    }
    /// Compare tree structures (ignoring values)
    pub fn same_structure(&self, other: &Self) -> bool {
        Self::same_structure_nodes(&self.root, &other.root)
    }
    fn same_structure_nodes(
        node1: &Option<Box<TreeNode<T>>>,
        node2: &Option<Box<TreeNode<T>>>,
    ) -> bool {
        match (node1, node2) {
            (None, None) => true,
            (Some(n1), Some(n2)) => {
                Self::same_structure_nodes(&n1.left, &n2.left)
                    && Self::same_structure_nodes(&n1.right, &n2.right)
            }
            _ => false,
        }
    }
    /// Get tree statistics
    pub fn statistics(&self) -> TreeStatistics {
        let mut stats = TreeStatistics::default();
        Self::collect_statistics(&self.root, &mut stats, 0);
        stats
    }
    fn collect_statistics(
        node: &Option<Box<TreeNode<T>>>,
        stats: &mut TreeStatistics,
        depth: usize,
    ) {
        match node {
            None => {
                stats.max_depth = stats.max_depth.max(depth);
            }
            Some(node) => {
                stats.node_count += 1;
                stats.max_depth = stats.max_depth.max(depth);
                let has_left = node.left.is_some();
                let has_right = node.right.is_some();
                match (has_left, has_right) {
                    (false, false) => stats.leaf_count += 1,
                    (true, false) | (false, true) => stats.internal_count += 1,
                    (true, true) => stats.internal_count += 1,
                }
                Self::collect_statistics(&node.left, stats, depth + 1);
                Self::collect_statistics(&node.right, stats, depth + 1);
            }
        }
    }
    /// Check if the tree is balanced (AVL property)
    pub fn is_balanced(&self) -> bool {
        Self::check_balance(&self.root).is_some()
    }
    fn check_balance(node: &Option<Box<TreeNode<T>>>) -> Option<usize> {
        match node {
            None => Some(0),
            Some(node) => {
                let left_height = Self::check_balance(&node.left)?;
                let right_height = Self::check_balance(&node.right)?;
                if left_height.abs_diff(right_height) <= 1 {
                    Some(1 + left_height.max(right_height))
                } else {
                    None
                }
            }
        }
    }
}
/// Atomic counter for thread-safe counting operations
#[derive(Clone, Debug)]
pub struct AtomicCounter {
    pub(crate) value: Arc<AtomicUsize>,
}
impl AtomicCounter {
    /// Create a new atomic counter
    pub fn new(initial: usize) -> Self {
        Self {
            value: Arc::new(AtomicUsize::new(initial)),
        }
    }
    /// Increment the counter and return the new value
    pub fn increment(&self) -> usize {
        self.value.fetch_add(1, Ordering::SeqCst) + 1
    }
    /// Decrement the counter and return the new value
    pub fn decrement(&self) -> usize {
        self.value.fetch_sub(1, Ordering::SeqCst).saturating_sub(1)
    }
    /// Add to the counter and return the new value
    pub fn add(&self, val: usize) -> usize {
        self.value.fetch_add(val, Ordering::SeqCst) + val
    }
    /// Subtract from the counter and return the new value
    pub fn sub(&self, val: usize) -> usize {
        self.value
            .fetch_sub(val, Ordering::SeqCst)
            .saturating_sub(val)
    }
    /// Get the current value
    pub fn get(&self) -> usize {
        self.value.load(Ordering::SeqCst)
    }
    /// Set the value
    pub fn set(&self, val: usize) {
        self.value.store(val, Ordering::SeqCst);
    }
    /// Compare and swap operation
    pub fn compare_and_swap(&self, current: usize, new: usize) -> usize {
        match self
            .value
            .compare_exchange(current, new, Ordering::SeqCst, Ordering::SeqCst)
        {
            Ok(prev) => prev,
            Err(prev) => prev,
        }
    }
}
/// Tree statistics for analysis
#[derive(Debug, Default, Clone)]
pub struct TreeStatistics {
    pub node_count: usize,
    pub leaf_count: usize,
    pub internal_count: usize,
    pub max_depth: usize,
}
/// Trie (Prefix Tree) for string storage and retrieval
#[derive(Clone, Debug)]
pub struct Trie {
    pub(crate) children: HashMap<char, Trie>,
    pub(crate) is_end_of_word: bool,
}
impl Trie {
    /// Create a new empty trie
    pub fn new() -> Self {
        Self {
            children: HashMap::new(),
            is_end_of_word: false,
        }
    }
    /// Insert a word into the trie
    pub fn insert(&mut self, word: &str) {
        let mut current = self;
        for ch in word.chars() {
            current = current.children.entry(ch).or_default();
        }
        current.is_end_of_word = true;
    }
    /// Search for a word in the trie
    pub fn search(&self, word: &str) -> bool {
        let mut current = self;
        for ch in word.chars() {
            match current.children.get(&ch) {
                Some(node) => current = node,
                None => return false,
            }
        }
        current.is_end_of_word
    }
    /// Check if any word starts with the given prefix
    pub fn starts_with(&self, prefix: &str) -> bool {
        let mut current = self;
        for ch in prefix.chars() {
            match current.children.get(&ch) {
                Some(node) => current = node,
                None => return false,
            }
        }
        true
    }
    /// Get all words with the given prefix
    pub fn words_with_prefix(&self, prefix: &str) -> Vec<String> {
        let mut current = self;
        for ch in prefix.chars() {
            match current.children.get(&ch) {
                Some(node) => current = node,
                None => return Vec::new(),
            }
        }
        let mut words = Vec::new();
        current.collect_words(prefix.to_string(), &mut words);
        words
    }
    fn collect_words(&self, prefix: String, words: &mut Vec<String>) {
        if self.is_end_of_word {
            words.push(prefix.clone());
        }
        for (&ch, child) in &self.children {
            let mut new_prefix = prefix.clone();
            new_prefix.push(ch);
            child.collect_words(new_prefix, words);
        }
    }
    /// Get all words stored in the trie
    pub fn all_words(&self) -> Vec<String> {
        let mut words = Vec::new();
        self.collect_words(String::new(), &mut words);
        words
    }
    /// Get the number of words stored in the trie
    pub fn word_count(&self) -> usize {
        let mut count = 0;
        if self.is_end_of_word {
            count += 1;
        }
        for child in self.children.values() {
            count += child.word_count();
        }
        count
    }
    /// Serialize the trie to a string representation
    pub fn serialize(&self) -> String {
        let mut result = String::new();
        self.serialize_node("".to_string(), &mut result, 0);
        result
    }
    fn serialize_node(&self, prefix: String, result: &mut String, depth: usize) {
        let indent = "  ".repeat(depth);
        if self.is_end_of_word {
            result.push_str(&format!("{indent}word: {prefix}\n"));
        }
        for (&ch, child) in &self.children {
            let mut new_prefix = prefix.clone();
            new_prefix.push(ch);
            result.push_str(&format!("{indent}char: {ch} -> \n"));
            child.serialize_node(new_prefix, result, depth + 1);
        }
    }
    /// Visualize the trie structure
    pub fn visualize(&self) -> String {
        let mut result = String::from("Trie\n");
        self.visualize_node("", "", true, &mut result);
        result
    }
    fn visualize_node(&self, prefix: &str, char_prefix: &str, is_last: bool, result: &mut String) {
        let connector = if is_last { "└── " } else { "├── " };
        if self.is_end_of_word {
            result.push_str(&format!("{char_prefix}{connector}[{prefix}] ✓\n"));
        } else if !prefix.is_empty() {
            result.push_str(&format!("{char_prefix}{connector}[{prefix}]\n"));
        }
        let extension = if is_last { "    " } else { "│   " };
        let new_char_prefix = format!("{char_prefix}{extension}");
        let children: Vec<_> = self.children.iter().collect();
        for (i, (&ch, child)) in children.iter().enumerate() {
            let is_last_child = i == children.len() - 1;
            let mut new_prefix = prefix.to_string();
            new_prefix.push(ch);
            child.visualize_node(&new_prefix, &new_char_prefix, is_last_child, result);
        }
    }
    /// Compare two tries for structural equality
    pub fn structural_equals(&self, other: &Self) -> bool {
        if self.is_end_of_word != other.is_end_of_word {
            return false;
        }
        if self.children.len() != other.children.len() {
            return false;
        }
        for (&ch, child) in &self.children {
            match other.children.get(&ch) {
                Some(other_child) => {
                    if !child.structural_equals(other_child) {
                        return false;
                    }
                }
                None => return false,
            }
        }
        true
    }
    /// Check if this trie contains all words from another trie
    pub fn contains_trie(&self, other: &Self) -> bool {
        for word in other.all_words() {
            if !self.search(&word) {
                return false;
            }
        }
        true
    }
    /// Get trie statistics
    pub fn statistics(&self) -> TrieStatistics {
        let mut stats = TrieStatistics::default();
        self.collect_trie_statistics(&mut stats, 0);
        stats
    }
    fn collect_trie_statistics(&self, stats: &mut TrieStatistics, depth: usize) {
        stats.node_count += 1;
        stats.max_depth = stats.max_depth.max(depth);
        if self.is_end_of_word {
            stats.word_count += 1;
        }
        if self.children.is_empty() {
            stats.leaf_count += 1;
        } else {
            stats.internal_count += 1;
            stats.branch_factor = stats.branch_factor.max(self.children.len());
        }
        for child in self.children.values() {
            child.collect_trie_statistics(stats, depth + 1);
        }
    }
    /// Remove a word from the trie
    pub fn remove(&mut self, word: &str) -> bool {
        let chars: Vec<char> = word.chars().collect();
        self.remove_recursive(&chars, 0).0
    }
    fn remove_recursive(&mut self, chars: &[char], index: usize) -> (bool, bool) {
        if index == chars.len() {
            if self.is_end_of_word {
                self.is_end_of_word = false;
                return (true, self.children.is_empty());
            }
            return (false, false);
        }
        let ch = chars[index];
        if let Some(child) = self.children.get_mut(&ch) {
            let (word_existed, should_remove_child) = child.remove_recursive(chars, index + 1);
            if should_remove_child {
                self.children.remove(&ch);
            }
            let can_remove_self = !self.is_end_of_word && self.children.is_empty();
            return (word_existed, can_remove_self);
        }
        (false, false)
    }
    /// Get the longest common prefix of all words in the trie
    pub fn longest_common_prefix(&self) -> String {
        let mut prefix = String::new();
        let mut current = self;
        while current.children.len() == 1 && !current.is_end_of_word {
            let (&ch, child) = current
                .children
                .iter()
                .next()
                .expect("operation should succeed");
            prefix.push(ch);
            current = child;
        }
        prefix
    }
}
/// Cache-friendly matrix storage with block layout
#[derive(Clone, Debug)]
pub struct BlockMatrix<T> {
    pub(crate) data: Vec<T>,
    pub(crate) rows: usize,
    pub(crate) cols: usize,
    pub(crate) block_size: usize,
}
impl<T: Clone + Zero> BlockMatrix<T> {
    /// Create a new block matrix with specified block size
    pub fn new(rows: usize, cols: usize, block_size: usize) -> Self {
        let total_size = rows * cols;
        Self {
            data: vec![T::zero(); total_size],
            rows,
            cols,
            block_size,
        }
    }
    /// Get element at (row, col)
    pub fn get(&self, row: usize, col: usize) -> UtilsResult<&T> {
        if row >= self.rows || col >= self.cols {
            return Err(UtilsError::InvalidParameter(format!(
                "Index ({}, {}) out of bounds for {}x{} matrix",
                row, col, self.rows, self.cols
            )));
        }
        let index = self.block_index(row, col);
        Ok(&self.data[index])
    }
    /// Set element at (row, col)
    pub fn set(&mut self, row: usize, col: usize, value: T) -> UtilsResult<()> {
        if row >= self.rows || col >= self.cols {
            return Err(UtilsError::InvalidParameter(format!(
                "Index ({}, {}) out of bounds for {}x{} matrix",
                row, col, self.rows, self.cols
            )));
        }
        let index = self.block_index(row, col);
        self.data[index] = value;
        Ok(())
    }
    fn block_index(&self, row: usize, col: usize) -> usize {
        let block_row = row / self.block_size;
        let block_col = col / self.block_size;
        let in_block_row = row % self.block_size;
        let in_block_col = col % self.block_size;
        let blocks_per_row = self.cols.div_ceil(self.block_size);
        let block_index = block_row * blocks_per_row + block_col;
        let block_start = block_index * self.block_size * self.block_size;
        block_start + in_block_row * self.block_size + in_block_col
    }
    /// Get dimensions
    pub fn dim(&self) -> (usize, usize) {
        (self.rows, self.cols)
    }
}
/// Thread-safe work queue for task distribution
#[derive(Clone, Debug)]
pub struct WorkQueue<T> {
    pub(crate) queue: Arc<Mutex<VecDeque<T>>>,
    pub(crate) active_workers: Arc<AtomicUsize>,
}
impl<T: Clone> WorkQueue<T> {
    /// Create a new work queue
    pub fn new() -> Self {
        Self {
            queue: Arc::new(Mutex::new(VecDeque::new())),
            active_workers: Arc::new(AtomicUsize::new(0)),
        }
    }
    /// Add work to the queue
    pub fn add_work(&self, work: T) -> UtilsResult<()> {
        let mut queue = self
            .queue
            .lock()
            .map_err(|_| UtilsError::InvalidParameter("Lock poisoned".to_string()))?;
        queue.push_back(work);
        Ok(())
    }
    /// Get work from the queue
    pub fn get_work(&self) -> UtilsResult<Option<T>> {
        let mut queue = self
            .queue
            .lock()
            .map_err(|_| UtilsError::InvalidParameter("Lock poisoned".to_string()))?;
        Ok(queue.pop_front())
    }
    /// Register a worker as active
    pub fn register_worker(&self) {
        self.active_workers.fetch_add(1, Ordering::SeqCst);
    }
    /// Unregister a worker
    pub fn unregister_worker(&self) {
        self.active_workers.fetch_sub(1, Ordering::SeqCst);
    }
    /// Get number of active workers
    pub fn active_worker_count(&self) -> usize {
        self.active_workers.load(Ordering::SeqCst)
    }
    /// Check if there's work available
    pub fn has_work(&self) -> UtilsResult<bool> {
        let queue = self
            .queue
            .lock()
            .map_err(|_| UtilsError::InvalidParameter("Lock poisoned".to_string()))?;
        Ok(!queue.is_empty())
    }
    /// Get the current queue size
    pub fn queue_size(&self) -> UtilsResult<usize> {
        let queue = self
            .queue
            .lock()
            .map_err(|_| UtilsError::InvalidParameter("Lock poisoned".to_string()))?;
        Ok(queue.len())
    }
}
/// Ring buffer for efficient circular storage
#[derive(Clone, Debug)]
pub struct RingBuffer<T> {
    pub(crate) data: Vec<Option<T>>,
    pub(crate) capacity: usize,
    pub(crate) head: usize,
    pub(crate) tail: usize,
    pub(crate) size: usize,
}
impl<T: Clone> RingBuffer<T> {
    /// Create a new ring buffer with given capacity
    pub fn new(capacity: usize) -> Self {
        Self {
            data: vec![None; capacity],
            capacity,
            head: 0,
            tail: 0,
            size: 0,
        }
    }
    /// Push an element to the buffer
    pub fn push(&mut self, item: T) -> Option<T> {
        let old_item = if self.size == self.capacity {
            self.data[self.tail].take()
        } else {
            None
        };
        self.data[self.tail] = Some(item);
        self.tail = (self.tail + 1) % self.capacity;
        if self.size == self.capacity {
            self.head = (self.head + 1) % self.capacity;
        } else {
            self.size += 1;
        }
        old_item
    }
    /// Pop the oldest element from the buffer
    pub fn pop(&mut self) -> Option<T> {
        if self.size == 0 {
            return None;
        }
        let item = self.data[self.head].take();
        self.head = (self.head + 1) % self.capacity;
        self.size -= 1;
        item
    }
    /// Get the current size of the buffer
    pub fn len(&self) -> usize {
        self.size
    }
    /// Check if the buffer is empty
    pub fn is_empty(&self) -> bool {
        self.size == 0
    }
    /// Check if the buffer is full
    pub fn is_full(&self) -> bool {
        self.size == self.capacity
    }
    /// Get iterator over elements in insertion order
    pub fn iter(&self) -> RingBufferIter<'_, T> {
        RingBufferIter {
            buffer: self,
            index: 0,
        }
    }
}