aprender-present-terminal 0.29.0

Terminal backend for Presentar UI framework with zero-allocation rendering
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
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
// ============================================================================
// CACHE & LOAD BALANCING HELPERS (trueno-viz parity)
// ============================================================================

/// O(1) cache statistics tracker.
///
/// Tracks cache hits, misses, evictions, and calculates hit rate.
#[derive(Debug, Clone)]
pub struct CacheStats {
    /// Total hits
    hits: u64,
    /// Total misses
    misses: u64,
    /// Total evictions
    evictions: u64,
    /// Total insertions
    insertions: u64,
    /// Bytes in cache
    bytes_cached: u64,
    /// Capacity in bytes
    capacity_bytes: u64,
}

impl Default for CacheStats {
    fn default() -> Self {
        Self::new(0)
    }
}

impl CacheStats {
    /// Create with capacity in bytes
    #[must_use]
    pub fn new(capacity_bytes: u64) -> Self {
        Self {
            hits: 0,
            misses: 0,
            evictions: 0,
            insertions: 0,
            bytes_cached: 0,
            capacity_bytes,
        }
    }

    /// Create for L1 cache (32KB typical)
    #[must_use]
    pub fn for_l1_cache() -> Self {
        Self::new(32 * 1024)
    }

    /// Create for L2 cache (256KB typical)
    #[must_use]
    pub fn for_l2_cache() -> Self {
        Self::new(256 * 1024)
    }

    /// Create for application cache (16MB)
    #[must_use]
    pub fn for_app_cache() -> Self {
        Self::new(16 * 1024 * 1024)
    }

    /// Record a cache hit
    pub fn hit(&mut self) {
        self.hits += 1;
    }

    /// Record a cache miss
    pub fn miss(&mut self) {
        self.misses += 1;
    }

    /// Record an eviction
    pub fn evict(&mut self, bytes: u64) {
        self.evictions += 1;
        self.bytes_cached = self.bytes_cached.saturating_sub(bytes);
    }

    /// Record an insertion
    pub fn insert(&mut self, bytes: u64) {
        self.insertions += 1;
        self.bytes_cached += bytes;
    }

    /// Get hit rate as percentage
    #[must_use]
    pub fn hit_rate(&self) -> f64 {
        let total = self.hits + self.misses;
        if total == 0 {
            0.0
        } else {
            (self.hits as f64 / total as f64) * 100.0
        }
    }

    /// Get miss rate as percentage
    #[must_use]
    pub fn miss_rate(&self) -> f64 {
        100.0 - self.hit_rate()
    }

    /// Get eviction rate (evictions per insertion)
    #[must_use]
    pub fn eviction_rate(&self) -> f64 {
        if self.insertions == 0 {
            0.0
        } else {
            self.evictions as f64 / self.insertions as f64
        }
    }

    /// Get fill percentage
    #[must_use]
    pub fn fill_percentage(&self) -> f64 {
        if self.capacity_bytes == 0 {
            0.0
        } else {
            (self.bytes_cached as f64 / self.capacity_bytes as f64) * 100.0
        }
    }

    /// Get total requests
    #[must_use]
    pub fn total_requests(&self) -> u64 {
        self.hits + self.misses
    }

    /// Check if cache is effective (hit rate > threshold)
    #[must_use]
    pub fn is_effective(&self, threshold: f64) -> bool {
        self.hit_rate() >= threshold
    }

    /// Reset all counters
    pub fn reset(&mut self) {
        self.hits = 0;
        self.misses = 0;
        self.evictions = 0;
        self.insertions = 0;
        self.bytes_cached = 0;
    }
}

/// O(1) Bloom filter for probabilistic membership testing.
///
/// Fixed-size bloom filter with configurable hash count.
/// False positives possible, false negatives impossible.
#[derive(Debug, Clone)]
pub struct BloomFilter {
    /// Bit array (using u64 words)
    bits: [u64; 16], // 1024 bits
    /// Number of hash functions
    hash_count: u32,
    /// Items added
    items: u64,
}

impl Default for BloomFilter {
    fn default() -> Self {
        Self::new(3)
    }
}

impl BloomFilter {
    /// Create with number of hash functions
    #[must_use]
    pub fn new(hash_count: u32) -> Self {
        Self {
            bits: [0; 16],
            hash_count: hash_count.clamp(1, 10),
            items: 0,
        }
    }

    /// Create optimized for ~100 items (3 hashes)
    #[must_use]
    pub fn for_small() -> Self {
        Self::new(3)
    }

    /// Create optimized for ~500 items (5 hashes)
    #[must_use]
    pub fn for_medium() -> Self {
        Self::new(5)
    }

    /// Simple hash function (FNV-1a style)
    fn hash(&self, value: u64, seed: u32) -> usize {
        let mut h = value.wrapping_mul(0x517cc1b727220a95);
        h = h.wrapping_add(seed as u64);
        h ^= h >> 33;
        h = h.wrapping_mul(0xff51afd7ed558ccd);
        (h as usize) % 1024
    }

    /// Add item to filter
    pub fn add(&mut self, value: u64) {
        for i in 0..self.hash_count {
            let bit_idx = self.hash(value, i);
            let word_idx = bit_idx / 64;
            let bit_pos = bit_idx % 64;
            self.bits[word_idx] |= 1 << bit_pos;
        }
        self.items += 1;
    }

    /// Check if item might be in filter
    #[must_use]
    pub fn might_contain(&self, value: u64) -> bool {
        for i in 0..self.hash_count {
            let bit_idx = self.hash(value, i);
            let word_idx = bit_idx / 64;
            let bit_pos = bit_idx % 64;
            if self.bits[word_idx] & (1 << bit_pos) == 0 {
                return false;
            }
        }
        true
    }

    /// Get number of items added
    #[must_use]
    pub fn len(&self) -> u64 {
        self.items
    }

