Last active
November 3, 2017 09:04
-
-
Save monopolly/284cfb644b84663d07890df7e61af228 to your computer and use it in GitHub Desktop.
Move up slice element, golang
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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