开源 企业版 高校版 私有云 模力方舟 AI 队友
代码拉取完成,页面将自动刷新
加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
已有帐号? 立即登录
文件
devel
分支 (25)
devel
spiderbasic-3.10
v6.21
v6.20
spiderbasic-3.02
qt
v6.12
spiderbasic-3.01
enablejs-ignore-formatting
master
spiderbasic-3.00
webview-tool
v6.11
v6.10
v6.04
spiderbasic-2.51
v6.03
editor-fix-3
editor-slow-fix2
editor-slow-fix
devel
分支 (25)
devel
spiderbasic-3.10
v6.21
v6.20
spiderbasic-3.02
qt
v6.12
spiderbasic-3.01
enablejs-ignore-formatting
master
spiderbasic-3.00
webview-tool
v6.11
v6.10
v6.04
spiderbasic-2.51
v6.03
editor-fix-3
editor-slow-fix2
editor-slow-fix
克隆/下载
克隆/下载
提示
下载代码请复制以下命令到终端执行
为确保你提交的代码身份被 Gitee 正确识别,请执行以下命令完成配置
初次使用 SSH 协议进行代码克隆、推送等操作时,需按下述提示完成 SSH 配置
1 生成 RSA 密钥
2 获取 RSA 公钥内容,并配置到 SSH公钥
在 Gitee 上使用 SVN,请访问 使用指南
使用 HTTPS 协议时,命令行会出现如下账号密码验证步骤。基于安全考虑,Gitee 建议 配置并使用私人令牌 替代登录密码进行克隆、推送等操作
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # 私人令牌
devel
分支 (25)
devel
spiderbasic-3.10
v6.21
v6.20
spiderbasic-3.02
qt
v6.12
spiderbasic-3.01
enablejs-ignore-formatting
master
spiderbasic-3.00
webview-tool
v6.11
v6.10
v6.04
spiderbasic-2.51
v6.03
editor-fix-3
editor-slow-fix2
editor-slow-fix
purebasic
/
PureBasicIDE
/
RadixTree.pb
purebasic
/
PureBasicIDE
/
RadixTree.pb
RadixTree.pb 12.02 KB
一键复制 编辑 原始数据 按行查看 历史
Fred Laboureur 提交于 2024年02月08日 21:24 +08:00 . - Fixed case correction ( )
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
; --------------------------------------------------------------------------------------------
; Copyright (c) Fantaisie Software. All rights reserved.
; Dual licensed under the GPL and Fantaisie Software licenses.
; See LICENSE and LICENSE-FANTAISIE in the project root for license information.
; --------------------------------------------------------------------------------------------
;
; Simple implementation of a compact prefix tree (radix tree) to store values ordered by a key
; with fast lookup of all values matching a specific key prefix.
;
; Only "insert" and "free" are implemented because that is all we need for our purposes.
; Deleting individual keys is not implemented for now.
;
; All keys are handled Case insensitive.
;
CompilerIf Not Defined(RadixNode, #PB_Structure) ; For local testing. Defined in Common.pb too
Structure RadixNode
Chars$ ; Incoming prefix for this node (stored in uppercase)
*Child.RadixNode ; First child node (if any)
*Next.RadixNode ; Next sibling node in same parent (single linked list, sorted by prefix)
*Value ; Stored value or null
EndStructure
Structure RadixTree
*Node ; First child of the root node (other children are under Child\Next). Null for an empty tree
EndStructure
#CharSize = 2
CompilerEndIf
Procedure Radix_FreeNode(*Node.RadixNode)
While *Node
*Next = *Node\Next
Radix_FreeNode(*Node\Child)
FreeStructure(*Node)
*Node = *Next
Wend
EndProcedure
Procedure RadixFree(*Tree.RadixTree)
Radix_FreeNode(*Tree\Node)
*Tree\Node = #Null
EndProcedure
; Returns RadixNode or null
;
Procedure RadixFindPrefix(*Tree.RadixTree, Prefix,ドル ExactMatchOnly = #False)
; The algorithm below expects at least one character to be searched for
If Prefix$ = ""
ProcedureReturn #Null
EndIf
; Use LCase() instead of UCase() to be compatiable to internal PB case insensitive compare (https://www.purebasic.fr/english/viewtopic.php?p=615646)
PrefixL$ = LCase(Prefix$)
*PrefixCursor.Character = @PrefixL$
*Node.RadixNode = *Tree\Node
While *Node
*NodeCursor.Character = @*Node\Chars$
; Trie-invariant: Each child starts with a unique character so we only need to look at that
;
If *PrefixCursor\c = *NodeCursor\c
; Node matches first char. Match further characters in the node
*NodeCursor + #CharSize
*PrefixCursor + #CharSize
While *NodeCursor\c And *PrefixCursor\c And *NodeCursor\c = *PrefixCursor\c
*NodeCursor + #CharSize
*PrefixCursor + #CharSize
Wend
If *PrefixCursor\c = 0
; prefix fully matched the node
If ExactMatchOnly And *NodeCursor\c <> 0
ProcedureReturn #Null
Else
ProcedureReturn *Node
EndIf
ElseIf *NodeCursor\c = 0
; all characters consumed. continue searching child nodes
*Node = *Node\Child
Else
; difference in part of the node. no match
ProcedureReturn #Null
EndIf
ElseIf *NodeCursor\c < *PrefixCursor\c
; Look at next node on same level
*Node = *Node\Next
Else
; No more matches possible since nodes are sorted
ProcedureReturn #Null
EndIf
Wend
; Reached the end of child-node list without a match but unmatched prefix chars remain
ProcedureReturn #Null
EndProcedure
Procedure RadixLookupValue(*Tree.RadixTree, Name$)
*Result.RadixNode = RadixFindPrefix(*Tree, Name,ドル #True)
If *Result
ProcedureReturn *Result\Value
Else
ProcedureReturn #Null
EndIf
EndProcedure
; Fills values of first and last match for the prefix
; This is useful if the underlying data is sorted too and the tree stores indexes rather than pointers
;
Procedure RadixFindRange(*Tree.RadixTree, Prefix,ドル *First.INTEGER, *Last.INTEGER)
*First\i = #Null
*Last\i = #Null
*Result.RadixNode = RadixFindPrefix(*Tree, Prefix,ドル #False)
If *Result
; All leaf nodes have non-empty values but nodes in the tree might too (if they match a full word)
; So the first non-empty node must be on the path of each first child node of *Result or *Result itself
*FirstNode.RadixNode = *Result
While *FirstNode
If *FirstNode\Value
*First\i = *FirstNode\Value
Break
EndIf
*FirstNode = *FirstNode\Child
Wend
*Last\i = *First\i ; in case only *Result matched
; Similarily the last non-empty node that matches must be on the path of each last child node
; Not on the first level though because the *Result\Next pointers here point to nodes unrelated to the match
*LastNode.RadixNode = *Result\Child
While *LastNode
; skip to last child
While *LastNode\Next <> #Null
*LastNode = *LastNode\Next
Wend
If *LastNode\Value
*Last\i = *LastNode\Value
; no Break. Any children are sorted AFTER this node so keep looking
EndIf
*LastNode = *LastNode\Child
Wend
If *First\i
ProcedureReturn #True
EndIf
EndIf
ProcedureReturn #False
EndProcedure
; Helper function
Procedure Radix_EnumerateNodes(*Node.RadixNode, List *Values())
While *Node
If *Node\Value
AddElement(*Values())
*Values() = *Node\Value
EndIf
Radix_EnumerateNodes(*Node\Child, *Values())
*Node = *Node\Next
Wend
EndProcedure
Procedure RadixEnumerateAll(*Tree.RadixTree, List *Values())
ClearList(*Values())
Radix_EnumerateNodes(*Tree\Node, *Values())
EndProcedure
Procedure RadixEnumeratePrefix(*Tree.RadixTree, Name,ドル List *Values())
ClearList(*Values())
*Result.RadixNode = RadixFindPrefix(*Tree, Name,ドル #False)
If *Result
; Do not look at the \Next nodes from the current node as we only want the current node and anything below it
If *Result\Value
AddElement(*Values())
*Values() = *Result\Value
EndIf
Radix_EnumerateNodes(*Result\Child, *Values())
EndIf
EndProcedure
; Helper function
Procedure Radix_AllocNode(*PrefixCursor.Character, *Value)
*Node.RadixNode = AllocateStructure(RadixNode)
*Node\Chars$ = PeekS(*PrefixCursor)
*Node\Value = *Value
ProcedureReturn *Node
EndProcedure
Procedure RadixInsert(*Tree.RadixTree, Name,ドル *Value)
If *Value = 0
ProcedureReturn
EndIf
; Use LCase() instead of UCase() to be compatiable to internal PB case insensitive compare (https://www.purebasic.fr/english/viewtopic.php?p=615646)
PrefixL$ = LCase(Name$)
*PrefixCursor.Character = @PrefixL$
If *Tree\Node = #Null
; Tree is empty so far
*Tree\Node = Radix_AllocNode(*PrefixCursor, *Value)
Else
; Similar to the algorithm in RadixFindPrefix()
*Node.RadixNode = *Tree\Node
*Parent.RadixNode = #Null
*Previous.RadixNode = #Null
While *Node
*NodeCursor.Character = @*Node\Chars$
If *PrefixCursor\c = *NodeCursor\c
*NodeCursor + #CharSize
*PrefixCursor + #CharSize
While *NodeCursor\c And *PrefixCursor\c And *NodeCursor\c = *PrefixCursor\c
*NodeCursor + #CharSize
*PrefixCursor + #CharSize
Wend
If *PrefixCursor\c = 0 And *NodeCursor\c = 0
; exact match. do not add duplicates
If *Node\Value = 0
*Node\Value = *Value
EndIf
ProcedureReturn
ElseIf *NodeCursor\c = 0
; all characters consumed.
If *Node\Child
; continue searching child nodes
*Parent = *Node
*Previous = #Null
*Node = *Node\Child
Else
; Add a child node
*Node\Child = Radix_AllocNode(*PrefixCursor, *Value)
ProcedureReturn
EndIf
ElseIf *PrefixCursor\c = 0
; Need to split the node to add the value (same prefix as the existing node)
*SplitNode.RadixNode = Radix_AllocNode(*NodeCursor, *Node\Value)
*SplitNode\Child = *Node\Child
*Node\Chars$ = Left(*Node\Chars,ドル Len(*Node\Chars$)-Len(*SplitNode\Chars$))
*Node\Value = *Value
*Node\Child = *SplitNode
ProcedureReturn
Else
; Need to split the node and add a separate sub-node because prefix and node chars differ
NodeChar.c = *NodeCursor\c ; save this as we re-alloc the string
*SplitNode.RadixNode = Radix_AllocNode(*NodeCursor, *Node\Value)
*SplitNode\Child = *Node\Child
*NewNode.RadixNode = Radix_AllocNode(*PrefixCursor, *Value)
*Node\Chars$ = Left(*Node\Chars,ドル Len(*Node\Chars$)-Len(*SplitNode\Chars$)) ; invalidates *NodeCursor !
*Node\Value = #Null
If NodeChar < *PrefixCursor\c
*Node\Child = *SplitNode
*SplitNode\Next = *NewNode
Else
*Node\Child = *NewNode
*NewNode\Next = *SplitNode
EndIf
ProcedureReturn
EndIf
ElseIf *NodeCursor\c < *PrefixCursor\c
; Look at next node on same level
*Previous = *Node
*Node = *Node\Next
Else
; No more matches possible since nodes are sorted. Add a new node before this one
*NewNode.RadixNode = Radix_AllocNode(*PrefixCursor, *Value)
*NewNode\Next = *Node
If *Previous
*Previous\Next = *NewNode
ElseIf *Parent
*Parent\Child = *NewNode
Else
*Tree\Node = *NewNode
EndIf
ProcedureReturn
EndIf
Wend
; Reached end of the node list. Add a new one here
; There must be a *Previous node as the no-child case is handled above already
*NewNode = Radix_AllocNode(*PrefixCursor, *Value)
*Previous\Next = *NewNode
EndIf
EndProcedure
;- ------ Debugging and Testing ------
CompilerIf #False
Procedure DebugRadixGraph_Recursive(*Node.RadixNode, Spaces,ドル Parent,ドル *Selected.RadixNode, InSelection)
While *Node
NodeName$ = "n" + Hex(*Node)
If *Node\Value
Label$ = PeekS(*Node\Value)
Else
Label$ = ""
EndIf
If *Node = *Selected
Debug Spaces$ + NodeName$ + ~" [label=\"" + Label$ + ~"\", color=\"red\"]"
ElseIf InSelection
Debug Spaces$ + NodeName$ + ~" [label=\"" + Label$ + ~"\", color=\"green\"]"
Else
Debug Spaces$ + NodeName$ + ~" [label=\"" + Label$ + ~"\"]"
EndIf
Debug Spaces$ + Parent$ + " -> " + NodeName$ + ~" [label=\"" + *Node\Chars$ + ~"\"]"
If *Node = *Selected
DebugRadixGraph_Recursive(*Node\Child, Spaces$ + " ", NodeName,ドル *Selected, #True)
Else
DebugRadixGraph_Recursive(*Node\Child, Spaces$ + " ", NodeName,ドル *Selected, InSelection)
EndIf
*Node = *Node\Next
Wend
EndProcedure
; Go here to visualize the graph: https://dreampuf.github.io/GraphvizOnline
Procedure DebugRadixGraph(*Tree.RadixTree, *Selected.RadixNode = #Null)
Debug "digraph G {"
Debug ~" root [label=\"\"]"
DebugRadixGraph_Recursive(*Tree\Node, " ", "root", *Selected, #False)
Debug "}"
EndProcedure
XIncludeFile "ConstantsData.pbi"
NewList Names.s()
Restore BasicFunctionConstants
Repeat
Read$ x$
If x$ = ""
Break
EndIf
AddElement(Names())
Names() = StringField(x,ドル 1, ",")
ForEver
; Test random insertions
RandomSeed(123)
RandomizeList(Names())
Define Tree.RadixTree
; Insert all function names
; ForEach Names()
; RadixInsert(Tree, Names(), PeekI(@Names())) ; store the actual name as the node value
; Next Names()
; Insert just a few names
ResetList(Names())
For i = 1 To 25
NextElement(Names())
RadixInsert(Tree, Names(), PeekI(@Names()))
Next i
; Test lookup/enumeration
NewList *Found()
;RadixEnumerateAll(Tree, *Found())
RadixEnumeratePrefix(Tree, "de", *Found())
ForEach *Found()
Debug PeekS(*Found())
Next
RadixFindRange(Tree, "de", @*First, @*Last)
Debug "First: " + PeekS(*First) + " Last: " + PeekS(*Last)
; Visualize the graph
;DebugRadixGraph(Tree, RadixFindPrefix(Tree, "de"))
CompilerEndIf
Loading...
举报
举报成功
我们将于2个工作日内通过站内信反馈结果给你!
请认真填写举报原因,尽可能描述详细。
请选择举报类型
取消
发送
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

简介

PureBasic是由Fantaisie Software所开发的商用BASIC程序语言及集成开发环境。特点是语法简单直接,不依赖运行时库,因此能编译出相当小巧的程序,包含命令列或GUI执行档、DLL等。而且不使用各系统的API,所以有高度的跨平台特性,支持Windows 32/64位元、Linux 32/64位元、Mac OS X、Amiga。
取消

发行版

暂无发行版

贡献者

全部

近期动态

不能加载更多了
编辑仓库简介
简介内容
主页
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/PureBasic/purebasic.git
git@gitee.com:PureBasic/purebasic.git
PureBasic
purebasic
PureBASIC
devel
点此查找更多帮助

搜索帮助

评论
仓库举报
回到顶部
登录提示
该操作需登录 Gitee 帐号,请先登录后再操作。
立即登录
没有帐号,去注册

AltStyle によって変換されたページ (->オリジナル) /