    /// Check if empty
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.items == 0
    }

    /// Get estimated false positive rate
    #[must_use]
    pub fn false_positive_rate(&self) -> f64 {
        let m = 1024.0; // bits
        let k = self.hash_count as f64;
        let n = self.items as f64;
        if n == 0.0 {
            return 0.0;
        }
        (1.0 - (-k * n / m).exp()).powf(k)
    }

    /// Get fill percentage (bits set / total bits)
    #[must_use]
    pub fn fill_percentage(&self) -> f64 {
        let set_bits: u32 = self.bits.iter().map(|w| w.count_ones()).sum();
        (set_bits as f64 / 1024.0) * 100.0
    }

    /// Reset filter
    pub fn reset(&mut self) {
        self.bits = [0; 16];
        self.items = 0;
    }
}

/// O(1) weighted round-robin load balancer.
///
/// Distributes load across backends with configurable weights.
#[derive(Debug, Clone)]
pub struct LoadBalancer {
    /// Backend weights
    weights: [u32; 8],
    /// Current weights (for WRR algorithm)
    current: [i32; 8],
    /// Active backends count
    active: usize,
    /// Total requests dispatched
    dispatched: u64,
    /// Requests per backend
    per_backend: [u64; 8],
}

impl Default for LoadBalancer {
    fn default() -> Self {
        Self::new()
    }
}

impl LoadBalancer {
    /// Create empty load balancer
    #[must_use]
    pub fn new() -> Self {
        Self {
            weights: [0; 8],
            current: [0; 8],
            active: 0,
            dispatched: 0,
            per_backend: [0; 8],
        }
    }

    /// Create with equal weights for n backends
    #[must_use]
    pub fn equal_weights(n: usize) -> Self {
        let mut lb = Self::new();
        for _ in 0..n.min(8) {
            lb.add_backend(1);
        }
        lb
    }

    /// Add backend with weight
    pub fn add_backend(&mut self, weight: u32) {
        if self.active < 8 {
            self.weights[self.active] = weight.max(1);
            self.current[self.active] = 0;
            self.active += 1;
        }
    }

    /// Select next backend (weighted round-robin)
    #[must_use]
    pub fn select_backend(&mut self) -> Option<usize> {
        if self.active == 0 {
            return None;
        }

        // Weighted round-robin: select backend with highest current weight
        let total_weight: i32 = self.weights[..self.active].iter().map(|&w| w as i32).sum();

        // Add weights to current
        for i in 0..self.active {
            self.current[i] += self.weights[i] as i32;
        }

        // Find max current weight
        let mut max_idx = 0;
        let mut max_weight = self.current[0];
        for i in 1..self.active {
            if self.current[i] > max_weight {
                max_weight = self.current[i];
                max_idx = i;
            }
        }

        // Subtract total weight from selected
        self.current[max_idx] -= total_weight;
        self.dispatched += 1;
        self.per_backend[max_idx] += 1;

        Some(max_idx)
    }

    /// Get distribution percentage for backend
    #[must_use]
    pub fn distribution(&self, backend: usize) -> f64 {
        if self.dispatched == 0 || backend >= self.active {
            0.0
        } else {
            (self.per_backend[backend] as f64 / self.dispatched as f64) * 100.0
        }
    }

    /// Get total dispatched
    #[must_use]
    pub fn total_dispatched(&self) -> u64 {
        self.dispatched
    }

    /// Get active backend count
    #[must_use]
    pub fn backend_count(&self) -> usize {
        self.active
    }

    /// Check if load is balanced (within threshold)
    #[must_use]
    pub fn is_balanced(&self, threshold: f64) -> bool {
        if self.active <= 1 || self.dispatched < 10 {
            return true;
        }
        let avg = self.dispatched as f64 / self.active as f64;
        for i in 0..self.active {
            let deviation = ((self.per_backend[i] as f64 - avg) / avg).abs() * 100.0;
            if deviation > threshold {
                return false;
            }
        }
        true
    }

    /// Reset all counters
    pub fn reset(&mut self) {
        self.current = [0; 8];
        self.dispatched = 0;
        self.per_backend = [0; 8];
    }
}

/// O(1) token bucket with burst tracking.
///
/// Enhanced token bucket that tracks burst patterns.
#[derive(Debug, Clone)]
pub struct BurstTracker {
    /// Current tokens
    tokens: f64,
    /// Bucket capacity
    capacity: f64,
    /// Refill rate (tokens per second)
    refill_rate: f64,
    /// Last update timestamp (us)
    last_update_us: u64,
    /// Current burst count
    burst_count: u64,
    /// Max burst seen
    max_burst: u64,
    /// Total bursts
    total_bursts: u64,
}

impl Default for BurstTracker {
    fn default() -> Self {
        Self::new(100.0, 10.0)
    }
}

impl BurstTracker {
    /// Create with capacity and refill rate
    #[must_use]
    pub fn new(capacity: f64, refill_rate: f64) -> Self {
        Self {
            tokens: capacity,
            capacity: capacity.max(1.0),
            refill_rate: refill_rate.max(0.1),
            last_update_us: 0,
            burst_count: 0,
            max_burst: 0,
            total_bursts: 0,
        }
    }

    /// Create for API rate limiting
    #[must_use]
    pub fn for_api() -> Self {
        Self::new(100.0, 50.0)
    }

    /// Create for network throttling
    #[must_use]
    pub fn for_network() -> Self {
        Self::new(1000.0, 100.0)
    }

    /// Consume tokens, returns true if allowed
    pub fn consume(&mut self, tokens: f64, now_us: u64) -> bool {
        self.refill(now_us);

        if tokens <= self.tokens {
            self.tokens -= tokens;
            self.burst_count += 1;
            if self.burst_count > self.max_burst {
                self.max_burst = self.burst_count;
            }
            true
        } else {
            // End of burst
            if self.burst_count > 0 {
                self.total_bursts += 1;
            }
            self.burst_count = 0;
            false
        }
    }

