Skip to content
This repository has been archived by the owner on Feb 26, 2022. It is now read-only.
/ rant3 Public archive

Commit

Permalink
Add RantDictionaryTable.GetClasses
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicholas Fleck committed Jul 25, 2016
1 parent dcb0eed commit 9027eda
Show file tree
Hide file tree
Showing 14 changed files with 2,300 additions and 2,286 deletions.
40 changes: 20 additions & 20 deletions Rant/Vocabulary/EntryTypeDef.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,30 @@

namespace Rant.Vocabulary
{
internal class EntryTypeDef
{
private readonly HashSet<string> _classes;
internal class EntryTypeDef
{
private readonly HashSet<string> _classes;

public string Name { get; }
public string Name { get; }

public EntryTypeDefFilter Filter { get; }
public EntryTypeDefFilter Filter { get; }

public IEnumerator<string> GetTypeClasses() => _classes.AsEnumerable().GetEnumerator();
public IEnumerator<string> GetTypeClasses() => _classes.AsEnumerable().GetEnumerator();

public EntryTypeDef(string name, IEnumerable<string> classes, EntryTypeDefFilter filter)
{
Name = name;
_classes = new HashSet<string>();
foreach (var c in classes) _classes.Add(c);
Filter = filter;
}
public EntryTypeDef(string name, IEnumerable<string> classes, EntryTypeDefFilter filter)
{
Name = name;
_classes = new HashSet<string>();
foreach (var c in classes) _classes.Add(c);
Filter = filter;
}

public bool IsValidValue(string value) => _classes.Contains(value);
public bool IsValidValue(string value) => _classes.Contains(value);

public bool Test(RantDictionaryEntry entry)
{
if (!EntryTypeDefFilter.Test(Filter, entry)) return true;
return entry.GetClasses().Where(IsValidValue).Count() == 1;
}
}
public bool Test(RantDictionaryEntry entry)
{
if (!EntryTypeDefFilter.Test(Filter, entry)) return true;
return entry.GetClasses().Where(IsValidValue).Count() == 1;
}
}
}
50 changes: 25 additions & 25 deletions Rant/Vocabulary/EntryTypeDefFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,32 @@

namespace Rant.Vocabulary
{
internal class EntryTypeDefFilter
{
private readonly Regex _filterRegex = new Regex(@"!?\w+");
private readonly _<string, bool>[] _filterParts;
internal class EntryTypeDefFilter
{
private readonly Regex _filterRegex = new Regex(@"!?\w+");
private readonly _<string, bool>[] _filterParts;

public EntryTypeDefFilter(string filter)
{
if (filter.Trim() == "*") return;
_filterParts = filter
.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
.Where(s => _filterRegex.IsMatch(s))
.Select(s => _.Create(s.TrimStart('!'), s.StartsWith("!")))
.ToArray();
}
public EntryTypeDefFilter(string filter)
{
if (filter.Trim() == "*") return;
_filterParts = filter
.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
.Where(s => _filterRegex.IsMatch(s))
.Select(s => _.Create(s.TrimStart('!'), s.StartsWith("!")))
.ToArray();
}

private bool DoTest(RantDictionaryEntry entry)
{
return _filterParts == null || _filterParts.All(f => entry.ContainsClass(f.Item1) == f.Item2);
}
private bool DoTest(RantDictionaryEntry entry)
{
return _filterParts == null || _filterParts.All(f => entry.ContainsClass(f.Item1) == f.Item2);
}

/// <summary>
/// Determines whether a type should apply to the specifed entry according to the specified filter.
/// </summary>
/// <param name="filter">The filter to test with.</param>
/// <param name="entry">The entry to test.</param>
/// <returns></returns>
public static bool Test(EntryTypeDefFilter filter, RantDictionaryEntry entry) => filter?.DoTest(entry) ?? false;
}
/// <summary>
/// Determines whether a type should apply to the specifed entry according to the specified filter.
/// </summary>
/// <param name="filter">The filter to test with.</param>
/// <param name="entry">The entry to test.</param>
/// <returns></returns>
public static bool Test(EntryTypeDefFilter filter, RantDictionaryEntry entry) => filter?.DoTest(entry) ?? false;
}
}
256 changes: 128 additions & 128 deletions Rant/Vocabulary/RantDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,145 +8,145 @@

