标签: recursive-datastructures

Java Generics Type带有递归Hashmap的安全警告

我正在使用一个递归的hashmaps树,特别是Hashmap map,其中Object是对另一个Hashmap的引用,依此类推.这将通过递归算法传递:

foo(String filename, Hashmap<String, Object> map)
{
    //some stuff here
    for (Entry<String, Object> entry : map.entrySet()) 
    {
       //type warning that must be suppressed
       foo(entry.getKey(), (HashMap<String, Object>)entry.getValue());
    }
}
Run Code Online (Sandbox Code Playgroud)

我知道肯定Object是类型,Hashmap<String, Object>但我很恼火,我必须使用抑制警告@SuppressWarnings("unchecked").

我会对一个解决方案感到满意,该解决方案可以执行assert(/*entry.getValue() is of type HashMap<String, Object>*/)或者抛出异常.我沿着Generics路线进行编译类型安全,如果我压制警告那么它就会失败.

谢谢你的意见,ksb

java generics hashmap recursive-datastructures type-safety

5
推荐指数
2
解决办法
2668
查看次数

Python和F#中的递归变量定义(也可能是OCaml)

鉴于这些F#类型声明......

type Message =
    | MessageA
    | MessageB
    | MessageC
    | MessageD

type State = {
    Name:string
    NextStateMap: Map<Message,State>
}
Run Code Online (Sandbox Code Playgroud)

...对这个特定的状态机有一个同样富有表现力的定义......

