From f2fac77d8c6ae42ce2e9a67dcfde4085ec44cfdf Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Wed, 18 Oct 2017 08:50:01 +0200 Subject: [PATCH] queue/Queue: add methods MoveOrderBefore() and MoveOrderAfter() --- src/queue/Queue.cxx | 18 ++++++++++++++++++ src/queue/Queue.hxx | 18 ++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/src/queue/Queue.cxx b/src/queue/Queue.cxx index 9ee9568e0..720778099 100644 --- a/src/queue/Queue.cxx +++ b/src/queue/Queue.cxx @@ -215,6 +215,24 @@ Queue::MoveOrder(unsigned from_order, unsigned to_order) noexcept return to_order; } +unsigned +Queue::MoveOrderBefore(unsigned from_order, unsigned to_order) noexcept +{ + /* if "from_order" comes before "to_order", then the new + position is "to_order-1"; otherwise the "to_order" song is + moved one ahead */ + return MoveOrder(from_order, to_order - (from_order < to_order)); +} + +unsigned +Queue::MoveOrderAfter(unsigned from_order, unsigned to_order) noexcept +{ + /* if "from_order" comes after "to_order", then the new + position is "to_order+1"; otherwise the "to_order" song is + moved one back */ + return MoveOrder(from_order, to_order + (from_order > to_order)); +} + void Queue::DeletePosition(unsigned position) noexcept { diff --git a/src/queue/Queue.hxx b/src/queue/Queue.hxx index b9ca341e4..06e800479 100644 --- a/src/queue/Queue.hxx +++ b/src/queue/Queue.hxx @@ -289,6 +289,24 @@ struct Queue { */ unsigned MoveOrder(unsigned from_order, unsigned to_order) noexcept; + /** + * Moves a song to a new position in the "order" list before + * the given one. + * + * @return the new order number of the given "from" song + */ + unsigned MoveOrderBefore(unsigned from_order, + unsigned to_order) noexcept; + + /** + * Moves a song to a new position in the "order" list after + * the given one. + * + * @return the new order number of the given "from" song + */ + unsigned MoveOrderAfter(unsigned from_order, + unsigned to_order) noexcept; + /** * Moves a song to a new position. */