    fn refill(&mut self, now_us: u64) {
        if self.last_update_us == 0 {
            self.last_update_us = now_us;
            return;
        }
        let elapsed_s = (now_us.saturating_sub(self.last_update_us)) as f64 / 1_000_000.0;
        let refill = elapsed_s * self.refill_rate;
        self.tokens = (self.tokens + refill).min(self.capacity);
        self.last_update_us = now_us;
    }

    /// Get current token count
    #[must_use]
    pub fn tokens(&self) -> f64 {
        self.tokens
    }

    /// Get fill percentage
    #[must_use]
    pub fn fill_percentage(&self) -> f64 {
        (self.tokens / self.capacity) * 100.0
    }

    /// Get max burst size seen
    #[must_use]
    pub fn max_burst(&self) -> u64 {
        self.max_burst
    }

    /// Get total bursts
    #[must_use]
    pub fn total_bursts(&self) -> u64 {
        self.total_bursts
    }

    /// Get average burst size
    #[must_use]
    pub fn avg_burst(&self) -> f64 {
        if self.total_bursts == 0 {
            0.0
        } else {
            self.max_burst as f64 // Approximation
        }
    }

    /// Reset tracker
    pub fn reset(&mut self) {
        self.tokens = self.capacity;
        self.burst_count = 0;
        self.max_burst = 0;
        self.total_bursts = 0;
        self.last_update_us = 0;
    }
}

// ============================================================================
// TopKTracker - Fixed-size top-K value tracker (O(1) amortized insertion)
// ============================================================================

/// O(1) amortized top-K value tracker.
/// Uses a fixed-size array with insertion sort for small K values.
#[derive(Debug, Clone)]
pub struct TopKTracker {
    values: [f64; 32],
    count: usize,
    k: usize,
}

impl Default for TopKTracker {
    fn default() -> Self {
        Self::new(10)
    }
}

impl TopKTracker {
    /// Create new top-K tracker
    #[must_use]
    pub fn new(k: usize) -> Self {
        Self {
            values: [f64::NEG_INFINITY; 32],
            count: 0,
            k: k.min(32),
        }
    }

    /// Create for metrics (top 10)
    #[must_use]
    pub fn for_metrics() -> Self {
        Self::new(10)
    }

    /// Create for processes (top 20)
    #[must_use]
    pub fn for_processes() -> Self {
        Self::new(20)
    }

    /// Add value (O(k) insertion)
    pub fn add(&mut self, value: f64) {
        if self.count < self.k {
            // Not full yet, insert in sorted order
            let mut i = self.count;
            while i > 0 && self.values[i - 1] < value {
                self.values[i] = self.values[i - 1];
                i -= 1;
            }
            self.values[i] = value;
            self.count += 1;
        } else if value > self.values[self.k - 1] {
            // Replace minimum if value is larger
            let mut i = self.k - 1;
            while i > 0 && self.values[i - 1] < value {
                self.values[i] = self.values[i - 1];
                i -= 1;
            }
            self.values[i] = value;
        }
    }

    /// Get top-K values (sorted descending)
    #[must_use]
    pub fn top(&self) -> &[f64] {
        &self.values[..self.count]
    }

    /// Get K value
    #[must_use]
    pub fn k(&self) -> usize {
        self.k
    }

    /// Get count of tracked values
    #[must_use]
    pub fn count(&self) -> usize {
        self.count
    }

    /// Get minimum value in top-K
    #[must_use]
    pub fn minimum(&self) -> Option<f64> {
        if self.count > 0 {
            Some(self.values[self.count - 1])
        } else {
            None
        }
    }

    /// Get maximum value (always at index 0)
    #[must_use]
    pub fn maximum(&self) -> Option<f64> {
        if self.count > 0 {
            Some(self.values[0])
        } else {
            None
        }
    }

    /// Reset tracker
    pub fn reset(&mut self) {
        self.values = [f64::NEG_INFINITY; 32];
        self.count = 0;
    }
}

// ============================================================================
// QuotaTracker - Resource quota tracking
// ============================================================================

/// O(1) resource quota tracker.
/// Tracks usage against a limit with percentage and exhaustion checks.
#[derive(Debug, Clone)]
pub struct QuotaTracker {
    limit: u64,
    used: u64,
    peak_usage: u64,
}

impl Default for QuotaTracker {
    fn default() -> Self {
        Self::new(1000)
    }
}

impl QuotaTracker {
    /// Create with limit
    #[must_use]
    pub fn new(limit: u64) -> Self {
        Self {
            limit: limit.max(1),
            used: 0,
            peak_usage: 0,
        }
    }

    /// Create for API daily limit (10K requests)
    #[must_use]
    pub fn for_api_daily() -> Self {
        Self::new(10000)
    }

    /// Create for storage limit (100 GB)
    #[must_use]
    pub fn for_storage_gb() -> Self {
        Self::new(100)
    }

    /// Use quota, returns false if would exceed
    pub fn use_quota(&mut self, amount: u64) -> bool {
        if self.used + amount > self.limit {
            false
        } else {
            self.used += amount;
            if self.used > self.peak_usage {
                self.peak_usage = self.used;
            }
            true
        }
    }

    /// Release quota
    pub fn release(&mut self, amount: u64) {
        self.used = self.used.saturating_sub(amount);
    }

    /// Get limit
    #[must_use]
    pub fn limit(&self) -> u64 {
        self.limit
    }

    /// Get remaining quota
    #[must_use]
    pub fn remaining(&self) -> u64 {
        self.limit.saturating_sub(self.used)
    }

    /// Get usage percentage
    #[must_use]
    pub fn usage_percentage(&self) -> f64 {
        (self.used as f64 / self.limit as f64) * 100.0
    }

    /// Check if exhausted
    #[must_use]
    pub fn is_exhausted(&self) -> bool {
        self.used >= self.limit
    }

    /// Get peak usage
    #[must_use]
    pub fn peak_usage(&self) -> u64 {
        self.peak_usage
    }

    /// Reset tracker
    pub fn reset(&mut self) {
        self.used = 0;
        self.peak_usage = 0;
    }
}

