Skip to content

Instantly share code, notes, and snippets.

@monopolly
Last active November 3, 2017 09:04
Show Gist options
  • Save monopolly/284cfb644b84663d07890df7e61af228 to your computer and use it in GitHub Desktop.
Save monopolly/284cfb644b84663d07890df7e61af228 to your computer and use it in GitHub Desktop.
Move up slice element, golang
//find "up" in slice and move it to begin of slice
func upslice(f *[]uint32, up uint32) {
if len((*f)) == 0 || (*f)[0] == up {
return
}
if (*f)[len(*f)-1] == up {
(*f) = append([]uint32{up}, (*f)[:len(*f)-1]...)
return
}
for p, x := range *f {
if x == up {
(*f) = append([]uint32{up}, append((*f)[:p], (*f)[p+1:]...)...)
break
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment