Skip to content
This repository
Browse code

split trie.jl out of openlib.jl

  • Loading branch information...
commit 98c083bd5fce414e7426e3324d475d846cf22155 1 parent 4c0ed94
Mike Nolta authored March 15, 2012
81  extras/openlib.jl
... ...
@@ -1,73 +1,5 @@
1 1
 
2  
-# TODO: collapse single-child chains
3  
-type Trie{T}
4  
-    value::T
5  
-    children::HashTable{Char,Trie{T}}
6  
-    is_key::Bool
7  
-
8  
-    function Trie{T}()
9  
-        self = new()
10  
-        self.children = HashTable{Char,Trie{T}}()
11  
-        self.is_key = false
12  
-        self
13  
-    end
14  
-end
15  
-
16  
-Trie() = Trie{Any}()
17  
-
18  
-function assign{T}(t::Trie{T}, val::T, key::String)
19  
-    node = t
20  
-    for char in key
21  
-        if !has(node.children, char)
22  
-            node.children[char] = Trie{T}()
23  
-        end
24  
-        node = node.children[char]
25  
-    end
26  
-    node.is_key = true
27  
-    node.value = val
28  
-end
29  
-
30  
-function subtrie(t::Trie, prefix::String)
31  
-    node = t
32  
-    for char in prefix
33  
-        if !has(node.children, char)
34  
-            return nothing
35  
-        else
36  
-            node = node.children[char]
37  
-        end
38  
-    end
39  
-    node
40  
-end
41  
-
42  
-function has(t::Trie, key::String)
43  
-    node = subtrie(t, key)
44  
-    node != nothing && node.is_key
45  
-end
46  
-
47  
-get(t::Trie, key::String) = get(t, key, nothing)
48  
-function get(t::Trie, key::String, notfound)
49  
-    node = subtrie(t, key)
50  
-    if node != nothing && node.is_key
51  
-        return node.value
52  
-    end
53  
-    notfound
54  
-end
55  
-
56  
-function keys(t::Trie, prefix::String, found)
57  
-    if t.is_key
58  
-        push(found, prefix)
59  
-    end
60  
-    for (char,child) in t.children
61  
-        keys(child, strcat(prefix,char), found)
62  
-    end
63  
-end
64  
-keys(t::Trie, prefix::String) = (found=String[]; keys(t, prefix, found); found)
65  
-keys(t::Trie) = keys(t, "")
66  
-
67  
-function keys_with_prefix(t::Trie, prefix::String)
68  
-    st = subtrie(t, prefix)
69  
-    st != nothing ? keys(st,prefix) : []
70  
-end
  2
+load("trie.jl")
71 3
 
72 4
 type DLCache
73 5
     libnames::Trie{Bool}
@@ -100,11 +32,16 @@ function lookup(cache::DLCache, name::String)
100 32
     strcat(cache.prefix, name_with_suffix)
101 33
 end
102 34
 
103  
-const _jl_dlcache = DLCache()
  35
+const UNAME = strip(readall(stdout(`uname`)))
  36
+
  37
+if UNAME == "Linux"
  38
+    global const _jl_dlcache = DLCache()
  39
+else
  40
+    global const _jl_dlcache = nothing
  41
+end
104 42
 
105 43
 function openlib(name::String)
106  
-    libname = lookup(_jl_dlcache, name)
107  
-println(libname)
  44
+    libname = _jl_dlcache != nothing ? lookup(_jl_dlcache, name) : name
108 45
     dlopen(libname)
109 46
 end
110 47
 
70  extras/trie.jl
... ...
@@ -0,0 +1,70 @@
  1
+
  2
+type Trie{T}
  3
+    value::T
  4
+    children::HashTable{Char,Trie{T}}
  5
+    is_key::Bool
  6
+
  7
+    function Trie{T}()
  8
+        self = new()
  9
+        self.children = HashTable{Char,Trie{T}}()
  10
+        self.is_key = false
  11
+        self
  12
+    end
  13
+end
  14
+
  15
+Trie() = Trie{Any}()
  16
+
  17
+function assign{T}(t::Trie{T}, val::T, key::String)
  18
+    node = t
  19
+    for char in key
  20
+        if !has(node.children, char)
  21
+            node.children[char] = Trie{T}()
  22
+        end
  23
+        node = node.children[char]
  24
+    end
  25
+    node.is_key = true
  26
+    node.value = val
  27
+end
  28
+
  29
+function subtrie(t::Trie, prefix::String)
  30
+    node = t
  31
+    for char in prefix
  32
+        if !has(node.children, char)
  33
+            return nothing
  34
+        else
  35
+            node = node.children[char]
  36
+        end
  37
+    end
  38
+    node
  39
+end
  40
+
  41
+function has(t::Trie, key::String)
  42
+    node = subtrie(t, key)
  43
+    node != nothing && node.is_key
  44
+end
  45
+
  46
+get(t::Trie, key::String) = get(t, key, nothing)
  47
+function get(t::Trie, key::String, notfound)
  48
+    node = subtrie(t, key)
  49
+    if node != nothing && node.is_key
  50
+        return node.value
  51
+    end
  52
+    notfound
  53
+end
  54
+
  55
+function keys(t::Trie, prefix::String, found)
  56
+    if t.is_key
  57
+        push(found, prefix)
  58
+    end
  59
+    for (char,child) in t.children
  60
+        keys(child, strcat(prefix,char), found)
  61
+    end
  62
+end
  63
+keys(t::Trie, prefix::String) = (found=String[]; keys(t, prefix, found); found)
  64
+keys(t::Trie) = keys(t, "")
  65
+
  66
+function keys_with_prefix(t::Trie, prefix::String)
  67
+    st = subtrie(t, prefix)
  68
+    st != nothing ? keys(st,prefix) : []
  69
+end
  70
+

0 notes on commit 98c083b

Please sign in to comment.
Something went wrong with that request. Please try again.