Global usings and file scoped namespaces

This commit is contained in:
Kwoth
2021-12-19 05:14:11 +01:00
parent bc31dae965
commit ee33313519
548 changed files with 47528 additions and 49115 deletions

View File

@@ -1,77 +1,73 @@
using System.Collections.Generic;
using System.Linq;
namespace NadekoBot.Services;
namespace NadekoBot.Services
public class GreetGrouper<T>
{
public class GreetGrouper<T>
private readonly Dictionary<ulong, HashSet<T>> group;
private readonly object locker = new object();
public GreetGrouper()
{
private readonly Dictionary<ulong, HashSet<T>> group;
private readonly object locker = new object();
group = new Dictionary<ulong, HashSet<T>>();
}
public GreetGrouper()
/// <summary>
/// Creates a group, if group already exists, adds the specified user
/// </summary>
/// <param name="guildId">Id of the server for which to create group for</param>
/// <param name="toAddIfExists">User to add if group already exists</param>
/// <returns></returns>
public bool CreateOrAdd(ulong guildId, T toAddIfExists)
{
lock (locker)
{
group = new Dictionary<ulong, HashSet<T>>();
}
/// <summary>
/// Creates a group, if group already exists, adds the specified user
/// </summary>
/// <param name="guildId">Id of the server for which to create group for</param>
/// <param name="toAddIfExists">User to add if group already exists</param>
/// <returns></returns>
public bool CreateOrAdd(ulong guildId, T toAddIfExists)
{
lock (locker)
if (group.TryGetValue(guildId, out var list))
{
if (group.TryGetValue(guildId, out var list))
list.Add(toAddIfExists);
return false;
}
group[guildId] = new HashSet<T>();
return true;
}
}
/// <summary>
/// Remove the specified amount of items from the group. If all items are removed, group will be removed.
/// </summary>
/// <param name="guildId">Id of the group</param>
/// <param name="count">Maximum number of items to retrieve</param>
/// <param name="items">Items retrieved</param>
/// <returns>Whether the group has no more items left and is deleted</returns>
public bool ClearGroup(ulong guildId, int count, out IEnumerable<T> items)
{
lock (locker)
{
if (group.TryGetValue(guildId, out var set))
{
// if we want more than there are, return everything
if (count >= set.Count)
{
list.Add(toAddIfExists);
return false;
items = set;
group.Remove(guildId);
return true;
}
group[guildId] = new HashSet<T>();
return true;
// if there are more in the group than what's needed
// take the requested number, remove them from the set
// and return them
var toReturn = set.TakeWhile(item => count-- != 0).ToList();
foreach (var item in toReturn)
set.Remove(item);
items = toReturn;
// returning falsemeans group is not yet deleted
// because there are items left
return false;
}
}
/// <summary>
/// Remove the specified amount of items from the group. If all items are removed, group will be removed.
/// </summary>
/// <param name="guildId">Id of the group</param>
/// <param name="count">Maximum number of items to retrieve</param>
/// <param name="items">Items retrieved</param>
/// <returns>Whether the group has no more items left and is deleted</returns>
public bool ClearGroup(ulong guildId, int count, out IEnumerable<T> items)
{
lock (locker)
{
if (group.TryGetValue(guildId, out var set))
{
// if we want more than there are, return everything
if (count >= set.Count)
{
items = set;
group.Remove(guildId);
return true;
}
// if there are more in the group than what's needed
// take the requested number, remove them from the set
// and return them
var toReturn = set.TakeWhile(item => count-- != 0).ToList();
foreach (var item in toReturn)
set.Remove(item);
items = toReturn;
// returning falsemeans group is not yet deleted
// because there are items left
return false;
}
items = Enumerable.Empty<T>();
return true;
}
items = Enumerable.Empty<T>();
return true;
}
}
}