Idiomatic way of implementing nested matrices in golang

huangapple go评论85阅读模式
英文:

Idiomatic way of implementing nested matrices in golang

问题

  1. 我正在尝试在内存中表示一个超图。除了嵌套矩阵之外,还有没有更好的数据结构来完成这个任务?嵌套矩阵是一种可以同时包含“原生”类型元素(比如简单起见,我们假设是int)和矩阵的矩阵。

  2. 这是这样一个矩阵的开始。代码中有没有什么不完善之处,以使其看起来更符合惯用方式?如何使其看起来更符合惯用方式?

代码:

package main

import "fmt"

type Matricial interface {
    Put(interface{}, ...int)
    Get(...int) interface{}
}

type Matrix struct {
    Matricial
    values map[int]interface{}
}

func NewMatrix() *Matrix {
    m := &Matrix{}
    m.values = make(map[int]interface{})
    return m
}

func (m *Matrix) Set(atom interface{}, pos ...int) {
    firstdim := pos[0]
    if val, ok := m.values[firstdim]; ok {
        fmt.Println("map key exists", val)
        switch converted := val.(type) {
        case int:
            m.values[firstdim] = converted
        default:
            fmt.Println("ERR: unknown type: %T", val)
        }
    } else {
        if len(pos[1:]) > 0 {
            newm := NewMatrix()
            m.values[firstdim] = newm
            newm.Set(atom, pos[1:]...)
        } else {
            m.values[firstdim] = atom
        }
    }
}
func (m *Matrix) Get(pos ...int) interface{} {
    if len(pos) == 1 {
        return m.values[pos[0]]
    } else {
        switch accessor := m.values[pos[0]].(type) {
        case Matricial:
            return accessor.Get(pos[1:]...)
        default:
            return nil
        }
    }
    return nil
}

func main() {
    m := NewMatrix()
    m.Set(42, 2, 3, 4)
    m.Set(43, 0)
    fmt.Println(m.Get(2, 3))
    fmt.Println(m.Get(2, 3, 4))
    fmt.Println(m.Get(0))
}

这个数据结构必须允许将超边连接到其他超边(即将超边处理为节点)。

英文:
  1. I am trying to represent a hypergraph in memory. Are there any better data structures for this task beside nested matrices? A nested matrix is a matrix which can have elements of both the "native" type (let's say int for the sake of simplicity) and matrices.

  2. This is the beginning of such a matrix. Are there any rough edges in the code, to make it look more idiomatic? How to make it look more idiomatic?

The code:

package main

import "fmt"

type Matricial interface {
	Put(interface{}, ...int)
	Get(...int) interface{}
}

type Matrix struct {
	Matricial
	values map[int]interface{}
}

func NewMatrix() *Matrix {
	m := &Matrix{}
	m.values = make(map[int]interface{})
	return m
}

func (m *Matrix) Set(atom interface{}, pos ...int) {
	firstdim := pos[0]
	if val, ok := m.values[firstdim]; ok {
		fmt.Println("map key exists", val)
		switch converted := val.(type) {
		case int:
			m.values[firstdim] = converted
		default:
			fmt.Println("ERR: unknown type: %T", val)
		}
	} else {
		if len(pos[1:]) > 0 {
			newm := NewMatrix()
			m.values[firstdim] = newm
			newm.Set(atom, pos[1:]...)
		} else {
			m.values[firstdim] = atom
		}
	}
}
func (m *Matrix) Get(pos ...int) interface{} {
	if len(pos) == 1 {
		return m.values[pos[0]]
	} else {
		switch accessor := m.values[pos[0]].(type) {
		case Matricial:
			return accessor.Get(pos[1:]...)
		default:
			return nil
		}
	}
	return nil
}

func main() {
	m := NewMatrix()
	m.Set(42, 2, 3, 4)
	m.Set(43, 0)
	fmt.Println(m.Get(2, 3))
	fmt.Println(m.Get(2, 3, 4))
	fmt.Println(m.Get(0))
}

The data structure must allow connecting hyperedges with other hyperedges (i.e. handling hyperedges as though they were nodes).

答案1

得分: 1

  1. 一个嵌套矩阵(采用您对该术语的定义)似乎是超图的一个合理表示,无论如何,我对您的应用程序了解得不多。一个示例Go实现是Rosetta code上的power set示例。

  2. 嵌入接口不是惯用的做法。例如,如果您将Matricial的Put方法重命名为Set(我认为这是您的意思),那么您可以删除Matrix的Matricial字段,您的程序将产生相同的输出。

英文:
  1. A nested matrix (adopting your definition of the term) seems a reasonable representation for hypergraph, not knowing anything more about your application anyway. An example Go implementation is the power set example at Rosetta code.

  2. It is not idiomatic to embed an interface. For example, if you rename the Put method of Matricial to be Set, which is what I think you meant, then you can just delete the Matricial field of Matrix and your program produces the same output.

huangapple
  • 本文由 发表于 2013年10月4日 04:20:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/19168535.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定