let rec state0 = { Name = "0"; NextStateMap = Map.ofList [ (MessageA,state1); (MessageB,state2)] }
    and state1 = { Name = "1"; NextStateMap = Map.ofList [ (MessageB,state3)] }
    and state2 = { Name = "2"; NextStateMap = Map.ofList [ (MessageA,state3)] }
    and state3 = { Name = "3"; NextStateMap = Map.ofList [ (MessageC,state4)] }
    and state4 = { Name …
Run Code Online (Sandbox Code Playgroud)

python f# ocaml recursive-datastructures

5
推荐指数
1
解决办法
1236
查看次数

Operator []在MultiDimensional Arrays中重载c ++

当我打电话:a7 [0] [1] [100];

我能够在operator []中获得第一个索引'0'但是因为索引I将无法以递归方式获得其他索引值1和100.我怎么能够使用operator []来获得递归的folowing索引值.在这个3维数组的例子中,operator []仅对第一个维度"0"只调用一次.

我的示例代码:

template <class T, unsigned ... RestD> struct array;

template <class T, unsigned PrimaryD>
struct array <T, PrimaryD> {
    typedef T type[PrimaryD];

    type data;

    T& operator[] (unsigned i) {
        return data[i];
    }
}; 

template <class T, unsigned PrimaryD, unsigned ... RestD>
struct array <T, PrimaryD, RestD...> {
    typedef typename array<T, RestD...>::type OneDimensionDownArrayT;
    typedef OneDimensionDownArrayT type[PrimaryD];

    type data;

    OneDimensionDownArrayT& operator[] (int i) {
        OneDimensionDownArrayT& a = data[i];
        return a;
    }
}; 

int main …
Run Code Online (Sandbox Code Playgroud)

c++ operator-overloading recursive-datastructures multidimensional-array variadic-templates

5
推荐指数
1
解决办法
2122
查看次数

为什么coq互感类型必须具有相同的参数?

按照亚瑟的建议,我改变了我的Fixpoint关系,Inductive建立了一种"建立"游戏之间不同比较而不是"向下钻取" 的相互关系.

但现在我收到一条全新的错误消息:

Error: Parameters should be syntactically the same for each inductive type.
Run Code Online (Sandbox Code Playgroud)

我认为错误信息是说我需要所有这些互感定义的相同精确参数.

我意识到有一些简单的黑客来解决这个问题(未使用的虚拟变量,包含所有内容的长函数类型forall),但我不明白为什么我应该这样做.

有人可以解释这种对互感类型的限制背后的逻辑吗?是否有更优雅的方式来写这个?这种限制是否也意味着相互之间的归纳调用必须都使用相同的参数(在这种情况下,我不知道任何黑客可以保存大量的代码重复)?

(所有类型和术语的定义,例如compare_quest,game,g1side等,与我在第一个问题中的定义相同.

Inductive gameCompare (c : compare_quest) : game -> game -> Prop :=
 | igc : forall g1 g2 : game,
    innerGCompare (nextCompare c) (compareCombiner c) (g1side c) (g2side c) g1 g2 ->
    gameCompare c g1 g2
with innerGCompare (next_c : compare_quest) (cbn : combiner) (g1s g2s : side)
    : game -> game …
Run Code Online (Sandbox Code Playgroud)

comparison game-theory recursive-datastructures coq induction

5
推荐指数
1
解决办法
1042
查看次数

VBA中的多维Dictionary类

这篇文章一半是分享解决方案,一半是询问是否有更好的方法来做到这一点。

问题:如何在VBA中构建多维字典。

似乎有人在寻找一个,但周围没有明显的简洁解决方案,所以我想出了一些代码,如下所示。

特定情况:将 ADO 记录集转换为字典,其中几列组成一行的唯一键。除非您想出一个键来连接构成唯一键的所有列,否则将多条记录添加到同一个字典会失败。

一般情况:在对象层次结构中对树结构进行建模,其中层次结构中同一级别的每个节点上的分支数量可能不同。

下面的代码解决了这两个问题。性能未经测试,但 VBA 脚本库的 Dictionary 类显然是用哈希表索引的,我见过用它构建的非常大的系统,所以我怀疑性能会成为问题。也许那里的一个巨大的大脑会在这方面纠正我。

将其放入名为 multiDictionary 的 VBA 类中:

Option Explicit

' generic multi-dimensional dictionary class
' each successive higher dimension dictionary is nested within a lower dimension dictionary
Private pDictionary As Dictionary
Private pDimensionKeys() As Variant

Private Const reservedItemName As String = "multiItem"

Public Function add(value As Variant, ParamArray keys() As Variant)
    Dim searchDictionary As Dictionary
    Dim newDictionary As Dictionary
    Dim count As Long
    If pDictionary Is Nothing Then Set pDictionary …
Run Code Online (Sandbox Code Playgroud)

excel vba recursive-datastructures hierarchical-data data-structures

5
推荐指数
0
解决办法
9772
查看次数

在相同的操作中,nodejs中的有限函数调用?

我目前正在进行一些科学计算,只要至少有一个参数为false,我的基本计算循环就会一遍又一遍地使用递归调用执行.

目前我的nodejs服务器停止在大约905 - 915递归函数调用.

奇怪的是,它不会崩溃,也不会输出任何错误.它只是停止做任何事情 - >没有更多的日志等.

这是节点的一些保护行为,以避免溢出?

我正在努力解决这个问题几周,同时试图用尽可能智能的软件来限制"循环".

感谢您的帮助和建议.问候Noa.

根据要求,我提供了一些我的实际代码的抽象

我希望这有帮助.我不能把原始代码放在这里,因为它包含超过1.5k的行 - 需要检查很多.但是下面的例子涵盖了递归调用背后的基本逻辑.

// Contains objects which contain an array
// which represents the amount of the ex_obj terms
var amount = {
  a:[10,10],
  b:[7.5,7.5],
  c:[2.5,2.5,2.5,2.5]
}

// Contains objects, which contain an array of other objects
// that represent some selection
// Each object in an array consists of different values per attribute per 1 amount
var ex_obj = {
  a: [
    {aa: 1.41, ab: 0.143, ac: 0.5}, …
Run Code Online (Sandbox Code Playgroud)

recursion recursive-datastructures node.js

5
推荐指数
1
解决办法
131
查看次数

在已经是Functor的数据类型上使用`Fix`的递归方案?

仍在我的文本编辑器Rasa上工作

目前,我正在构建用于跟踪视口/拆分的系统(类似于vim拆分)。对我来说,将这种结构表示为一棵树似乎很自然:

data Dir = Hor
         | Vert
         deriving (Show)

data Window a =
  Split Dir SplitInfo (Window a) (Window a)
    | Single ViewInfo a
    deriving (Show, Functor, Traversable, Foldable)
Run Code Online (Sandbox Code Playgroud)

效果很好,我将Views 存储在树中,然后可以在它们上遍历/ fmap来更改它们,这也与镜头包很好地吻合!

我最近一直在学习递归方案,这似乎是一个合适的用例,因为树是递归的数据结构。

我设法弄清楚了,以构建Fixpoint版本:

data WindowF a r =
  Split Dir SplitInfo r r
    | Single ViewInfo a
    deriving (Show, Functor)

type Window a = Fix (WindowF a)
Run Code Online (Sandbox Code Playgroud)

但是,现在Functor实例已被r; 用尽。

我尝试了几种

deriving instance Functor Window
Run Code Online (Sandbox Code Playgroud)

但是它很令人吃惊,因为window是类型的同义词。

和:

newtype Window a = …
Run Code Online (Sandbox Code Playgroud)

haskell functor recursive-datastructures recursion-schemes fixpoint-combinators

5
推荐指数
1
解决办法
384
查看次数

像树一样的递归数据类型,如Avro模式

阅读https://avro.apache.org/docs/current/spec.html表示模式必须是以下之一:

  • JSON字符串,命名已定义的类型。
  • JSON对象,其形式为: {"type": "typeName" ...attributes...}其中typeName是原始类型名称或派生类型名称,如下所示。允许将本文档中未定义的属性用作元数据,但不得影响序列化数据的格式。
  • JSON数组,表示嵌入式类型的并集。

我想要一个使用树的递归定义来描述树的架构:

  • 具有值(例如整数)和树列表​​(子代)的节点
  • 有值的叶子

我最初的尝试看起来像:

{
  "name": "Tree",
  "type": [
    {
      "name": "Node",
      "type": "record",
      "fields": [
        {
          "name": "value",
          "type": "long"
        },
        {
          "name": "children",
          "type": { "type": "array", "items": "Tree" }
        }
      ]
    },
    {
      "name": "Leaf",
      "type": "record",
      "fields": [
        {
          "name": "value",
          "type": "long"
        }
      ]
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

但是Avro编译器拒绝此操作,抱怨没有类型{"name":"Tree","type":[{"name":"Node"...。似乎Avro不喜欢顶层的联合类型。我猜测这属于上述规则“架构必须是.. JSON对象..其中typeName是原始类型或派生类型名称之一”。我不确定“派生类型名称”是什么。起初,我认为它与“复杂类型”相同,但其中包括联合类型。

无论如何,将其更改为更复杂的定义:

{
  "name": "Tree",
  "type": "record",
  "fields": [{
    "name": "ctors",
    "type": [ …
Run Code Online (Sandbox Code Playgroud)

protocols recursive-datastructures avro

5
推荐指数
2
解决办法
1244
查看次数

Haskell AST Annotation with Fix

我正在努力在Haskell中创建一个AST.我想添加不同的注释,例如类型和位置信息,所以我最终使用了fixplate.但是,我在网上找不到任何例子,我遇到了一些困难.

我按照fixplate的推荐设置了我的AST(有些条纹):

data ProgramF a
  = Unary a
          Operator
  | Number Int
  | Let { bindings :: [(Identifier, a)]
        , body :: a }

type Program = Mu ProgramF
Run Code Online (Sandbox Code Playgroud)

接下来添加标签我创建了另一种类型,以及一个基于树遍历添加标签的函数.

type LabelProgram = Attr ProgramF PLabel

labelProgram :: Program -> LabelProgram
labelProgram =
  annMap (PLabel . show . fst) . (snd . synthAccumL (\i x -> (i + 1, (i, x))) 0)
Run Code Online (Sandbox Code Playgroud)

但是,除此之外,我遇到了一些问题.例如,我正在尝试编写一个对AST进行一些转换的函数.因为它需要一个标签来运行,我已经制作了类型LabelProgram -> Program,但我认为我在做错了.下面是一部分函数的片段(一个更简单的部分):

toANF :: LabelProgram -> Program
toANF (Fix (Ann label (Let {bindings, …
Run Code Online (Sandbox Code Playgroud)

haskell abstract-syntax-tree recursive-datastructures fixpoint-combinators

5
推荐指数
1
解决办法
187
查看次数

如何使用Map <String,T>在java中创建递归的树状数据结构?

在尝试创建遵循模式的数据结构时,我有一个思维模块:

Map<String, T>是一个主要的构建块,TMap<String, T>或作为终端运营商List<String>.是否有可能构建类似的东西Java,这个想法来自像F#或类似的函数式语言Haskell.

我搜索过,SO但到目前为止找不到任何符合我想法的东西Java.

java recursive-datastructures

5
推荐指数
1
解决办法
337
查看次数