Is there a version of the removeElement function in Go for the vector package like Java has in its Vector class?

StackOverflow https://stackoverflow.com/questions/1802102

  •  05-07-2019
  •  | 
  •  

Question

I am porting over some Java code into Google's Go language and I converting all code except I am stuck on just one part after an amazingly smooth port. My Go code looks like this and the section I am talking about is commented out:

func main() {
    var puzzleHistory * vector.Vector;
    puzzleHistory = vector.New(0);
    var puzzle PegPuzzle;
    puzzle.InitPegPuzzle(3,2);
    puzzleHistory.Push(puzzle);

    var copyPuzzle PegPuzzle;
    var currentPuzzle PegPuzzle;

    currentPuzzle = puzzleHistory.At(0).(PegPuzzle);
    isDone := false;
    for !isDone {
        currentPuzzle = puzzleHistory.At(0).(PegPuzzle);
        currentPuzzle.findAllValidMoves();

        for i := 0; i < currentPuzzle.validMoves.Len(); i++ {
            copyPuzzle.NewPegPuzzle(currentPuzzle.holes, currentPuzzle.movesAlreadyDone);
            copyPuzzle.doMove(currentPuzzle.validMoves.At(i).(Move));
            // There is no function in Go's Vector that will remove an element like Java's Vector
            //puzzleHistory.removeElement(currentPuzzle);
            copyPuzzle.findAllValidMoves();
            if copyPuzzle.validMoves.Len() != 0 {
                puzzleHistory.Push(copyPuzzle);
            }
            if copyPuzzle.isSolutionPuzzle() {
                fmt.Printf("Puzzle Solved");
                copyPuzzle.show();
                isDone = true;
            }
        }
    }
}

If there is no version available, which I believe there isn't ... does anyone know how I would go about implementing such a thing on my own?

Was it helpful?

Solution

How about Vector.Delete( i ) ?

OTHER TIPS

Right now Go doesn't support generic equality operators. So you'll have to write something that iterates over the vector and removes the correct one.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top