Permalink
Browse files

split trie.jl out of openlib.jl

  • Loading branch information...
1 parent 4c0ed94 commit 98c083bd5fce414e7426e3324d475d846cf22155 @nolta nolta committed Mar 15, 2012
Showing with 79 additions and 72 deletions.
  1. +9 −72 extras/openlib.jl
  2. +70 −0 extras/trie.jl
View
81 extras/openlib.jl
@@ -1,73 +1,5 @@
-# TODO: collapse single-child chains
-type Trie{T}
- value::T
- children::HashTable{Char,Trie{T}}
- is_key::Bool
-
- function Trie{T}()
- self = new()
- self.children = HashTable{Char,Trie{T}}()
- self.is_key = false
- self
- end
-end
-
-Trie() = Trie{Any}()
-
-function assign{T}(t::Trie{T}, val::T, key::String)
- node = t
- for char in key
- if !has(node.children, char)
- node.children[char] = Trie{T}()
- end
- node = node.children[char]
- end
- node.is_key = true
- node.value = val
-end
-
-function subtrie(t::Trie, prefix::String)
- node = t
- for char in prefix
- if !has(node.children, char)
- return nothing
- else
- node = node.children[char]
- end
- end
- node
-end
-
-function has(t::Trie, key::String)
- node = subtrie(t, key)
- node != nothing && node.is_key
-end
-
-get(t::Trie, key::String) = get(t, key, nothing)
-function get(t::Trie, key::String, notfound)
- node = subtrie(t, key)
- if node != nothing && node.is_key
- return node.value
- end
- notfound
-end
-
-function keys(t::Trie, prefix::String, found)
- if t.is_key
- push(found, prefix)
- end
- for (char,child) in t.children
- keys(child, strcat(prefix,char), found)
- end
-end
-keys(t::Trie, prefix::String) = (found=String[]; keys(t, prefix, found); found)
-keys(t::Trie) = keys(t, "")
-
-function keys_with_prefix(t::Trie, prefix::String)
- st = subtrie(t, prefix)
- st != nothing ? keys(st,prefix) : []
-end
+load("trie.jl")
type DLCache
libnames::Trie{Bool}
@@ -100,11 +32,16 @@ function lookup(cache::DLCache, name::String)
strcat(cache.prefix, name_with_suffix)
end
-const _jl_dlcache = DLCache()
+const UNAME = strip(readall(stdout(`uname`)))
+
+if UNAME == "Linux"
+ global const _jl_dlcache = DLCache()
+else
+ global const _jl_dlcache = nothing
+end
function openlib(name::String)
- libname = lookup(_jl_dlcache, name)
-println(libname)
+ libname = _jl_dlcache != nothing ? lookup(_jl_dlcache, name) : name
dlopen(libname)
end
View
70 extras/trie.jl
@@ -0,0 +1,70 @@
+
+type Trie{T}
+ value::T
+ children::HashTable{Char,Trie{T}}
+ is_key::Bool
+
+ function Trie{T}()
+ self = new()
+ self.children = HashTable{Char,Trie{T}}()
+ self.is_key = false
+ self
+ end
+end
+
+Trie() = Trie{Any}()
+
+function assign{T}(t::Trie{T}, val::T, key::String)
+ node = t
+ for char in key
+ if !has(node.children, char)
+ node.children[char] = Trie{T}()
+ end
+ node = node.children[char]
+ end
+ node.is_key = true
+ node.value = val
+end
+
+function subtrie(t::Trie, prefix::String)
+ node = t
+ for char in prefix
+ if !has(node.children, char)
+ return nothing
+ else
+ node = node.children[char]
+ end
+ end
+ node
+end
+
+function has(t::Trie, key::String)
+ node = subtrie(t, key)
+ node != nothing && node.is_key
+end
+
+get(t::Trie, key::String) = get(t, key, nothing)
+function get(t::Trie, key::String, notfound)
+ node = subtrie(t, key)
+ if node != nothing && node.is_key
+ return node.value
+ end
+ notfound
+end
+
+function keys(t::Trie, prefix::String, found)
+ if t.is_key
+ push(found, prefix)
+ end
+ for (char,child) in t.children
+ keys(child, strcat(prefix,char), found)
+ end
+end
+keys(t::Trie, prefix::String) = (found=String[]; keys(t, prefix, found); found)
+keys(t::Trie) = keys(t, "")
+
+function keys_with_prefix(t::Trie, prefix::String)
+ st = subtrie(t, prefix)
+ st != nothing ? keys(st,prefix) : []
+end
+

0 comments on commit 98c083b

Please sign in to comment.