dsf12313 2019-08-05 13:16
浏览 112

奇怪的切片行为

I am trying to implement BFS algorithm for finding all paths (from src and dest) in a graph. I am using a slice to simulate a queue, but the slice gets corrupted (the append didn't work as expected) when I append to it more than one elements in the for cycle. I have no idea why. I am new in GoLand

// GetPathsFromCache retrieve information from loaded jsons in the cache
func (cache *ModelsDataCache) GetPathsFromCache(modelUrn, selectedElement, targetType, authToken string, modelJSONs *ModelJSONs) []systemdetection.GraphPath {

    result := make([]systemdetection.GraphPath, 0)

    //create the queue which stores the paths
    q := make([]Path, 0)

    //Path to store the current path
    var currentPath Path
    currentPath.path = append(currentPath.path, selectedElement)
    q = append(q, currentPath)


    for len(q) > 0 {


        currentPath = q[0] //get top
        q = q[1:]


        lastInThePath := currentPath.path[len(currentPath.path)-1]
        connectedToTheCurrent := cache.GetConnectionsFromCache(modelUrn, lastInThePath, authToken, modelJSONs)

        lastInPathType := modelJSONs.Elements[lastInThePath].Type

        if lastInPathType == targetType {
            //cache.savePath(currentPath, &result)

            var newPath systemdetection.GraphPath
            newPath.Target = lastInThePath
            newPath.Path = currentPath.path[:]
            newPath.Distance = 666
            result = append(result, newPath)
        }


        for _, connected := range connectedToTheCurrent {
            if cache.isNotVisited(connected, currentPath.path) {


                var newPathN Path

                newPathN.path = currentPath.path
                newPathN.path = append(newPathN.path, connected)

                q = append(q, newPathN)
            }
        }

    }

    return result

}
  • 写回答

1条回答 默认 最新

  • dongzhuang6177 2019-08-05 15:58
    关注

    You may not be using make correctly. make for a slice takes two arguments, length and (optional) capacity:

    make([]T, len, cap)
    

    Len is the starting number of elements it contains, capacity is the number of elements it could contain without expanding. More on A Tour of Go.

    Visually:

    make([]string, 5) #=> ["", "", "", "", ""]
    make([]string, 0, 5) #=> [] (but the underlying array can hold 5 elements)
    

    append adds to the end of the array, so following the same example:

    arr1 := make([]string, 5) #=> ["", "", "", "", ""]
    arr1 = append(arr1, "foo") #=> ["", "", "", "", "", "foo"] 
    
    arr2 := make([]string, 0, 5) #=> []
    arr2 = append(arr2, "foo") #=> ["foo"] (underlying array can hold 4 more elements before expanding)
    

    You are creating slices of a length and then appending to the end, meaning the first N elements of your slice are the zero value. Change your make([]T, N) to make([]T, 0, N).

    Here's a link to the Go Playground showing the difference: https://play.golang.org/p/ezoUGPHqSRj

    评论

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用