How to reverse a slice of a pointer array?

Hello everyone,

I have this snippet of code
(ignore the reflection, is merely for debbuging)

func (p Path) GenerateNeighbour() *Path {
	randGen := rand.New(rand.NewSource(time.Now().UnixNano()))

	nodes := make(NodeArray, p.Nodes.Len())
	copy(nodes, p.Nodes)

	fmt.Printf("NODES:%s\n\n", nodes)

	randI := randGen.Intn(nodes.Len())
	randJ := randGen.Intn(nodes.Len())

	nodes.Swap(randI, randJ)
	if randI > randJ {
		r := sort.Reverse(nodes[randJ:randI])
		fmt.Println(r)
		fmt.Println(reflect.TypeOf(r))
		copy(nodes[randJ:], r.(NodeArray)[:])
	} else {
		r := sort.Reverse(nodes[randI:randJ])
		fmt.Println(r)
		fmt.Println(reflect.TypeOf(r))

		copy(nodes[randI:], r.(NodeArray)[:])
	}

	fmt.Println(nodes)
	return &Path{nodes}
}

Where Path is a structure holding a NodeArray

type Path struct {
	Nodes NodeArray
}

Being that NodeArray is nothing more than []*Node that implements the sort.Interface interface

type NodeArray []*Node

Right now, when running, Go panics at copy(nodes[randI:], r.(NodeArray)[:]) either one and tells me that r has type *sort.reverse

What I want to do is to take a slice from an array, reverse it and put it back with the new order, the sort.Interface seemed to allow me to write it in a simple why while proving sort.Swap which helps me in the previous step of swapping nodes

I have no idea how to put the array back in place after reversing because of the types…

Thank you!

sort.Reverse does something different than what you expect. If you see the example in the docs - https://golang.org/pkg/sort/#Reverse - you will see that it is intended to be used with sort.Sort and if you look at the source (https://golang.org/src/sort/sort.go?s=6261:6299#L239) you will see that it basically just reverses the inputs for any calls to the Less method of the sort interface. Its intended purpose is to take a type that you might normally sort in increasing order and instead give you an easy way to sort it in decreasing order.

What you want to do is take an existing array (or slice, I’m not sure which you are actually working with since your code is incomplete) and reverse all the elements in it. To answer this question, I’d first try to answer the question - how would you reverse the order of an integer slice?

That is, given this code:

package main

import (
	"fmt"
)

func main() {
	numbers := []int{2, 7, 1, 3, 6}
	numbers = reverse(numbers)
	fmt.Println(numbers)
}

func reverse(slice []int) []int {
	var ret []int
	// TODO: Implement this
	return ret
}

Playground link: https://play.golang.org/p/BXE7izC5Umd

How would you go about implementing the reverse function?

Your solution here will end up being VERY similar to what you likely want in your given example code, but this way we can all work on the code together and it is much simpler since everyone understands an integer slice pretty well.

I’d suggest taking a stab at implementing that code first. If you have questions let me know where specifically you are getting stuck. eg are you having issues swapping two elements? Are you unsure of which elements to swap? Are you unsure of the overall algorithm needed to reverse a slice?

I understand now, before you commented I wrote this but let the question around because I wanted to understand the mechanic. Thank you!

func (n NodeArray) Reverse() NodeArray {
	l := len(n)
	rev := make(NodeArray, l)
	copy(rev, n)
	for i, j := 0, l-1; i < j; i, j = i+1, j-1 {
		rev[i] = n[j]
		rev[j] = n[i]
	}
	return rev
}

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.