mirror of https://github.com/Squidex/squidex.git
48 changed files with 577 additions and 323 deletions
@ -1,68 +0,0 @@ |
|||||
// ==========================================================================
|
|
||||
// Squidex Headless CMS
|
|
||||
// ==========================================================================
|
|
||||
// Copyright (c) Squidex UG (haftungsbeschränkt)
|
|
||||
// All rights reserved. Licensed under the MIT license.
|
|
||||
// ==========================================================================
|
|
||||
|
|
||||
using System.Collections; |
|
||||
using System.Collections.Generic; |
|
||||
using System.Collections.Immutable; |
|
||||
|
|
||||
namespace Squidex.Domain.Apps.Core |
|
||||
{ |
|
||||
public abstract class DictionaryWrapper<TKey, TValue> : IReadOnlyDictionary<TKey, TValue> |
|
||||
{ |
|
||||
private readonly ImmutableDictionary<TKey, TValue> inner; |
|
||||
|
|
||||
public TValue this[TKey key] |
|
||||
{ |
|
||||
get { return inner[key]; } |
|
||||
} |
|
||||
|
|
||||
public IEnumerable<TKey> Keys |
|
||||
{ |
|
||||
get { return inner.Keys; } |
|
||||
} |
|
||||
|
|
||||
public IEnumerable<TValue> Values |
|
||||
{ |
|
||||
get { return inner.Values; } |
|
||||
} |
|
||||
|
|
||||
public int Count |
|
||||
{ |
|
||||
get { return inner.Count; } |
|
||||
} |
|
||||
|
|
||||
protected ImmutableDictionary<TKey, TValue> Inner |
|
||||
{ |
|
||||
get { return inner; } |
|
||||
} |
|
||||
|
|
||||
protected DictionaryWrapper(ImmutableDictionary<TKey, TValue> inner) |
|
||||
{ |
|
||||
this.inner = inner; |
|
||||
} |
|
||||
|
|
||||
public bool ContainsKey(TKey key) |
|
||||
{ |
|
||||
return inner.ContainsKey(key); |
|
||||
} |
|
||||
|
|
||||
public bool TryGetValue(TKey key, out TValue value) |
|
||||
{ |
|
||||
return inner.TryGetValue(key, out value); |
|
||||
} |
|
||||
|
|
||||
IEnumerator<KeyValuePair<TKey, TValue>> IEnumerable<KeyValuePair<TKey, TValue>>.GetEnumerator() |
|
||||
{ |
|
||||
return inner.GetEnumerator(); |
|
||||
} |
|
||||
|
|
||||
IEnumerator IEnumerable.GetEnumerator() |
|
||||
{ |
|
||||
return inner.GetEnumerator(); |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
@ -0,0 +1,21 @@ |
|||||
|
// ==========================================================================
|
||||
|
// Squidex Headless CMS
|
||||
|
// ==========================================================================
|
||||
|
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
||||
|
// All rights reserved. Licensed under the MIT license.
|
||||
|
// ==========================================================================
|
||||
|
|
||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using System.Linq; |
||||
|
|
||||
|
namespace Squidex.Infrastructure.Collections |
||||
|
{ |
||||
|
public static class ArrayDictionary |
||||
|
{ |
||||
|
public static ArrayDictionary<TKey, TValue> ToArrayDictionary<TKey, TValue>(this IEnumerable<TValue> source, Func<TValue, TKey> keyExtractor) |
||||
|
{ |
||||
|
return new ArrayDictionary<TKey, TValue>(source.Select(x => new KeyValuePair<TKey, TValue>(keyExtractor(x), x)).ToArray()); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,163 @@ |
|||||
|
// ==========================================================================
|
||||
|
// Squidex Headless CMS
|
||||
|
// ==========================================================================
|
||||
|
// Copyright (c) Squidex UG (haftungsbeschränkt)
|
||||
|
// All rights reserved. Licensed under the MIT license.
|
||||
|
// ==========================================================================
|
||||
|
|
||||
|
using System; |
||||
|
using System.Collections; |
||||
|
using System.Collections.Generic; |
||||
|
using System.Linq; |
||||
|
|
||||
|
namespace Squidex.Infrastructure.Collections |
||||
|
{ |
||||
|
public class ArrayDictionary<TKey, TValue> : IReadOnlyDictionary<TKey, TValue> |
||||
|
{ |
||||
|
private readonly IEqualityComparer<TKey> keyComparer; |
||||
|
private readonly KeyValuePair<TKey, TValue>[] items; |
||||
|
|
||||
|
public TValue this[TKey key] |
||||
|
{ |
||||
|
get |
||||
|
{ |
||||
|
if (!TryGetValue(key, out var value)) |
||||
|
{ |
||||
|
throw new KeyNotFoundException(); |
||||
|
} |
||||
|
|
||||
|
return value; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public IEnumerable<TKey> Keys |
||||
|
{ |
||||
|
get { return items.Select(x => x.Key); } |
||||
|
} |
||||
|
|
||||
|
public IEnumerable<TValue> Values |
||||
|
{ |
||||
|
get { return items.Select(x => x.Value); } |
||||
|
} |
||||
|
|
||||
|
public int Count |
||||
|
{ |
||||
|
get { return items.Length; } |
||||
|
} |
||||
|
|
||||
|
public ArrayDictionary() |
||||
|
: this(EqualityComparer<TKey>.Default, Array.Empty<KeyValuePair<TKey, TValue>>()) |
||||
|
{ |
||||
|
} |
||||
|
|
||||
|
public ArrayDictionary(KeyValuePair<TKey, TValue>[] items) |
||||
|
: this(EqualityComparer<TKey>.Default, items) |
||||
|
{ |
||||
|
} |
||||
|
|
||||
|
public ArrayDictionary(IEqualityComparer<TKey> keyComparer, KeyValuePair<TKey, TValue>[] items) |
||||
|
{ |
||||
|
Guard.NotNull(items, nameof(items)); |
||||
|
Guard.NotNull(keyComparer, nameof(keyComparer)); |
||||
|
|
||||
|
this.items = items; |
||||
|
|
||||
|
this.keyComparer = keyComparer; |
||||
|
} |
||||
|
|
||||
|
public KeyValuePair<TKey, TValue>[] With(TKey key, TValue value) |
||||
|
{ |
||||
|
var result = new List<KeyValuePair<TKey, TValue>>(Math.Max(items.Length, 1)); |
||||
|
|
||||
|
var wasReplaced = false; |
||||
|
|
||||
|
for (var i = 0; i < items.Length; i++) |
||||
|
{ |
||||
|
var item = items[i]; |
||||
|
|
||||
|
if (wasReplaced || !keyComparer.Equals(item.Key, key)) |
||||
|
{ |
||||
|
result.Add(item); |
||||
|
} |
||||
|
else |
||||
|
{ |
||||
|
result.Add(new KeyValuePair<TKey, TValue>(key, value)); |
||||
|
wasReplaced = true; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
if (!wasReplaced) |
||||
|
{ |
||||
|
result.Add(new KeyValuePair<TKey, TValue>(key, value)); |
||||
|
} |
||||
|
|
||||
|
return result.ToArray(); |
||||
|
} |
||||
|
|
||||
|
public KeyValuePair<TKey, TValue>[] Without(TKey key) |
||||
|
{ |
||||
|
var result = new List<KeyValuePair<TKey, TValue>>(Math.Max(items.Length, 1)); |
||||
|
|
||||
|
var wasRemoved = false; |
||||
|
|
||||
|
for (var i = 0; i < items.Length; i++) |
||||
|
{ |
||||
|
var item = items[i]; |
||||
|
|
||||
|
if (wasRemoved || !keyComparer.Equals(item.Key, key)) |
||||
|
{ |
||||
|
result.Add(item); |
||||
|
} |
||||
|
else |
||||
|
{ |
||||
|
wasRemoved = true; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
return result.ToArray(); |
||||
|
} |
||||
|
|
||||
|
public bool ContainsKey(TKey key) |
||||
|
{ |
||||
|
for (var i = 0; i < items.Length; i++) |
||||
|
{ |
||||
|
if (keyComparer.Equals(items[i].Key, key)) |
||||
|
{ |
||||
|
return true; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
return false; |
||||
|
} |
||||
|
|
||||
|
public bool TryGetValue(TKey key, out TValue value) |
||||
|
{ |
||||
|
for (var i = 0; i < items.Length; i++) |
||||
|
{ |
||||
|
if (keyComparer.Equals(items[i].Key, key)) |
||||
|
{ |
||||
|
value = items[i].Value; |
||||
|
return true; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
value = default(TValue); |
||||
|
return false; |
||||
|
} |
||||
|
|
||||
|
IEnumerator<KeyValuePair<TKey, TValue>> IEnumerable<KeyValuePair<TKey, TValue>>.GetEnumerator() |
||||
|
{ |
||||
|
return GetEnumerable(items).GetEnumerator(); |
||||
|
} |
||||
|
|
||||
|
IEnumerator IEnumerable.GetEnumerator() |
||||
|
{ |
||||
|
return items.GetEnumerator(); |
||||
|
} |
||||
|
|
||||
|
private static IEnumerable<T2> GetEnumerable<T2>(T2[] array) |
||||
|
{ |
||||
|
return array; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,36 @@ |
|||||
|
// ==========================================================================
|
||||
|
// Squidex Headless CMS
|
||||
|
// ==========================================================================
|
||||
|
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
||||
|
// All rights reserved. Licensed under the MIT license.
|
||||
|
// ==========================================================================
|
||||
|
|
||||
|
using System.Collections.Generic; |
||||
|
using System.Collections.ObjectModel; |
||||
|
using System.Linq; |
||||
|
|
||||
|
namespace Squidex.Infrastructure.Collections |
||||
|
{ |
||||
|
public static class ReadOnlyCollection |
||||
|
{ |
||||
|
private static class Empties<T> |
||||
|
{ |
||||
|
public static readonly ReadOnlyCollection<T> Collection = new ReadOnlyCollection<T>(new List<T>()); |
||||
|
} |
||||
|
|
||||
|
public static ReadOnlyCollection<T> Create<T>(params T[] items) |
||||
|
{ |
||||
|
return new ReadOnlyCollection<T>(items.ToList()); |
||||
|
} |
||||
|
|
||||
|
public static ReadOnlyCollection<T> Empty<T>() |
||||
|
{ |
||||
|
return Empties<T>.Collection; |
||||
|
} |
||||
|
|
||||
|
public static ReadOnlyCollection<T> ToReadOnlyCollection<T>(this IEnumerable<T> source) |
||||
|
{ |
||||
|
return new ReadOnlyCollection<T>(source.ToList()); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue