Genie 的 GLib TreeSearchFunc 委托
Genie's GLib TreeSearchFunc delegate
我写了一个代码,大概是GLib.Tree。但是不知道怎么用搜索方式
Valadoc 有一个例子,并且有效!
下面是我的代码:
[indent = 4]
def cmp (a: string, b: string): int
return strcmp (a, b)
init
var t = new Tree of string, string (cmp)
t.insert ("a", "aaa")
t.insert ("b", "bbb")
var needle = "A"
fun: TreeSearchFunc = def (k)
return strcmp (k.down(), needle.down())
var ret = t.search (fun)
错误!
error: The name down' does not exist in the context of
K'
再试一次:
fun: TreeSearchFunc of string = def (k)
错误!
error: 'GTreeSearchFunc' undeclared
TreeSearchFunc 说明:
public delegate int TreeSearchFunc (K key)
如果我想写一个TreeSearchFunc委托?
怎么做?
从实际的角度来看,您正在对 GLib.Tree 实施不区分大小写的搜索。使用 search_key()
函数可能会更好:
[indent = 4]
init
var tree = new Tree of ( string, string )( cmp )
tree.insert( "a", "aaa" )
tree.insert( "b", "bbb" )
result:string = tree.search_key( case_insensitive_compare, "A" )
print result
def cmp( a:string, b:string ):int
return strcmp( a, b )
def case_insensitive_compare ( a:string, b:string ):int
return strcmp( a.down(), b.down() )
由于您的问题特别要求 TreeSearchFunc
委托,因此代码为:
[indent = 4]
init
var tree = new Tree of ( string, string ).full( cmp, free, free )
tree.insert( "a", "aaa" )
tree.insert( "b", "bbb" )
result:string = tree.search( case_insensitive_search_for_a )
print result
def cmp( a:string, b:string ):int
return strcmp( a, b )
def case_insensitive_search_for_a ( k:string ):int
return strcmp( k.down(), "A".down() )
注意几点:
TreeSearchFunc
委托版本必须在其范围内包含搜索词 "A"。 Valadoc 中的示例使用了一个闭包,它还包括封闭范围,不幸的是,您还不能在 Genie 中这样做
- Valadoc 版本使用了
Tree.full
,所以我已经在 TreeSearchFunc
委托版本中展示了如何做到这一点。这是为了防止您不确定 Genie 中如何使用类型(泛型)参数
在您的示例中,您正确地尝试通过将闭包分配给变量来在 Genie 中使用闭包。这仅适用于 Vala 的开发版本,因此您必须下载并编译最新的 Vala。这应该在 Vala 0.32 发布时可用。如果您尝试在 Vala 中使用相同的方法,您将得到相同的错误。所以我认为问题出在 Vala 编译器中。目前 Genie 不支持在圆括号或大括号内定义匿名函数。如果您有兴趣添加支持,请查看 https://bugzilla.gnome.org/show_bug.cgi?id=760492
我写了一个代码,大概是GLib.Tree。但是不知道怎么用搜索方式
Valadoc 有一个例子,并且有效!
下面是我的代码:
[indent = 4]
def cmp (a: string, b: string): int
return strcmp (a, b)
init
var t = new Tree of string, string (cmp)
t.insert ("a", "aaa")
t.insert ("b", "bbb")
var needle = "A"
fun: TreeSearchFunc = def (k)
return strcmp (k.down(), needle.down())
var ret = t.search (fun)
错误!
error: The name
down' does not exist in the context of
K'
再试一次:
fun: TreeSearchFunc of string = def (k)
错误!
error: 'GTreeSearchFunc' undeclared
TreeSearchFunc 说明:
public delegate int TreeSearchFunc (K key)
如果我想写一个TreeSearchFunc委托? 怎么做?
从实际的角度来看,您正在对 GLib.Tree 实施不区分大小写的搜索。使用 search_key()
函数可能会更好:
[indent = 4]
init
var tree = new Tree of ( string, string )( cmp )
tree.insert( "a", "aaa" )
tree.insert( "b", "bbb" )
result:string = tree.search_key( case_insensitive_compare, "A" )
print result
def cmp( a:string, b:string ):int
return strcmp( a, b )
def case_insensitive_compare ( a:string, b:string ):int
return strcmp( a.down(), b.down() )
由于您的问题特别要求 TreeSearchFunc
委托,因此代码为:
[indent = 4]
init
var tree = new Tree of ( string, string ).full( cmp, free, free )
tree.insert( "a", "aaa" )
tree.insert( "b", "bbb" )
result:string = tree.search( case_insensitive_search_for_a )
print result
def cmp( a:string, b:string ):int
return strcmp( a, b )
def case_insensitive_search_for_a ( k:string ):int
return strcmp( k.down(), "A".down() )
注意几点:
TreeSearchFunc
委托版本必须在其范围内包含搜索词 "A"。 Valadoc 中的示例使用了一个闭包,它还包括封闭范围,不幸的是,您还不能在 Genie 中这样做- Valadoc 版本使用了
Tree.full
,所以我已经在TreeSearchFunc
委托版本中展示了如何做到这一点。这是为了防止您不确定 Genie 中如何使用类型(泛型)参数
在您的示例中,您正确地尝试通过将闭包分配给变量来在 Genie 中使用闭包。这仅适用于 Vala 的开发版本,因此您必须下载并编译最新的 Vala。这应该在 Vala 0.32 发布时可用。如果您尝试在 Vala 中使用相同的方法,您将得到相同的错误。所以我认为问题出在 Vala 编译器中。目前 Genie 不支持在圆括号或大括号内定义匿名函数。如果您有兴趣添加支持,请查看 https://bugzilla.gnome.org/show_bug.cgi?id=760492