在新结构中快速创建结构列表 [英] Quickly creating a list of structs in a new struct
问题描述
我正在尝试构建一些原始对象来存储图形:
I'm trying to build a few primitive objects to store a graph:
Vertex = Struct.new(:parent, :rank)
Graph = Struct.new(:vertexes, :edges)
Edge = Struct.new(:weight, :endpoints)
这里的想法是:endpoints
(和:parent
)是顶点,并且,自然地, Graph
存储顶点和边的列表.我试图进行设置,但遇到了麻烦:
The idea here is that :endpoints
(and :parent
) are Vertexes, and, naturally, Graph
stores lists of Vertexes and Edges. I tried to set this up, and I ran into trouble:
[47] pry(main)> t_graph = Graph.new()
=> #<struct Graph vertexes=nil, edges=nil>
[48] pry(main)> t_graph[:vertexes] = Array.new(Vertex.new())
TypeError: can't convert Vertex into Integer
from (pry):48:in `initialize'
另一方面,这可行:
[49] pry(main)> t_graph[:vertexes] = [Vertex.new(), Vertex.new()]
=> [#<struct Vertex parent=nil, rank=nil>,
#<struct Vertex parent=nil, rank=nil>]
在图形(或任何此类关系)中生成大量空顶点进行测试的有效方法是什么?
What's the efficient way to generate a mass of empty Vertexes in a Graph (or any such relation) for testing?
推荐答案
一种简单填充顶点列表的方法是使用 .times
,这看起来有点笨拙,但是会创建一个列表(我添加了:id
值):
One way to simply populate a list of Vertexes is to use .times
, which seems kind of clumsy, but creates a list (I added an :id
value):
t_verts = []
9.times do t_verts << Vertex.new end
[10] pry(main)> t_verts
=> [#<struct Vertex parent=nil, rank=nil, id=nil>,
#<struct Vertex parent=nil, rank=nil, id=nil>,
#<struct Vertex parent=nil, rank=nil, id=nil>,
#<struct Vertex parent=nil, rank=nil, id=nil>,
#<struct Vertex parent=nil, rank=nil, id=nil>,
#<struct Vertex parent=nil, rank=nil, id=nil>,
#<struct Vertex parent=nil, rank=nil, id=nil>,
#<struct Vertex parent=nil, rank=nil, id=nil>,
#<struct Vertex parent=nil, rank=nil, id=nil>]
这将创建不同的对象.
[11] pry(main)> t_verts.each do |vert|
[11] pry(main)* vert.id = rand
[11] pry(main)* end
=> [#<struct Vertex parent=nil, rank=nil, id=0.755245226082449>,
#<struct Vertex parent=nil, rank=nil, id=0.0167377598961559>,
#<struct Vertex parent=nil, rank=nil, id=0.759799975185007>,
#<struct Vertex parent=nil, rank=nil, id=0.613897002692335>,
#<struct Vertex parent=nil, rank=nil, id=0.0407847317079812>,
#<struct Vertex parent=nil, rank=nil, id=0.549475744759297>,
#<struct Vertex parent=nil, rank=nil, id=0.571876769381891>,
#<struct Vertex parent=nil, rank=nil, id=0.270800829904956>,
#<struct Vertex parent=nil, rank=nil, id=0.814754555208641>]
要将这些整体添加到图中,可以使用Simone的方法将顶点列表添加到图中:
To add these en masse to a Graph, Simone's method can be used to add the list of Vertexes to the Graph:
def add_vertex(new_verts) # Adds or updates vertexes non-destructively
new_verts.each do |new_vert|
# first case, redundant insert
if self.vertexes.include?(new_vert)
warn "Vertex already exists in graph, no replacement done"
# second case, update
elsif not (update_vert = self.vertexes.select { |vert| vert.id == new_vert.id }).empty?
self.vertexes[self.vertexes.index(update_vert)] = new_vert
# third case, append new
else
self.vertexes ||= []
self.vertexes << vertex
end
end
end
似乎我需要构建一个构造器方法,该方法从邻接列表和权重列表构建Graph;我看不出有一个简单的方法可以一步一步完成.
It looks like I need to build a constructor method that builds the Graph from an adjacency list and weight list; I don't see a simple way to do this in one big step.
这篇关于在新结构中快速创建结构列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!