// ============================================================================
// FrequencyCounter - Categorical frequency tracking
// ============================================================================

/// O(1) categorical frequency counter.
/// Tracks occurrence counts and calculates frequencies for up to 16 categories.
#[derive(Debug, Clone)]
pub struct FrequencyCounter {
    counts: [u64; 16],
    total: u64,
}

impl Default for FrequencyCounter {
    fn default() -> Self {
        Self::new()
    }
}

impl FrequencyCounter {
    /// Create new counter
    #[must_use]
    pub fn new() -> Self {
        Self {
            counts: [0; 16],
            total: 0,
        }
    }

    /// Increment category count
    pub fn increment(&mut self, category: usize) {
        if category < 16 {
            self.counts[category] += 1;
            self.total += 1;
        }
    }

    /// Add multiple to category
    pub fn add(&mut self, category: usize, count: u64) {
        if category < 16 {
            self.counts[category] += count;
            self.total += count;
        }
    }

    /// Get count for category
    #[must_use]
    pub fn count(&self, category: usize) -> u64 {
        if category < 16 {
            self.counts[category]
        } else {
            0
        }
    }

    /// Get frequency percentage for category
    #[must_use]
    pub fn frequency(&self, category: usize) -> f64 {
        if self.total == 0 || category >= 16 {
            0.0
        } else {
            (self.counts[category] as f64 / self.total as f64) * 100.0
        }
    }

    /// Get total count
    #[must_use]
    pub fn total(&self) -> u64 {
        self.total
    }

    /// Get most frequent category
    #[must_use]
    pub fn most_frequent(&self) -> Option<usize> {
        if self.total == 0 {
            return None;
        }
        let mut max_idx = 0;
        let mut max_count = self.counts[0];
        for i in 1..16 {
            if self.counts[i] > max_count {
                max_count = self.counts[i];
                max_idx = i;
            }
        }
        Some(max_idx)
    }

    /// Get number of non-zero categories
    #[must_use]
    pub fn non_zero_count(&self) -> usize {
        self.counts.iter().filter(|&&c| c > 0).count()
    }

    /// Calculate Shannon entropy (normalized 0-1)
    #[must_use]
    pub fn entropy(&self) -> f64 {
        if self.total == 0 {
            return 0.0;
        }
        let mut entropy = 0.0;
        for &count in &self.counts {
            if count > 0 {
                let p = count as f64 / self.total as f64;
                entropy -= p * p.log2();
            }
        }
        // Normalize by max entropy (log2(16) = 4)
        entropy / 4.0
    }

    /// Reset counter
    pub fn reset(&mut self) {
        self.counts = [0; 16];
        self.total = 0;
    }
}

// ============================================================================
// MovingRange - Moving min/max range tracking for volatility
// ============================================================================

/// O(1) moving range tracker for volatility analysis.
/// Maintains min/max over a sliding window for range and volatility metrics.
#[derive(Debug, Clone)]
pub struct MovingRange {
    values: [f64; 128],
    window_size: usize,
    head: usize,
    count: usize,
    current_min: f64,
    current_max: f64,
}

impl Default for MovingRange {
    fn default() -> Self {
        Self::new(10)
    }
}

impl MovingRange {
    /// Create with window size
    #[must_use]
    pub fn new(window_size: usize) -> Self {
        Self {
            values: [0.0; 128],
            window_size: window_size.min(128),
            head: 0,
            count: 0,
            current_min: f64::INFINITY,
            current_max: f64::NEG_INFINITY,
        }
    }

    /// Create for price volatility (20 samples)
    #[must_use]
    pub fn for_prices() -> Self {
        Self::new(20)
    }

    /// Create for latency volatility (100 samples)
    #[must_use]
    pub fn for_latency() -> Self {
        Self::new(100)
    }

    /// Add value to window
    pub fn add(&mut self, value: f64) {
        let idx = self.head;
        self.values[idx] = value;
        self.head = (self.head + 1) % self.window_size;
        if self.count < self.window_size {
            self.count += 1;
        }
        self.recalculate_minmax();
    }

    fn recalculate_minmax(&mut self) {
        self.current_min = f64::INFINITY;
        self.current_max = f64::NEG_INFINITY;
        for i in 0..self.count {
            let v = self.values[i];
            if v < self.current_min {
                self.current_min = v;
            }
            if v > self.current_max {
                self.current_max = v;
            }
        }
    }

    /// Get window size
    #[must_use]
    pub fn window_size(&self) -> usize {
        self.window_size
    }

    /// Get current count
    #[must_use]
    pub fn count(&self) -> usize {
        self.count
    }

    /// Get minimum value
    #[must_use]
    pub fn min(&self) -> Option<f64> {
        if self.count > 0 {
            Some(self.current_min)
        } else {
            None
        }
    }

    /// Get maximum value
    #[must_use]
    pub fn max(&self) -> Option<f64> {
        if self.count > 0 {
            Some(self.current_max)
        } else {
            None
        }
    }

    /// Get range (max - min)
    #[must_use]
    pub fn range(&self) -> f64 {
        if self.count > 0 {
            self.current_max - self.current_min
        } else {
            0.0
        }
    }

    /// Get mid-range ((max + min) / 2)
    #[must_use]
    pub fn midrange(&self) -> f64 {
        if self.count > 0 {
            (self.current_max + self.current_min) / 2.0
        } else {
            0.0
        }
    }

    /// Get volatility (range / midrange * 100)
    #[must_use]
    pub fn volatility(&self) -> f64 {
        let mid = self.midrange();
        if mid.abs() < 0.0001 {
            0.0
        } else {
            (self.range() / mid) * 100.0
        }
    }

    /// Reset tracker
    pub fn reset(&mut self) {
        self.values = [0.0; 128];
        self.head = 0;
        self.count = 0;
        self.current_min = f64::INFINITY;
        self.current_max = f64::NEG_INFINITY;
    }
}

// ============================================================================
// TimeoutTracker - Operation timeout tracking
// ============================================================================

