image.vision\aoci\aoci_index.mbt

100% · 35 / 35 points · 125 lines

Background color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.

  1. 1///|
  2. 2/// # AOCI 符号语义索引 AOCI Symbol Semantic Index
  3. 3///
  4. 4/// 全量符号索引 + 关键词倒排表
  5. 5/// 全符号索引 + 关键词倒排表 Full symbol index
  6. 6// AOCI 符号语义索引 — 符号索引结构
  7. 7// SymbolIndex: 全量符号索引 + 关键词倒排表
  8. 8
  9. 9///|
  10. 10#derive(Eq, @debug.Debug)
  11. 11pub(all) struct SymbolIndex {
  12. 12 symbols : Array[SymbolCoord]
  13. 13 keyword_map : Array[(String, Array[Int32])]
  14. 14}
  15. 15
  16. 16///|
  17. 17pub extend SymbolIndex with Eq::{not_equal, equal}
  18. 18
  19. 19///|
  20. 20pub extend SymbolIndex with @debug.Debug::{to_repr}
  21. 21
  22. 22///|
  23. 23pub fn SymbolIndex::make(symbols : Array[SymbolCoord]) -> SymbolIndex {
  24. 24● let keyword_map : Array[(String, Array[Int32])] = []
  25. 25● for i in 0..<symbols.length() {
  26. 26● for kw in symbols[i].keywords {
  27. 27● let mut found = false
  28. 28● for j in 0..<keyword_map.length() {
  29. 29● if keyword_map[j].0 == kw {
  30. 30● keyword_map[j].1.push(i)
  31. 31 found = true
  32. 32 break
  33. 33 }
  34. 34 }
  35. 35 if !found {
  36. 36● keyword_map.push((kw, [i]))
  37. 37 }
  38. 38 }
  39. 39 }
  40. 40 { symbols, keyword_map, }
  41. 41}
  42. 42
  43. 43///|
  44. 44pub fn SymbolIndex::size(self : SymbolIndex) -> Int32 {
  45. 45● self.symbols.length()
  46. 46}
  47. 47
  48. 48///|
  49. 49pub fn SymbolIndex::get(
  50. 50 self : SymbolIndex,
  51. 51 index : Int32,
  52. 52) -> SymbolCoord raise Failure {
  53. 53● if index < 0 || index >= self.symbols.length() {
  54. 54● fail("symbol index out of bounds: \{index}")
  55. 55 }
  56. 56 self.symbols[index]
  57. 57}
  58. 58
  59. 59///|
  60. 60pub fn SymbolIndex::find_by_name(
  61. 61 self : SymbolIndex,
  62. 62 name : String,
  63. 63) -> Array[SymbolCoord] {
  64. 64● let result : Array[SymbolCoord] = []
  65. 65 for sym in self.symbols {
  66. 66● if sym.name == name {
  67. 67● result.push(sym)
  68. 68 }
  69. 69 }
  70. 70 result
  71. 71}
  72. 72
  73. 73///|
  74. 74pub fn SymbolIndex::find_by_package(
  75. 75 self : SymbolIndex,
  76. 76 pkg : String,
  77. 77) -> Array[SymbolCoord] {
  78. 78● let result : Array[SymbolCoord] = []
  79. 79 for sym in self.symbols {
  80. 80● if sym.pkg == pkg {
  81. 81● result.push(sym)
  82. 82 }
  83. 83 }
  84. 84 result
  85. 85}
  86. 86
  87. 87///|
  88. 88pub fn SymbolIndex::find_by_keyword(
  89. 89 self : SymbolIndex,
  90. 90 keyword : String,
  91. 91) -> Array[SymbolCoord] {
  92. 92● for entry in self.keyword_map {
  93. 93● if entry.0 == keyword {
  94. 94● let result : Array[SymbolCoord] = []
  95. 95 for idx in entry.1 {
  96. 96● result.push(self.symbols[idx])
  97. 97 }
  98. 98 return result
  99. 99 }
  100. 100 }
  101. 101 []
  102. 102}
  103. 103
  104. 104///|
  105. 105pub fn SymbolIndex::find_by_kind(
  106. 106 self : SymbolIndex,
  107. 107 kind : SymbolKind,
  108. 108) -> Array[SymbolCoord] {
  109. 109● let result : Array[SymbolCoord] = []
  110. 110 for sym in self.symbols {
  111. 111● if sym.kind == kind {
  112. 112● result.push(sym)
  113. 113 }
  114. 114 }
  115. 115 result
  116. 116}
  117. 117
  118. 118///|
  119. 119pub fn SymbolIndex::total_tokens(self : SymbolIndex) -> Int32 {
  120. 120● let mut sum = 0
  121. 121 for sym in self.symbols {
  122. 122● sum = sum + sym.estimated_tokens()
  123. 123 }
  124. 124 sum
  125. 125}