committed by
GitHub
6 changed files with 467 additions and 14 deletions
@ -0,0 +1,79 @@ |
|||
using System; |
|||
using System.Collections; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
|
|||
namespace Avalonia.Layout |
|||
{ |
|||
internal class LayoutQueue<T> : IReadOnlyCollection<T> |
|||
{ |
|||
private struct Info |
|||
{ |
|||
public bool Active; |
|||
public int Count; |
|||
} |
|||
|
|||
public LayoutQueue(Func<T, bool> shouldEnqueue) |
|||
{ |
|||
_shouldEnqueue = shouldEnqueue; |
|||
} |
|||
|
|||
private Func<T, bool> _shouldEnqueue; |
|||
private Queue<T> _inner = new Queue<T>(); |
|||
private Dictionary<T, Info> _loopQueueInfo = new Dictionary<T, Info>(); |
|||
private int _maxEnqueueCountPerLoop = 1; |
|||
|
|||
public int Count => _inner.Count; |
|||
|
|||
public IEnumerator<T> GetEnumerator() => (_inner as IEnumerable<T>).GetEnumerator(); |
|||
|
|||
IEnumerator IEnumerable.GetEnumerator() => _inner.GetEnumerator(); |
|||
|
|||
public T Dequeue() |
|||
{ |
|||
var result = _inner.Dequeue(); |
|||
|
|||
if (_loopQueueInfo.TryGetValue(result, out var info)) |
|||
{ |
|||
info.Active = false; |
|||
_loopQueueInfo[result] = info; |
|||
} |
|||
|
|||
return result; |
|||
} |
|||
|
|||
public void Enqueue(T item) |
|||
{ |
|||
_loopQueueInfo.TryGetValue(item, out var info); |
|||
|
|||
if (!info.Active && info.Count < _maxEnqueueCountPerLoop) |
|||
{ |
|||
_inner.Enqueue(item); |
|||
_loopQueueInfo[item] = new Info() { Active = true, Count = info.Count + 1 }; |
|||
} |
|||
} |
|||
|
|||
public void BeginLoop(int maxEnqueueCountPerLoop) |
|||
{ |
|||
_maxEnqueueCountPerLoop = maxEnqueueCountPerLoop; |
|||
} |
|||
|
|||
public void EndLoop() |
|||
{ |
|||
var notfinalized = _loopQueueInfo.Where(v => v.Value.Count == _maxEnqueueCountPerLoop).ToArray(); |
|||
|
|||
_loopQueueInfo.Clear(); |
|||
|
|||
//prevent layout cycle but add to next layout the non arranged/measured items that might have caused cycle
|
|||
//one more time as a final attempt
|
|||
foreach (var item in notfinalized) |
|||
{ |
|||
if (_shouldEnqueue(item.Key)) |
|||
{ |
|||
_loopQueueInfo[item.Key] = new Info() { Active = true, Count = item.Value.Count + 1 }; |
|||
_inner.Enqueue(item.Key); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
} |
|||
@ -1,6 +1,10 @@ |
|||
// Copyright (c) The Avalonia Project. All rights reserved.
|
|||
// Licensed under the MIT license. See licence.md file in the project root for full license information.
|
|||
|
|||
using System.Runtime.CompilerServices; |
|||
using Avalonia.Metadata; |
|||
|
|||
[assembly: InternalsVisibleTo("Avalonia.Layout.UnitTests")] |
|||
|
|||
[assembly: XmlnsDefinition("https://github.com/avaloniaui", "Avalonia.Layout")] |
|||
|
|||
|
|||
@ -0,0 +1,196 @@ |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using Xunit; |
|||
|
|||
namespace Avalonia.Layout.UnitTests |
|||
{ |
|||
public class LayoutQueueTests |
|||
{ |
|||
[Fact] |
|||
public void Should_Enqueue() |
|||
{ |
|||
var target = new LayoutQueue<string>(_ => true); |
|||
var refQueue = new Queue<string>(); |
|||
var items = new[] { "1", "2", "3" }; |
|||
|
|||
foreach (var item in items) |
|||
{ |
|||
target.Enqueue(item); |
|||
refQueue.Enqueue(item); |
|||
} |
|||
|
|||
Assert.Equal(refQueue, target); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Should_Dequeue() |
|||
{ |
|||
var target = new LayoutQueue<string>(_ => true); |
|||
var refQueue = new Queue<string>(); |
|||
var items = new[] { "1", "2", "3" }; |
|||
|
|||
foreach (var item in items) |
|||
{ |
|||
target.Enqueue(item); |
|||
refQueue.Enqueue(item); |
|||
} |
|||
|
|||
while (refQueue.Count > 0) |
|||
{ |
|||
Assert.Equal(refQueue.Dequeue(), target.Dequeue()); |
|||
} |
|||
} |
|||
|
|||
[Fact] |
|||
public void Should_Enqueue_UniqueElements() |
|||
{ |
|||
var target = new LayoutQueue<string>(_ => true); |
|||
|
|||
var items = new[] { "1", "2", "3", "1" }; |
|||
|
|||
foreach (var item in items) |
|||
{ |
|||
target.Enqueue(item); |
|||
} |
|||
|
|||
Assert.Equal(3, target.Count); |
|||
Assert.Equal(items.Take(3), target); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Shouldnt_Enqueue_More_Than_Limit_In_Loop() |
|||
{ |
|||
var target = new LayoutQueue<string>(_ => true); |
|||
|
|||
//1
|
|||
target.Enqueue("Foo"); |
|||
|
|||
Assert.Equal(1, target.Count); |
|||
|
|||
target.BeginLoop(3); |
|||
|
|||
target.Dequeue(); |
|||
|
|||
//2
|
|||
target.Enqueue("Foo"); |
|||
target.Dequeue(); |
|||
|
|||
//3
|
|||
target.Enqueue("Foo"); |
|||
|
|||
Assert.Equal(1, target.Count); |
|||
|
|||
target.Dequeue(); |
|||
|
|||
//4 more than limit shouldn't be added
|
|||
target.Enqueue("Foo"); |
|||
|
|||
Assert.Equal(0, target.Count); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Shouldnt_Count_Unique_Enqueue_For_Limit_In_Loop() |
|||
{ |
|||
var target = new LayoutQueue<string>(_ => true); |
|||
|
|||
//1
|
|||
target.Enqueue("Foo"); |
|||
|
|||
Assert.Equal(1, target.Count); |
|||
|
|||
target.BeginLoop(3); |
|||
|
|||
target.Dequeue(); |
|||
|
|||
//2
|
|||
target.Enqueue("Foo"); |
|||
target.Enqueue("Foo"); |
|||
target.Dequeue(); |
|||
|
|||
//3
|
|||
target.Enqueue("Foo"); |
|||
target.Enqueue("Foo"); |
|||
|
|||
Assert.Equal(1, target.Count); |
|||
|
|||
target.Dequeue(); |
|||
|
|||
//4 more than limit shouldn't be added
|
|||
target.Enqueue("Foo"); |
|||
|
|||
Assert.Equal(0, target.Count); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Should_Enqueue_When_Condition_True_After_Loop_When_Limit_Met() |
|||
{ |
|||
var target = new LayoutQueue<string>(_ => true); |
|||
|
|||
//1
|
|||
target.Enqueue("Foo"); |
|||
|
|||
Assert.Equal(1, target.Count); |
|||
|
|||
target.BeginLoop(3); |
|||
|
|||
target.Dequeue(); |
|||
|
|||
//2
|
|||
target.Enqueue("Foo"); |
|||
target.Dequeue(); |
|||
|
|||
//3
|
|||
target.Enqueue("Foo"); |
|||
|
|||
Assert.Equal(1, target.Count); |
|||
|
|||
target.Dequeue(); |
|||
|
|||
//4 more than limit shouldn't be added to queue
|
|||
target.Enqueue("Foo"); |
|||
|
|||
Assert.Equal(0, target.Count); |
|||
|
|||
target.EndLoop(); |
|||
|
|||
//after loop should be added once
|
|||
Assert.Equal(1, target.Count); |
|||
Assert.Equal("Foo", target.First()); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Shouldnt_Enqueue_When_Condition_False_After_Loop_When_Limit_Met() |
|||
{ |
|||
var target = new LayoutQueue<string>(_ => false); |
|||
|
|||
//1
|
|||
target.Enqueue("Foo"); |
|||
|
|||
Assert.Equal(1, target.Count); |
|||
|
|||
target.BeginLoop(3); |
|||
|
|||
target.Dequeue(); |
|||
|
|||
//2
|
|||
target.Enqueue("Foo"); |
|||
target.Dequeue(); |
|||
|
|||
//3
|
|||
target.Enqueue("Foo"); |
|||
|
|||
Assert.Equal(1, target.Count); |
|||
|
|||
target.Dequeue(); |
|||
|
|||
//4 more than limit shouldn't be added
|
|||
target.Enqueue("Foo"); |
|||
|
|||
Assert.Equal(0, target.Count); |
|||
|
|||
target.EndLoop(); |
|||
|
|||
Assert.Equal(0, target.Count); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue