@@ -4804,18 +4804,16 @@ Params:
48044804 pred = Predicate for determining equivalence between range elements.
48054805 r = An $(REF_ALTTEXT input range, isInputRange, std,range,primitives) of
48064806 elements to filter.
4807- pp = The $(LREF PickingPolicy), which by default is the first unique
4808- element from left to right
48094807
48104808Returns:
48114809 An $(REF_ALTTEXT input range, isInputRange, std,range,primitives) of
48124810 consecutively unique elements in the original range. If $(D r) is also a
48134811 forward range or bidirectional range, the returned range will be likewise.
48144812*/
4815- auto uniq (alias pred = " a == b" , Range )(Range r, PickingPolicy pp = PickingPolicy.first )
4813+ auto uniq (alias pred = " a == b" , Range )(Range r)
48164814if (isInputRange! Range && is (typeof (binaryFun! pred(r.front, r.front)) == bool ))
48174815{
4818- return UniqResult! (binaryFun! pred, Range )(r, pp );
4816+ return UniqResult! (binaryFun! pred, Range )(r);
48194817}
48204818
48214819// /
@@ -4837,6 +4835,7 @@ if (isInputRange!Range && is(typeof(binaryFun!pred(r.front, r.front)) == bool))
48374835 assert (equal(uniq([ 1 , 1 , 2 , 1 , 1 , 3 , 1 ]), [1 , 2 , 1 , 3 , 1 ]));
48384836}
48394837
4838+ // /
48404839@safe unittest
48414840{
48424841 import std.algorithm.comparison : equal;
@@ -4855,46 +4854,23 @@ if (isInputRange!Range && is(typeof(binaryFun!pred(r.front, r.front)) == bool))
48554854 assert (r.front == S(1 , " a" ));
48564855 assert (r.back == S(2 , " c" ));
48574856
4858- auto r2 = arr.uniq! ((a, b) => a.i == b.i)(PickingPolicy.last);
4859- assert (r2.equal([S(1 , " b" ), S(2 , " d" )]));
4860- assert (r2.front == S(1 , " b" ));
4861- assert (r2.back == S(2 , " d" ));
4862-
4863- auto arr2 = [ S(1 , " a" ), S(1 , " b" ) ];
4864- auto r3 = arr2.uniq! ((a, b) => a.i == b.i);
4865- assert (r3.front == S(1 , " a" ));
4866- assert (r3.front == r3.back);
4867- }
4868-
4869- /**
4870- Dictates how `uniq` elements should be picked. By default stop at
4871- the end of the shortest of all ranges.
4872- */
4873- enum PickingPolicy
4874- {
4875- // / Stop at the first uniq element, from left to right
4876- first,
4877- // / Stop at the last uniq element, from left to right
4878- last
4857+ r.popBack;
4858+ assert (r.back == S(1 , " a" ));
4859+ assert (r.front == r.back);
48794860}
48804861
48814862private struct UniqResult (alias pred, Range )
48824863{
4883- private PickingPolicy _pickingPolicy;
4884- Range _input;
4885- ElementType! Range _front;
4886- bool _isInFront;
4864+ private Range _input;
48874865 static if (isBidirectionalRange! Range )
48884866 {
4889- ElementType! Range _back;
4890- bool _isInBack;
4891- bool _isOverlapping;
4867+ private ElementType! Range _back;
4868+ private bool _isInBack;
48924869 }
48934870
4894- this (Range input, PickingPolicy pp = PickingPolicy.first )
4871+ this (Range input)
48954872 {
48964873 _input = input;
4897- _pickingPolicy = pp;
48984874 }
48994875
49004876 auto opSlice ()
@@ -4905,53 +4881,34 @@ private struct UniqResult(alias pred, Range)
49054881 void popFront ()
49064882 {
49074883 assert (! empty, " Attempting to popFront an empty uniq." );
4908- if (_input.empty )
4884+ static if (isBidirectionalRange ! Range )
49094885 {
4910- _isInFront = false ;
4911- static if (isBidirectionalRange! Range )
4886+ if (_input.empty)
49124887 {
4913- if (_isOverlapping)
4914- {
4915- _isInBack = false ;
4916- }
4888+ _isInBack = false ;
4889+ return ;
49174890 }
49184891 }
4919- else
4920- {
4921- _isInFront = true ;
4922- _front = _input.front;
4923- ElementType! Range last;
4924- do
4925- {
4926- last = _input.front;
4927- _input.popFront();
4928- }
4929- while (! _input.empty && pred(last, _input.front));
49304892
4931- if (_pickingPolicy == PickingPolicy.last)
4932- {
4933- _front = last;
4934- }
4893+ auto last = _input.front;
4894+ do
4895+ {
4896+ _input.popFront;
49354897 }
4898+ while (! _input.empty && pred(last, _input.front));
49364899 }
49374900
49384901 @property ElementType! Range front()
49394902 {
49404903 assert (! empty, " Attempting to fetch the front of an empty uniq." );
49414904 static if (isBidirectionalRange! Range )
49424905 {
4943- if (_input.empty && ! _isInFront && _isInBack)
4906+ if (_input.empty && _isInBack)
49444907 {
4945- _isInFront = true ;
4946- _isOverlapping = true ;
4947- _front = _back;
4908+ return _back;
49484909 }
49494910 }
4950- if (! _isInFront)
4951- {
4952- popFront();
4953- }
4954- return _front;
4911+ return _input.front;
49554912 }
49564913
49574914 static if (isBidirectionalRange! Range )
@@ -4962,10 +4919,6 @@ private struct UniqResult(alias pred, Range)
49624919 if (_input.empty)
49634920 {
49644921 _isInBack = false ;
4965- if (_isOverlapping)
4966- {
4967- _isInFront = false ;
4968- }
49694922 }
49704923 else
49714924 {
@@ -4975,29 +4928,19 @@ private struct UniqResult(alias pred, Range)
49754928 do
49764929 {
49774930 last = _input.back;
4978- _input.popBack() ;
4931+ _input.popBack;
49794932 }
49804933 while (! _input.empty && pred(_back, _input.back));
4981-
4982- if (_pickingPolicy == PickingPolicy.first)
4983- {
4984- _back = last;
4985- }
4934+ _back = last;
49864935 }
49874936 }
49884937
49894938 @property ElementType! Range back()
49904939 {
49914940 assert (! empty, " Attempting to fetch the back of an empty uniq." );
4992- if (_input.empty && _isInFront && ! _isInBack)
4993- {
4994- _isInBack = true ;
4995- _isOverlapping = true ;
4996- _back = _front;
4997- }
4998- else if (! _isInBack)
4941+ if (! _isInBack)
49994942 {
5000- popBack() ;
4943+ popBack;
50014944 }
50024945 return _back;
50034946 }
@@ -5013,11 +4956,11 @@ private struct UniqResult(alias pred, Range)
50134956 {
50144957 static if (isBidirectionalRange! Range )
50154958 {
5016- return _input.empty && ! _isInFront && ! _isInBack;
4959+ return _input.empty && ! _isInBack;
50174960 }
50184961 else
50194962 {
5020- return _input.empty && ! _isInFront ;
4963+ return _input.empty;
50214964 }
50224965 }
50234966 }
0 commit comments