【问题标题】:A dictionary data structure in the Logo language (key/value store)Logo 语言中的字典数据结构(键/值存储)
【发布时间】:2015-02-10 12:25:01
【问题描述】:

我想知道是否存在可用于 Logo 语言的一流字典数据结构的实现?我在 UCB 徽标或 FMS 徽标的文档中没有看到可用的。我看到有属性列表,但它们似乎不是一流的,而且看起来它们是线性查找关联列表。我正在寻找基于哈希或树的东西。似乎那里有很多历史标志,也许有人已经实现了这样的东西。另外,如果有人知道徽标代码的存储库,我将不胜感激指针(即徽标的 CPAN)。

【问题讨论】:

  • 请定义您所说的一流。这不是我熟悉的术语,或者至少在技术意义上不是。 “一流”是指实现具有接近 O(1) 查找时间的哈希表吗?
  • 您打算为表中的键使用什么类型的数据?数字?字符串?
  • 对于第一类,能够创建新的匿名字典并将其传递给过程。 en.wikipedia.org/wiki/First-class_citizen 与 ucbLogo 和 FMSLogo 中的属性列表进行比较和对比:fmslogo.sourceforge.net/manual/property-lists.html
  • 顺便说一句,我打算建议,如果没有使用数组来创建自己的哈希表,这在技术上应该是可行的,您可以动态创建名称为"MyTable_MyKey 的变量,因为解释器可能将其变量存储/查找实现为某种哈希表。但在推荐之前,我下载了 UCBLogo 源来查看它,在我看来,变量可能只是存储在链表中。不过,我可能错了,因为我在导航代码时遇到了一点困难。
  • 为了不重新发明轮子,我只是想知道是否有人在 Logo 中有一个现有的字典实现,它不依赖于任何解释器的内部。当然,我们可以(在语言中)创建一组过程来实现字典接口,使用列表作为树/特里树,或者使用数组作为哈希表。

标签: dictionary ucb-logo logo-lang


【解决方案1】:

这是一个基于列表的真正简单的纯功能树实现。键可以是单词或数字。空列表是空字典。它不是自我平衡,因此在使用“dict_add”进行插入后,您必须自己调用“rebalance”。不要经常这样做,因为实施的“重新平衡”需要 O(n) 时间。一个过程“测试”会稍微练习一下。

to dict_lookup :dict :key
   if (empty? :dict) [output []]
   localmake "pair first :dict
   if (empty? :pair) [output []]
   localmake "test_k first :pair
   if (equal? :key :test_k) [output last :pair]
   output dict_lookup (ifelse (before? :key :test_k) [left_tree :dict] [right_tree :dict]) :key
end

to left_tree :dict
    output item 2 :dict
end

to right_tree :dict
    output item 3 :dict
end

to dict_add :dict :key :value
    localmake "new_pair (list :key :value)
    if (empty? :dict) [output (list :new_pair [] [])]
    localmake "old_pair first :dict 
    localmake "old_key first :old_pair

    localmake "l_tree left_tree :dict
    localmake "r_tree right_tree :dict

    if (equal? :key :old_key) [output (list :new_pair :l_tree :r_tree)] 
    ifelse (before? :key :old_key) [
        localmake "l_tree dict_add l_tree :key :value] [
        localmake "r_tree dict_add r_tree :key :value]
    output (list :old_pair :l_tree :r_tree)
end

to in_order :dict
    if (empty? :dict) [output []]
    output (sentence (in_order (left_tree :dict)) 
                     (list (first :dict))
                     (in_order (right_tree :dict)))
end

to draw_tree :dict :size
    if (empty? :dict) [label [[]] stop]
    rt 50 fd :size lt 50
        draw_tree (left_tree :dict) :size * 0.7 
    rt 50 bk :size lt 50
    label (first :dict)
    lt 50 fd :size rt 50 
        draw_tree (right_tree :dict) :size * 0.7
    lt 50 bk :size rt 50
end

to list_to_tree :l
    if (empty? :l) [output []]
    localmake "half int (count :l)/2
    localmake "firsts take :half :l
    localmake "rests drop :half :l
    output (list (first :rests) (list_to_tree :firsts) (list_to_tree bf :rests))
end

to take :n :l
    if (or (empty? :l) (:n < 1)) [output []]
    output fput (first :l) take :n-1 bf :l
end

to drop :n :l
    if (:n < 1) [output :l]
    output drop :n-1 bf :l
end

to rebalance :dict
   output list_to_tree in_order :dict
end

to testing
    localmake "test_dict []
    localmake "items [[apple 1] [ball 2] [banana 3] [cat 4] [zebra 5] 
                      [toy 6] [yak 7] [jump 8]]
    foreach :items [make "test_dict (dict_add :test_dict (first ?) (first bf ?))]
    cs pu
    lt 60 fd 500 setheading 180 pd 
        draw_tree :test_dict 200
    pu lt 90 fd 700 setheading 180 pd 
        draw_tree (rebalance :test_dict) 160
end

【讨论】:

    【解决方案2】:

    FMSLogo 中的属性列表确实不是一等公民。但是,属性列表“名称”是一流的。 FMSLogo 6.32.0 版也将属性列表实现为 AVL 树。这记录在这里:http://sourceforge.net/p/fmslogo/feature-requests/94/

    【讨论】:

      猜你喜欢
      • 2013-12-27
      • 2019-04-08
      • 1970-01-01
      • 2017-04-06
      • 2013-03-22
      • 1970-01-01
      • 1970-01-01
      • 2013-01-14
      相关资源
      最近更新 更多