/// O(1) operation timeout tracker.
/// Tracks successful and timed-out operations with configurable timeout threshold.
#[derive(Debug, Clone)]
pub struct TimeoutTracker {
    timeout_us: u64,
    total: u64,
    timed_out: u64,
    last_duration_us: u64,
    max_duration_us: u64,
}

impl Default for TimeoutTracker {
    fn default() -> Self {
        Self::new(1_000_000) // 1 second default
    }
}

impl TimeoutTracker {
    /// Create with timeout threshold in microseconds
    #[must_use]
    pub fn new(timeout_us: u64) -> Self {
        Self {
            timeout_us: timeout_us.max(1),
            total: 0,
            timed_out: 0,
            last_duration_us: 0,
            max_duration_us: 0,
        }
    }

    /// Create for network operations (5s timeout)
    #[must_use]
    pub fn for_network() -> Self {
        Self::new(5_000_000)
    }

    /// Create for database operations (30s timeout)
    #[must_use]
    pub fn for_database() -> Self {
        Self::new(30_000_000)
    }

    /// Create for fast operations (100ms timeout)
    #[must_use]
    pub fn for_fast() -> Self {
        Self::new(100_000)
    }

    /// Record operation completion
    pub fn record(&mut self, duration_us: u64) {
        self.total += 1;
        self.last_duration_us = duration_us;
        if duration_us > self.max_duration_us {
            self.max_duration_us = duration_us;
        }
        if duration_us > self.timeout_us {
            self.timed_out += 1;
        }
    }

    /// Get total operations
    #[must_use]
    pub fn total(&self) -> u64 {
        self.total
    }

    /// Get timed out count
    #[must_use]
    pub fn timed_out(&self) -> u64 {
        self.timed_out
    }

    /// Get timeout rate as percentage
    #[must_use]
    pub fn timeout_rate(&self) -> f64 {
        if self.total == 0 {
            0.0
        } else {
            (self.timed_out as f64 / self.total as f64) * 100.0
        }
    }

    /// Get success rate as percentage
    #[must_use]
    pub fn success_rate(&self) -> f64 {
        100.0 - self.timeout_rate()
    }

    /// Check if timeout rate is acceptable
    #[must_use]
    pub fn is_healthy(&self, max_timeout_rate: f64) -> bool {
        self.timeout_rate() <= max_timeout_rate
    }

    /// Get max duration seen
    #[must_use]
    pub fn max_duration_us(&self) -> u64 {
        self.max_duration_us
    }

    /// Get timeout threshold
    #[must_use]
    pub fn timeout_threshold_us(&self) -> u64 {
        self.timeout_us
    }

    /// Reset tracker
    pub fn reset(&mut self) {
        self.total = 0;
        self.timed_out = 0;
        self.last_duration_us = 0;
        self.max_duration_us = 0;
    }
}

// ============================================================================
// RetryTracker - Retry attempt tracking with backoff state
// ============================================================================

/// O(1) retry tracking with exponential backoff state.
/// Tracks retry attempts, success after retry, and calculates next retry delay.
#[derive(Debug, Clone)]
pub struct RetryTracker {
    max_retries: u32,
    base_delay_ms: u64,
    max_delay_ms: u64,
    total_attempts: u64,
    total_retries: u64,
    successful_retries: u64,
    current_retry: u32,
}

impl Default for RetryTracker {
    fn default() -> Self {
        Self::new(3, 100, 10000)
    }
}

impl RetryTracker {
    /// Create with max retries, base delay, and max delay in ms
    #[must_use]
    pub fn new(max_retries: u32, base_delay_ms: u64, max_delay_ms: u64) -> Self {
        Self {
            max_retries,
            base_delay_ms: base_delay_ms.max(1),
            max_delay_ms: max_delay_ms.max(base_delay_ms),
            total_attempts: 0,
            total_retries: 0,
            successful_retries: 0,
            current_retry: 0,
        }
    }

    /// Create for API retries (3 retries, 100ms base, 10s max)
    #[must_use]
    pub fn for_api() -> Self {
        Self::new(3, 100, 10000)
    }

    /// Create for network retries (5 retries, 1s base, 30s max)
    #[must_use]
    pub fn for_network() -> Self {
        Self::new(5, 1000, 30000)
    }

    /// Record attempt start
    pub fn attempt(&mut self) {
        self.total_attempts += 1;
    }

    /// Record retry (failed attempt, will retry)
    pub fn retry(&mut self) {
        self.total_retries += 1;
        if self.current_retry < self.max_retries {
            self.current_retry += 1;
        }
    }

    /// Record success (resets current retry count)
    pub fn success(&mut self) {
        if self.current_retry > 0 {
            self.successful_retries += 1;
        }
        self.current_retry = 0;
    }

    /// Get next retry delay in ms (exponential backoff)
    #[must_use]
    pub fn next_delay_ms(&self) -> u64 {
        let delay = self.base_delay_ms * (1 << self.current_retry);
        delay.min(self.max_delay_ms)
    }

    /// Check if retries exhausted
    #[must_use]
    pub fn retries_exhausted(&self) -> bool {
        self.current_retry >= self.max_retries
    }

    /// Get retry rate as percentage
    #[must_use]
    pub fn retry_rate(&self) -> f64 {
        if self.total_attempts == 0 {
            0.0
        } else {
            (self.total_retries as f64 / self.total_attempts as f64) * 100.0
        }
    }

    /// Get successful retry rate
    #[must_use]
    pub fn successful_retry_rate(&self) -> f64 {
        if self.total_retries == 0 {
            0.0
        } else {
            (self.successful_retries as f64 / self.total_retries as f64) * 100.0
        }
    }

    /// Get current retry count
    #[must_use]
    pub fn current_retry(&self) -> u32 {
        self.current_retry
    }

    /// Reset tracker
    pub fn reset(&mut self) {
        self.total_attempts = 0;
        self.total_retries = 0;
        self.successful_retries = 0;
        self.current_retry = 0;
    }
}

