Skip to content

Instantly share code, notes, and snippets.

@juliensimon
Last active August 29, 2015 14:17
Show Gist options
  • Save juliensimon/cc31d9707e7ae8e22624 to your computer and use it in GitHub Desktop.
Save juliensimon/cc31d9707e7ae8e22624 to your computer and use it in GitHub Desktop.
// This is equivalent to: linearSearch(list, t, build(modeMoveFirst))
public static <T> boolean linearSearchMoveFirst(List<T> list, T t) {
return linearSearch(list, t,
((List<T> l, int index)
-> { T elem = l.remove(index); l.add(0, elem); } ));
}
// This is equivalent to: linearSearch(list, t, build(modeMoveLast))
public static <T> boolean linearSearchMoveLast(List<T> list, T t) {
return linearSearch(list, t,
((List<T> l, int index)
-> { T elem = l.remove(index); l.add(elem); } ));
}
// This is equivalent to: linearSearch(list, t, build(modeMoveUp))
public static <T> boolean linearSearchMoveUp(List<T> list, T t) {
return linearSearch(list, t,
((List<T> l, int index) -> {
if (index != 0) {
Collections.swap(list, index-1, index);
}
}));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment