File 3212-Add-delete-2-and-delete_r-2-to-the-queue-module.patch of Package erlang

From 0e7d3f15d163d0408037e8dd151e787e9af6ae64 Mon Sep 17 00:00:00 2001
From: Maria-12648430 <maria-12648430@gmx.net>
Date: Mon, 16 Nov 2020 12:51:35 +0100
Subject: [PATCH 2/5] Add delete/2 and delete_r/2 to the queue module

---
 lib/stdlib/doc/src/queue.xml    | 22 +++++++++++
 lib/stdlib/src/queue.erl        | 68 ++++++++++++++++++++++++++++++++-
 lib/stdlib/test/queue_SUITE.erl | 12 ++++++
 3 files changed, 101 insertions(+), 1 deletion(-)

diff --git a/lib/stdlib/doc/src/queue.xml b/lib/stdlib/doc/src/queue.xml
index 297ea34ace..6446753f45 100644
--- a/lib/stdlib/doc/src/queue.xml
+++ b/lib/stdlib/doc/src/queue.xml
@@ -55,6 +55,8 @@
     <p>All operations have an amortized O(1) running time, except
       <seealso marker="#all/2"><c>all/2</c></seealso>,
       <seealso marker="#any/2"><c>any/2</c></seealso>,
+      <seealso marker="#delete/2"><c>delete/2</c></seealso>,
+      <seealso marker="#delete_r/2"><c>delete_r/2</c></seealso>,
       <seealso marker="#filter/2"><c>filter/2</c></seealso>,
       <seealso marker="#filtermap/2"><c>filtermap/2</c></seealso>,
       <seealso marker="#fold/3"><c>fold/3</c></seealso>,
@@ -139,6 +141,26 @@
       </desc>
     </func>
 
+    <func>
+      <name name="delete" arity="2"/>
+      <fsummary>Delete an item from the front of a queue.</fsummary>
+      <desc>
+        <p>Returns a copy of <c><anno>Q1</anno></c> where the first item
+          matching <c><anno>Item</anno></c> is deleted, if there is such an
+          element.</p>
+      </desc>
+    </func>
+
+    <func>
+      <name name="delete_r" arity="2"/>
+      <fsummary>Delete an item from the rear of a queue.</fsummary>
+      <desc>
+        <p>Returns a copy of <c><anno>Q1</anno></c> where the last item
+          matching <c><anno>Item</anno></c> is deleted, if there is such an
+          element.</p>
+      </desc>
+    </func>
+
     <func>
       <name name="filter" arity="2"/>
       <fsummary>Filter a queue.</fsummary>
diff --git a/lib/stdlib/src/queue.erl b/lib/stdlib/src/queue.erl
index e58f0404f0..2447f68946 100644
--- a/lib/stdlib/src/queue.erl
+++ b/lib/stdlib/src/queue.erl
@@ -27,7 +27,8 @@
 -export([get/1,get_r/1,peek/1,peek_r/1,drop/1,drop_r/1]).
 
 %% Higher level API
--export([reverse/1,join/2,split/2,filter/2,filtermap/2,fold/3,any/2,all/2]).
+-export([reverse/1,join/2,split/2,filter/2,filtermap/2,fold/3,any/2,all/2,
+	 delete/2,delete_r/2]).
 
 %% Okasaki API from klacke
 -export([cons/2,head/1,tail/1,
@@ -475,6 +476,71 @@ all(Pred, {R, F}) when is_function(Pred, 1), is_list(R), is_list(F) ->
 all(Pred, Q) ->
     erlang:error(badarg, [Pred, Q]).
 
+%% Delete the first occurence of an item in the queue,
+%% according to queue order.
+%%
+%% O(len(Q1)) worst case
+-spec delete(Item, Q1) -> Q2 when
+      Item :: T,
+      Q1 :: queue(T),
+      Q2 :: queue(T),
+      T :: term().
+delete(Item, {R0, F0} = Q) when is_list(R0), is_list(F0) ->
+    case delete_front(Item, F0) of
+        false ->
+            case delete_rear(Item, R0) of
+                false ->
+                    Q;
+                [] ->
+                    f2r(F0);
+                R1 ->
+                    {R1, F0}
+            end;
+        [] ->
+            r2f(R0);
+        F1 ->
+            {R0, F1}
+    end;
+delete(Item, Q) ->
+    erlang:error(badarg, [Item, Q]).
+
+%% Delete the last occurence of an item in the queue,
+%% according to queue order.
+%%
+%% O(len(Q1)) worst case
+-spec delete_r(Item, Q1) -> Q2 when
+      Item :: T,
+      Q1 :: queue(T),
+      Q2 :: queue(T),
+      T :: term().
+delete_r(Item, {R0, F0}) when is_list(R0), is_list(F0) ->
+    {F1, R1}=delete(Item, {F0, R0}),
+    {R1, F1};
+delete_r(Item, Q) ->
+    erlang:error(badarg, [Item, Q]).
+
+delete_front(Item, [Item|Rest]) ->
+    Rest;
+delete_front(Item, [X|Rest]) ->
+    case delete_front(Item, Rest) of
+        false -> false;
+        F -> [X|F]
+    end;
+delete_front(_, []) ->
+    false.
+
+delete_rear(Item, [X|Rest]) ->
+    case delete_rear(Item, Rest) of
+        false when X=:=Item ->
+            Rest;
+        false ->
+            false;
+        R ->
+            [X|R]
+    end;
+delete_rear(_, []) ->
+    false.
+
 %%--------------------------------------------------------------------------
 %% Okasaki API inspired by an Erlang user contribution "deque.erl" 
 %% by Claes Wikstrom <klacke@kaja.klacke.net> 1999.
diff --git a/lib/stdlib/test/queue_SUITE.erl b/lib/stdlib/test/queue_SUITE.erl
index 28a3dd7e9d..71af5fcf2d 100644
--- a/lib/stdlib/test/queue_SUITE.erl
+++ b/lib/stdlib/test/queue_SUITE.erl
@@ -400,6 +400,18 @@ do_op_test(F) ->
     false = queue:all(fun(X) -> X>1 end, AllQ),
     true = queue:all(fun(X) -> X=<3 end, AllQ),
     false = queue:all(fun(X) -> X<3 end, AllQ),
+    [] = queue:to_list(queue:delete(x, queue:new())),
+    [1,2,3] = queue:to_list(queue:delete(x, queue:from_list([1,2,3]))),
+    [2,3] = queue:to_list(queue:delete(x, queue:from_list([x,2,3]))),
+    [1,3] = queue:to_list(queue:delete(x, queue:from_list([1,x,3]))),
+    [1,2] = queue:to_list(queue:delete(x, queue:from_list([1,2,x]))),
+    [1,2,x] = queue:to_list(queue:delete(x, queue:from_list([x,1,2,x]))),
+    [] = queue:to_list(queue:delete_r(x, queue:new())),
+    [1,2,3] = queue:to_list(queue:delete_r(x, queue:from_list([1,2,3]))),
+    [2,3] = queue:to_list(queue:delete_r(x, queue:from_list([x,2,3]))),
+    [1,3] = queue:to_list(queue:delete_r(x, queue:from_list([1,x,3]))),
+    [1,2] = queue:to_list(queue:delete_r(x, queue:from_list([1,2,x]))),
+    [x,1,2] = queue:to_list(queue:delete_r(x, queue:from_list([x,1,2,x]))),
     %%
     ok.
 
-- 
2.26.2

openSUSE Build Service is sponsored by