// ============================================================================
// ScheduleSlot - Time-based slot scheduling
// ============================================================================

/// O(1) time-based slot scheduler.
/// Divides time into slots and tracks which slot is currently active.
#[derive(Debug, Clone)]
pub struct ScheduleSlot {
    slot_duration_us: u64,
    num_slots: usize,
    current_slot: usize,
    slot_start_us: u64,
    executions_per_slot: [u64; 16],
}

impl Default for ScheduleSlot {
    fn default() -> Self {
        Self::new(1_000_000, 10) // 1 second slots, 10 slots
    }
}

impl ScheduleSlot {
    /// Create with slot duration in microseconds and number of slots
    #[must_use]
    pub fn new(slot_duration_us: u64, num_slots: usize) -> Self {
        Self {
            slot_duration_us: slot_duration_us.max(1),
            num_slots: num_slots.min(16).max(1),
            current_slot: 0,
            slot_start_us: 0,
            executions_per_slot: [0; 16],
        }
    }

    /// Create for round-robin scheduling (1 second slots, 10 slots)
    #[must_use]
    pub fn for_round_robin() -> Self {
        Self::new(1_000_000, 10)
    }

    /// Create for minute-based scheduling (1 minute slots, 5 slots)
    #[must_use]
    pub fn for_minute() -> Self {
        Self::new(60_000_000, 5)
    }

    /// Update slot based on current time
    pub fn update(&mut self, now_us: u64) {
        if self.slot_start_us == 0 {
            self.slot_start_us = now_us;
            return;
        }

        let elapsed = now_us.saturating_sub(self.slot_start_us);
        let slots_passed = (elapsed / self.slot_duration_us) as usize;

        if slots_passed > 0 {
            self.current_slot = (self.current_slot + slots_passed) % self.num_slots;
            self.slot_start_us = now_us;
        }
    }

    /// Record execution in current slot
    pub fn execute(&mut self, now_us: u64) {
        self.update(now_us);
        if self.current_slot < 16 {
            self.executions_per_slot[self.current_slot] += 1;
        }
    }

    /// Get current slot
    #[must_use]
    pub fn current_slot(&self) -> usize {
        self.current_slot
    }

    /// Get number of slots
    #[must_use]
    pub fn num_slots(&self) -> usize {
        self.num_slots
    }

    /// Get executions for a slot
    #[must_use]
    pub fn executions(&self, slot: usize) -> u64 {
        if slot < 16 {
            self.executions_per_slot[slot]
        } else {
            0
        }
    }

    /// Get total executions across all slots
    #[must_use]
    pub fn total_executions(&self) -> u64 {
        self.executions_per_slot[..self.num_slots].iter().sum()
    }

    /// Check if slots are evenly distributed (within threshold %)
    #[must_use]
    pub fn is_balanced(&self, threshold: f64) -> bool {
        let total = self.total_executions();
        if total == 0 {
            return true;
        }
        let expected = total as f64 / self.num_slots as f64;
        for i in 0..self.num_slots {
            let diff = (self.executions_per_slot[i] as f64 - expected).abs();
            if diff / expected * 100.0 > threshold {
                return false;
            }
        }
        true
    }

    /// Reset tracker
    pub fn reset(&mut self) {
        self.current_slot = 0;
        self.slot_start_us = 0;
        self.executions_per_slot = [0; 16];
    }
}

// ============================================================================
// CooldownTimer - Cooldown period tracking
// ============================================================================

/// O(1) cooldown timer for rate limiting actions.
/// Tracks when an action can next be performed based on cooldown period.
#[derive(Debug, Clone)]
pub struct CooldownTimer {
    cooldown_us: u64,
    last_action_us: u64,
    total_actions: u64,
    blocked_attempts: u64,
}

impl Default for CooldownTimer {
    fn default() -> Self {
        Self::new(1_000_000) // 1 second cooldown
    }
}

impl CooldownTimer {
    /// Create with cooldown period in microseconds
    #[must_use]
    pub fn new(cooldown_us: u64) -> Self {
        Self {
            cooldown_us: cooldown_us.max(1),
            last_action_us: 0,
            total_actions: 0,
            blocked_attempts: 0,
        }
    }

    /// Create for fast cooldown (100ms)
    #[must_use]
    pub fn for_fast() -> Self {
        Self::new(100_000)
    }

    /// Create for normal cooldown (1 second)
    #[must_use]
    pub fn for_normal() -> Self {
        Self::new(1_000_000)
    }

    /// Create for slow cooldown (10 seconds)
    #[must_use]
    pub fn for_slow() -> Self {
        Self::new(10_000_000)
    }

    /// Check if action is ready (cooldown expired)
    #[must_use]
    pub fn is_ready(&self, now_us: u64) -> bool {
        if self.last_action_us == 0 {
            return true;
        }
        now_us.saturating_sub(self.last_action_us) >= self.cooldown_us
    }

    /// Try to perform action, returns true if allowed
    pub fn try_action(&mut self, now_us: u64) -> bool {
        if self.is_ready(now_us) {
            self.last_action_us = now_us;
            self.total_actions += 1;
            true
        } else {
            self.blocked_attempts += 1;
            false
        }
    }

    /// Force action (ignores cooldown)
    pub fn force_action(&mut self, now_us: u64) {
        self.last_action_us = now_us;
        self.total_actions += 1;
    }

    /// Get remaining cooldown time in microseconds
    #[must_use]
    pub fn remaining_us(&self, now_us: u64) -> u64 {
        if self.is_ready(now_us) {
            0
        } else {
            self.cooldown_us
                .saturating_sub(now_us.saturating_sub(self.last_action_us))
        }
    }

    /// Get cooldown period
    #[must_use]
    pub fn cooldown_us(&self) -> u64 {
        self.cooldown_us
    }

    /// Get total actions performed
    #[must_use]
    pub fn total_actions(&self) -> u64 {
        self.total_actions
    }

