dongzhi7763 2018-11-13 11:47
浏览 31
已采纳

切片别名

How to check whether two slices are backed up by the same array?

For example:

a := []int{1, 2, 3}
b := a[0:1]
c := a[2:3]

alias(b, c) == true

How should alias look like?

  • 写回答

2条回答 默认 最新

  • duangua5308 2018-11-13 12:16
    关注

    In general you can't tell if the backing array is shared between 2 slices, because using a full slice expression, one might control the capacity of the resulting slice, and then there will be no overlap even when checking the capacity.

    As an example, if you have a backing array with 10 elements, a slice may be created that only contains the first 2 elements, and its capacity might be 2. And another slice may be create that only holds its last 2 elements, its capacity again being 2.

    See this example:

    a := [10]int{}
    
    x := a[0:2:2]
    y := a[8:10:10]
    
    fmt.Println("len(x) = ", len(x), ", cap(x) = ", cap(x))
    fmt.Println("len(y) = ", len(y), ", cap(y) = ", cap(y))
    

    The above will print that both lengths and capcities of x and y are 2. They obviously have the same backing array, but you won't have any means to tell that.


    Edit: I've misunderstood the question, and the following describes how to tell if (elements of) 2 slices overlap.

    There is no language support for this, but since slices have a contiguous section of some backing array, we can check if the address range of their elements overlap.

    Unfortunately pointers are not ordered in the sense that we can't apply the < and > operators on them (there are pointers in Go, but there is no pointer arithmetic). And checking if all the addresses of the elements of the first slice matches any from the second, that's not feasible.

    But we can obtain a pointer value (an address) as a type of uintptr using the reflect package, more specifically the Value.Pointer() method (or we could also do that using package unsafe, but reflect is "safer"), and uintptr values are integers, they are ordered, so we can compare them.

    So what we can do is obtain the addresses of the first and last elements of the slices, and by comparing them, we can tell if they overlap.

    Here's a simple implementation:

    func overlap(a, b []int) bool {
        if len(a) == 0 || len(b) == 0 {
            return false
        }
    
        amin := reflect.ValueOf(&a[0]).Pointer()
        amax := reflect.ValueOf(&a[len(a)-1]).Pointer()
        bmin := reflect.ValueOf(&b[0]).Pointer()
        bmax := reflect.ValueOf(&b[len(b)-1]).Pointer()
    
        return !(amax < bmin || amin > bmax)
    }
    

    Testing it:

    a := []int{0, 1, 2, 3}
    b := a[0:2]
    c := a[2:4]
    d := a[0:3]
    
    fmt.Println(overlap(a, b)) // true
    fmt.Println(overlap(b, c)) // false
    fmt.Println(overlap(c, d)) // true
    

    Try it on the Go Playground.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错