namespace Rant.Vocabulary
{
/// <summary>
/// Represents a Rant dictionary.
/// </summary>
public sealed class RantDictionary
{
private readonly Dictionary<string, RantDictionaryTable> _tables = new Dictionary<string, RantDictionaryTable>();
private readonly HashSet<string> _exposedClasses = new HashSet<string>();
/// <summary>
/// Represents a Rant dictionary.
/// </summary>
public sealed class RantDictionary
{
private readonly Dictionary<string, RantDictionaryTable> _tables = new Dictionary<string, RantDictionaryTable>();
private readonly HashSet<string> _exposedClasses = new HashSet<string>();

/// <summary>
/// Creates a new RantDictionary object that contains no tables.
/// </summary>
public RantDictionary()
{
}
/// <summary>
/// Creates a new RantDictionary object that contains no tables.
/// </summary>
public RantDictionary()
{
}

/// <summary>
/// Creates a new RantDictionary object from the specified dictionary collection.
/// </summary>
/// <param name="tables">The tables to store in the dictionary.</param>
/// <param name="mergeBehavior">The merging strategy to employ.</param>
public RantDictionary(IEnumerable<RantDictionaryTable> tables, TableMergeBehavior mergeBehavior = TableMergeBehavior.Naive)
{
_tables = new Dictionary<string, RantDictionaryTable>();
/// <summary>
/// Creates a new RantDictionary object from the specified dictionary collection.
/// </summary>
/// <param name="tables">The tables to store in the dictionary.</param>
/// <param name="mergeBehavior">The merging strategy to employ.</param>
public RantDictionary(IEnumerable<RantDictionaryTable> tables, TableMergeBehavior mergeBehavior = TableMergeBehavior.Naive)
{
_tables = new Dictionary<string, RantDictionaryTable>();

if (tables == null) return;
if (tables == null) return;

RantDictionaryTable table;
foreach (var list in tables)
{
if (_tables.TryGetValue(list.Name, out table))
{
table.Merge(list, mergeBehavior);
}
else
{
_tables[list.Name] = list;
}
}
}
RantDictionaryTable table;
foreach (var list in tables)
{
if (_tables.TryGetValue(list.Name, out table))
{
table.Merge(list, mergeBehavior);
}
else
{
_tables[list.Name] = list;
}
}
}

/// <summary>
/// Exposes a hidden class to query results.
/// </summary>
/// <param name="hiddenClassName">The name of the hidden class to expose.</param>
/// <exception cref="ArgumentNullException"></exception>
public void IncludeHiddenClass(string hiddenClassName)
{
if (hiddenClassName == null) throw new ArgumentNullException(nameof(hiddenClassName));
if (Util.ValidateName(hiddenClassName)) _exposedClasses.Add(hiddenClassName);
}
/// <summary>
/// Exposes a hidden class to query results.
/// </summary>
/// <param name="hiddenClassName">The name of the hidden class to expose.</param>
/// <exception cref="ArgumentNullException"></exception>
public void IncludeHiddenClass(string hiddenClassName)
{
if (hiddenClassName == null) throw new ArgumentNullException(nameof(hiddenClassName));
if (Util.ValidateName(hiddenClassName)) _exposedClasses.Add(hiddenClassName);
}

/// <summary>
/// Unexposes a hidden class from query results.
/// </summary>
/// <param name="hiddenClassName">The name of the hidden class to unexpose.</param>
/// <exception cref="ArgumentNullException"></exception>
public void ExcludeHiddenClass(string hiddenClassName)
{
if (hiddenClassName == null) throw new ArgumentNullException(nameof(hiddenClassName));
_exposedClasses.Remove(hiddenClassName);
}
/// <summary>
/// Unexposes a hidden class from query results.
/// </summary>
/// <param name="hiddenClassName">The name of the hidden class to unexpose.</param>
/// <exception cref="ArgumentNullException"></exception>
public void ExcludeHiddenClass(string hiddenClassName)
{
if (hiddenClassName == null) throw new ArgumentNullException(nameof(hiddenClassName));
_exposedClasses.Remove(hiddenClassName);
}

/// <summary>
/// Gets all currently exposed hidden classes.
/// </summary>
public IEnumerable<string> IncludedHiddenClasses => _exposedClasses.AsEnumerable();
/// <summary>
/// Gets all currently exposed hidden classes.
/// </summary>
public IEnumerable<string> IncludedHiddenClasses => _exposedClasses.AsEnumerable();

/// <summary>
/// Adds a new RantDictionaryTable object to the collection.
/// </summary>
/// <param name="table"></param>
/// <param name="mergeBehavior">The merging strategy to employ.</param>
public void AddTable(RantDictionaryTable table, TableMergeBehavior mergeBehavior = TableMergeBehavior.Naive)
{
RantDictionaryTable oldTable;
if (_tables.TryGetValue(table.Name, out oldTable))
{
oldTable.Merge(table, mergeBehavior);
}
else
{
_tables[table.Name] = table;
}
}
/// <summary>
/// Adds a new RantDictionaryTable object to the collection.
/// </summary>
/// <param name="table"></param>
/// <param name="mergeBehavior">The merging strategy to employ.</param>
public void AddTable(RantDictionaryTable table, TableMergeBehavior mergeBehavior = TableMergeBehavior.Naive)
{
RantDictionaryTable oldTable;
if (_tables.TryGetValue(table.Name, out oldTable))
{
oldTable.Merge(table, mergeBehavior);
}
else
{
_tables[table.Name] = table;
}
}

/// <summary>
/// Enumerates the tables contained in the current RantDictionary instance.
/// </summary>
/// <returns></returns>
public IEnumerable<RantDictionaryTable> GetTables()
{
foreach (var pair in _tables) yield return pair.Value;
}
/// <summary>
/// Enumerates the tables contained in the current RantDictionary instance.
/// </summary>
/// <returns></returns>
public IEnumerable<RantDictionaryTable> GetTables()
{
foreach (var pair in _tables) yield return pair.Value;
}

/// <summary>
/// Loads all dictionary (.dic) files from the specified directory and returns a RantDictionary object that contains the loaded data.
/// </summary>
/// <param name="directory">The directory from which to load dictionaries.</param>
/// <param name="mergeBehavior">The merging strategy to employ.</param>
/// <returns></returns>
public static RantDictionary FromDirectory(string directory, TableMergeBehavior mergeBehavior = TableMergeBehavior.Naive)
{
return new RantDictionary(Directory.GetFiles(directory, "*.dic", SearchOption.AllDirectories).Select(RantDictionaryTable.FromFile).ToList(), mergeBehavior);
}
/// <summary>
/// Loads all dictionary (.dic) files from the specified directory and returns a RantDictionary object that contains the loaded data.
/// </summary>
/// <param name="directory">The directory from which to load dictionaries.</param>
/// <param name="mergeBehavior">The merging strategy to employ.</param>
/// <returns></returns>
public static RantDictionary FromDirectory(string directory, TableMergeBehavior mergeBehavior = TableMergeBehavior.Naive)
{
return new RantDictionary(Directory.GetFiles(directory, "*.dic", SearchOption.AllDirectories).Select(RantDictionaryTable.FromFile).ToList(), mergeBehavior);
}

/// <summary>
/// Loads all dictionary (.dic) files from the specified directories and returns a RantDictionary object that contains the loaded data.
/// </summary>
/// <param name="directories">The directories from which to load dictionaries.</param>
/// <returns></returns>
public static RantDictionary FromMultiDirectory(params string[] directories)
{
return new RantDictionary(directories.SelectMany(path => Directory.GetFiles(path, "*.dic", SearchOption.AllDirectories)).Select(RantDictionaryTable.FromFile));
}
/// <summary>
/// Loads all dictionary (.dic) files from the specified directories and returns a RantDictionary object that contains the loaded data.
/// </summary>
/// <param name="directories">The directories from which to load dictionaries.</param>
/// <returns></returns>
public static RantDictionary FromMultiDirectory(params string[] directories)
{
return new RantDictionary(directories.SelectMany(path => Directory.GetFiles(path, "*.dic", SearchOption.AllDirectories)).Select(RantDictionaryTable.FromFile));
}

/// <summary>
/// Loads all dictionary (.dic) files from the specified directories and returns a RantDictionary object that contains the loaded data.
/// </summary>
/// <param name="directories">The directories from which to load dictionaries.</param>
/// <param name="mergeBehavior">The merging strategy to employ.</param>
/// <returns></returns>
public static RantDictionary FromMultiDirectory(string[] directories, TableMergeBehavior mergeBehavior)
{
return new RantDictionary(directories.SelectMany(path => Directory.GetFiles(path, "*.dic", SearchOption.AllDirectories)).Select(RantDictionaryTable.FromFile), mergeBehavior);
}
/// <summary>
/// Loads all dictionary (.dic) files from the specified directories and returns a RantDictionary object that contains the loaded data.
/// </summary>
/// <param name="directories">The directories from which to load dictionaries.</param>
/// <param name="mergeBehavior">The merging strategy to employ.</param>
/// <returns></returns>
public static RantDictionary FromMultiDirectory(string[] directories, TableMergeBehavior mergeBehavior)
{
return new RantDictionary(directories.SelectMany(path => Directory.GetFiles(path, "*.dic", SearchOption.AllDirectories)).Select(RantDictionaryTable.FromFile), mergeBehavior);
}

/// <summary>
/// Queries the RantDictionary according to the specified criteria and returns a random match.
/// </summary>
/// <param name="rng">The random number generator to randomize the match with.</param>
/// <param name="query">The search criteria to use.</param>
/// <param name="syncState">The state object to use for carrier synchronization.</param>
/// <returns></returns>
internal string Query(RNG rng, Query query, QueryState syncState)
{
RantDictionaryTable table;
return !_tables.TryGetValue(query.Name, out table)
? "[Missing Table]"
: table.Query(this, rng, query, syncState);
}
}
/// <summary>
/// Queries the RantDictionary according to the specified criteria and returns a random match.
/// </summary>
/// <param name="rng">The random number generator to randomize the match with.</param>
/// <param name="query">The search criteria to use.</param>
/// <param name="syncState">The state object to use for carrier synchronization.</param>
/// <returns></returns>
internal string Query(RNG rng, Query query, QueryState syncState)
{
RantDictionaryTable table;
return !_tables.TryGetValue(query.Name, out table)
? "[Missing Table]"
: table.Query(this, rng, query, syncState);
}
}
}
Loading

0 comments on commit 9027eda

Please sign in to comment.