    /// Get blocked attempts
    #[must_use]
    pub fn blocked_attempts(&self) -> u64 {
        self.blocked_attempts
    }

    /// Get block rate as percentage
    #[must_use]
    pub fn block_rate(&self) -> f64 {
        let total = self.total_actions + self.blocked_attempts;
        if total == 0 {
            0.0
        } else {
            (self.blocked_attempts as f64 / total as f64) * 100.0
        }
    }

    /// Reset timer
    pub fn reset(&mut self) {
        self.last_action_us = 0;
        self.total_actions = 0;
        self.blocked_attempts = 0;
    }
}

// ============================================================================
// BackpressureMonitor - Track backpressure signals
// ============================================================================

/// O(1) backpressure monitoring.
/// Tracks when downstream systems signal overload and calculates pressure rates.
#[derive(Debug, Clone)]
pub struct BackpressureMonitor {
    signals: u64,
    total_ops: u64,
    consecutive: u32,
    max_consecutive: u32,
    last_signal_us: u64,
}

impl Default for BackpressureMonitor {
    fn default() -> Self {
        Self::new()
    }
}

impl BackpressureMonitor {
    /// Create new monitor
    #[must_use]
    pub fn new() -> Self {
        Self {
            signals: 0,
            total_ops: 0,
            consecutive: 0,
            max_consecutive: 0,
            last_signal_us: 0,
        }
    }

    /// Record successful operation (no backpressure)
    pub fn success(&mut self) {
        self.total_ops += 1;
        self.consecutive = 0;
    }

    /// Record backpressure signal
    pub fn signal(&mut self, now_us: u64) {
        self.signals += 1;
        self.total_ops += 1;
        self.consecutive += 1;
        self.last_signal_us = now_us;
        if self.consecutive > self.max_consecutive {
            self.max_consecutive = self.consecutive;
        }
    }

    /// Get backpressure rate as percentage
    #[must_use]
    pub fn pressure_rate(&self) -> f64 {
        if self.total_ops == 0 {
            0.0
        } else {
            (self.signals as f64 / self.total_ops as f64) * 100.0
        }
    }

    /// Check if currently under pressure (consecutive signals)
    #[must_use]
    pub fn is_under_pressure(&self, threshold: u32) -> bool {
        self.consecutive >= threshold
    }

    /// Get consecutive signal count
    #[must_use]
    pub fn consecutive(&self) -> u32 {
        self.consecutive
    }

    /// Get max consecutive signals
    #[must_use]
    pub fn max_consecutive(&self) -> u32 {
        self.max_consecutive
    }

    /// Get total signals
    #[must_use]
    pub fn total_signals(&self) -> u64 {
        self.signals
    }

    /// Check if healthy (below threshold)
    #[must_use]
    pub fn is_healthy(&self, max_rate: f64) -> bool {
        self.pressure_rate() <= max_rate
    }

    /// Reset monitor
    pub fn reset(&mut self) {
        self.signals = 0;
        self.total_ops = 0;
        self.consecutive = 0;
        self.max_consecutive = 0;
        self.last_signal_us = 0;
    }
}

// ============================================================================
// CapacityPlanner - Track capacity utilization for planning
// ============================================================================

/// O(1) capacity planning tracker.
/// Monitors utilization over time and predicts when capacity will be exhausted.
#[derive(Debug, Clone)]
pub struct CapacityPlanner {
    capacity: u64,
    current: u64,
    peak: u64,
    samples: u32,
    sum_utilization: f64,
    growth_rate: f64,
}

impl Default for CapacityPlanner {
    fn default() -> Self {
        Self::new(1000)
    }
}

impl CapacityPlanner {
    /// Create with capacity
    #[must_use]
    pub fn new(capacity: u64) -> Self {
        Self {
            capacity: capacity.max(1),
            current: 0,
            peak: 0,
            samples: 0,
            sum_utilization: 0.0,
            growth_rate: 0.0,
        }
    }

    /// Create for connections (1000)
    #[must_use]
    pub fn for_connections() -> Self {
        Self::new(1000)
    }

    /// Create for storage GB (100)
    #[must_use]
    pub fn for_storage() -> Self {
        Self::new(100)
    }

    /// Update current usage
    pub fn update(&mut self, current: u64) {
        let old = self.current;
        self.current = current;
        if current > self.peak {
            self.peak = current;
        }
        self.samples += 1;
        self.sum_utilization += self.utilization();

        // Calculate growth rate (simple difference)
        if old > 0 {
            self.growth_rate = (current as f64 - old as f64) / old as f64;
        }
    }

    /// Get current utilization as percentage
    #[must_use]
    pub fn utilization(&self) -> f64 {
        (self.current as f64 / self.capacity as f64) * 100.0
    }

    /// Get peak utilization as percentage
    #[must_use]
    pub fn peak_utilization(&self) -> f64 {
        (self.peak as f64 / self.capacity as f64) * 100.0
    }

    /// Get average utilization
    #[must_use]
    pub fn avg_utilization(&self) -> f64 {
        if self.samples == 0 {
            0.0
        } else {
            self.sum_utilization / self.samples as f64
        }
    }

    /// Get remaining capacity
    #[must_use]
    pub fn remaining(&self) -> u64 {
        self.capacity.saturating_sub(self.current)
    }

    /// Check if at risk (above threshold)
    #[must_use]
    pub fn at_risk(&self, threshold: f64) -> bool {
        self.utilization() >= threshold
    }

    /// Get growth rate
    #[must_use]
    pub fn growth_rate(&self) -> f64 {
        self.growth_rate
    }

    /// Reset planner
    pub fn reset(&mut self) {
        self.current = 0;
        self.peak = 0;
        self.samples = 0;
        self.sum_utilization = 0.0;
        self.growth_rate = 0.0;
    }
}

// ============================================================================
// DriftTracker - Track clock/timing drift
// ============================================================================

/// O(1) drift tracking for timing synchronization.
/// Monitors deviation from expected intervals and detects clock drift.
#[derive(Debug, Clone)]
pub struct DriftTracker {
    expected_interval_us: u64,
    last_timestamp_us: u64,
    total_drift_us: i64,
    samples: u64,
    max_drift_us: i64,
    min_drift_us: i64,
}

impl Default for DriftTracker {
    fn default() -> Self {
        Self::new(1_000_000) // 1 second expected interval
    }
}

impl DriftTracker {
    /// Create with expected interval in microseconds
    #[must_use]
    pub fn new(expected_interval_us: u64) -> Self {
        Self {
            expected_interval_us: expected_interval_us.max(1),
            last_timestamp_us: 0,
            total_drift_us: 0,
            samples: 0,
            max_drift_us: i64::MIN,
            min_drift_us: i64::MAX,
        }
    }

    /// Create for 60fps (16.67ms interval)
    #[must_use]
    pub fn for_60fps() -> Self {
        Self::new(16_667)
    }

    /// Create for 1 second heartbeat
    #[must_use]
    pub fn for_heartbeat() -> Self {
        Self::new(1_000_000)
    }

    /// Record timestamp and calculate drift
    pub fn record(&mut self, now_us: u64) {
        if self.last_timestamp_us == 0 {
            self.last_timestamp_us = now_us;
            return;
        }

        let actual_interval = now_us.saturating_sub(self.last_timestamp_us);
        let drift = actual_interval as i64 - self.expected_interval_us as i64;

        self.total_drift_us += drift;
        self.samples += 1;

        if drift > self.max_drift_us {
            self.max_drift_us = drift;
        }
        if drift < self.min_drift_us {
            self.min_drift_us = drift;
        }

        self.last_timestamp_us = now_us;
    }

    /// Get average drift in microseconds
    #[must_use]
    pub fn avg_drift_us(&self) -> f64 {
        if self.samples == 0 {
            0.0
        } else {
            self.total_drift_us as f64 / self.samples as f64
        }
    }

    /// Get max drift (positive = late, negative = early)
    #[must_use]
    pub fn max_drift_us(&self) -> i64 {
        if self.samples == 0 {
            0
        } else {
            self.max_drift_us
        }
    }

    /// Get min drift
    #[must_use]
    pub fn min_drift_us(&self) -> i64 {
        if self.samples == 0 {
            0
        } else {
            self.min_drift_us
        }
    }

    /// Check if drift is within tolerance
    #[must_use]
    pub fn is_stable(&self, tolerance_us: i64) -> bool {
        self.avg_drift_us().abs() < tolerance_us as f64
    }

    /// Get drift range
    #[must_use]
    pub fn drift_range_us(&self) -> i64 {
        if self.samples == 0 {
            0
        } else {
            self.max_drift_us - self.min_drift_us
        }
    }

    /// Get sample count
    #[must_use]
    pub fn samples(&self) -> u64 {
        self.samples
    }

    /// Reset tracker
    pub fn reset(&mut self) {
        self.last_timestamp_us = 0;
        self.total_drift_us = 0;
        self.samples = 0;
        self.max_drift_us = i64::MIN;
        self.min_drift_us = i64::MAX;
    }
}

// ============================================================================
// SemaphoreTracker - Track semaphore/permit usage
// ============================================================================

/// O(1) semaphore usage tracker.
/// Monitors permit acquisition and release patterns.
#[derive(Debug, Clone)]
pub struct SemaphoreTracker {
    total_permits: u32,
    acquired: u32,
    peak_acquired: u32,
    acquisitions: u64,
    releases: u64,
    contentions: u64,
}

impl Default for SemaphoreTracker {
    fn default() -> Self {
        Self::new(10)
    }
}

impl SemaphoreTracker {
    /// Create with total permits
    #[must_use]
    pub fn new(total_permits: u32) -> Self {
        Self {
            total_permits: total_permits.max(1),
            acquired: 0,
            peak_acquired: 0,
            acquisitions: 0,
            releases: 0,
            contentions: 0,
        }
    }

    /// Create for database connections (20)
    #[must_use]
    pub fn for_database() -> Self {
        Self::new(20)
    }

    /// Create for worker threads (8)
    #[must_use]
    pub fn for_workers() -> Self {
        Self::new(8)
    }

    /// Try to acquire permit, returns true if successful
    pub fn try_acquire(&mut self) -> bool {
        if self.acquired < self.total_permits {
            self.acquired += 1;
            self.acquisitions += 1;
            if self.acquired > self.peak_acquired {
                self.peak_acquired = self.acquired;
            }
            true
        } else {
            self.contentions += 1;
            false
        }
    }

    /// Release permit
    pub fn release(&mut self) {
        if self.acquired > 0 {
            self.acquired -= 1;
            self.releases += 1;
        }
    }

    /// Get available permits
    #[must_use]
    pub fn available(&self) -> u32 {
        self.total_permits.saturating_sub(self.acquired)
    }

    /// Get utilization as percentage
    #[must_use]
    pub fn utilization(&self) -> f64 {
        (self.acquired as f64 / self.total_permits as f64) * 100.0
    }

    /// Get peak utilization as percentage
    #[must_use]
    pub fn peak_utilization(&self) -> f64 {
        (self.peak_acquired as f64 / self.total_permits as f64) * 100.0
    }

    /// Get contention rate
    #[must_use]
    pub fn contention_rate(&self) -> f64 {
        let total = self.acquisitions + self.contentions;
        if total == 0 {
            0.0
        } else {
            (self.contentions as f64 / total as f64) * 100.0
        }
    }

    /// Check if healthy (low contention)
    #[must_use]
    pub fn is_healthy(&self, max_contention: f64) -> bool {
        self.contention_rate() <= max_contention
    }

    /// Get total permits
    #[must_use]
    pub fn total_permits(&self) -> u32 {
        self.total_permits
    }

    /// Reset tracker
    pub fn reset(&mut self) {
        self.acquired = 0;
        self.peak_acquired = 0;
        self.acquisitions = 0;
        self.releases = 0;
        self.contentions = 0;
    }
}