// Given a sequence of strings, return all non-trivial anagram // classes. Should use a *sequenced* equalityComparer on a TreeBag<char>, // obviously: after all, characters can be sorted by ASCII code. On // 347 000 distinct Danish words this takes 70 cpu seconds, 180 MB // memory, and 263 wall-clock seconds (due to swapping). // Using a TreeBag<char> and a sequenced equalityComparer takes 82 cpu seconds // and 180 MB RAM to find the 26,058 anagram classes among 347,000 // distinct words. // Using an unsequenced equalityComparer on TreeBag<char> or HashBag<char> // makes it criminally slow: at least 1200 cpu seconds. This must // be because many bags get the same hash code, so that there are // many collisions. But exactly how the unsequenced equalityComparer works is // not clear ... or is it because unsequenced equality is slow? public static SCG.IEnumerable<SCG.IEnumerable<String>> AnagramClasses(SCG.IEnumerable<String> ss) { bool unseq = true; IDictionary<TreeBag<char>, TreeSet<String>> classes; if (unseq) { SCG.IEqualityComparer<TreeBag<char>> unsequencedTreeBagEqualityComparer = UnsequencedCollectionEqualityComparer<TreeBag<char>, char>.Default; classes = new HashDictionary<TreeBag<char>, TreeSet<String>>(unsequencedTreeBagEqualityComparer); } else { SCG.IEqualityComparer<TreeBag<char>> sequencedTreeBagEqualityComparer = SequencedCollectionEqualityComparer<TreeBag<char>, char>.Default; classes = new HashDictionary<TreeBag<char>, TreeSet<String>>(sequencedTreeBagEqualityComparer); } foreach (String s in ss) { TreeBag<char> anagram = AnagramClass(s); TreeSet<String> anagramClass; if (!classes.Find(anagram, out anagramClass)) classes[anagram] = anagramClass = new TreeSet<String>(); anagramClass.Add(s); } foreach (TreeSet<String> anagramClass in classes.Values) if (anagramClass.Count > 1) yield return anagramClass; }
public static void Equalities(String msg, SCG.IEqualityComparer<IList<int>> equalityComparer) { Console.WriteLine("\n{0}:", msg); Console.Write("Equals(col1,col2)={0,-5}; ", equalityComparer.Equals(col1, col2)); Console.Write("Equals(col1,col3)={0,-5}; ", equalityComparer.Equals(col1, col3)); Console.WriteLine("Equals(col2,col3)={0,-5}", equalityComparer.Equals(col2, col3)); }
internal BootstrapConfigurationImpl( ISet<string> flags, SCG.IDictionary<string, string> properties ) { this.flags = flags; this.properties = properties; }
private void RandomBoardLayout(SCG.IEnumerable<Tile> tiles, SCG.IEnumerable<HexTile> hexes, int x1, int y1, int x2, int y2, int maxd1, int maxd2) { IList<HexTile> hexRandom = new ArrayList<HexTile>(); hexRandom.AddAll(from hex in hexes orderby RNG.Next() select hex); foreach (var t in tiles) { var d1 = this.tiles[x1][y1].DistanceTo(t); var d2 = this.tiles[x2][y2].DistanceTo(t); if (d1 == maxd1 && d2 >= maxd2 + 1 || d1 >= maxd1 + 1 && d2 == maxd2 || d1 == maxd1 + 1 && d2 == maxd2 + 1) { t.HexTile = new OceanHexTile(); } else if (d1 <= maxd1 && d2 <= maxd2) { if (hexRandom.Count == 0) { t.HexTile = new DesertHexTile(""); } else { t.HexTile = hexRandom.RemoveLast(); } } else { t.HexTile = null; } } }
public ReadableBootstrapConfiguration Build(SCG.IReadOnlyCollection<TrinketComponent> components) { var bootstrapConfiguration = new BootstrapConfigurationImpl(); foreach (var component in components) { component.HandleBootstrap(bootstrapConfiguration); } return bootstrapConfiguration; }
public static void FindCollisions(SCG.IEnumerable<String> ss) { HashBag<int> occurrences = new HashBag<int>(); foreach (String s in ss) { TreeBag<char> tb = TreeBag(s); // HashBag<char> hb = HashBag(s); occurrences.Add(sequencedTreeBagHasher.GetHashCode(tb)); // unsequencedTreeBagHasher.GetHashCode(tb); // unsequencedHashBagHasher.GetHashCode(hb); } }
public OptionGroup(string id, string name, string description, OptionGroupRequirement require, bool requireExplicitAssignment, SCG.IComparer<string> keyComparer) { Debug.Assert(!String.IsNullOrEmpty(id)); Debug.Assert(keyComparer != null); mId = id; mName = name; mDescription = description; mRequire = require; mOptions = new TreeDictionary<string, Option>(keyComparer); mRequireExplicitAssignment = requireExplicitAssignment; }
public void Subscribe(Action<CreatedProcessDescriptor> handler, SCG.IEnumerable<string> names, bool retroactive) { var lowerCaseNames = new ItzWarty.Collections.HashSet<string>(names.Select(FormatProcessName)); foreach (var lowerCaseName in lowerCaseNames) { processSpawnedHandlersByProcessName.Add(lowerCaseName, handler); } if (retroactive) { var processes = processWatcher.FindProcess((p) => lowerCaseNames.Contains(FormatProcessName(p.ProcessName))); foreach (var process in processes) { handler(new CreatedProcessDescriptor(process.ProcessName, process.Id, processProxy.GetParentProcess(process).Id)); } } }
// Given a sequence of strings, return all non-trivial anagram // classes. // Using HashBag<char> and an unsequenced equalityComparer, this performs as // follows on 1600 MHz Mobile P4 and .Net 2.0 beta 1 (wall-clock // time): // 50 000 words 2 822 classes 2.0 sec // 100 000 words 5 593 classes 4.3 sec // 200 000 words 11 705 classes 8.8 sec // 300 000 words 20 396 classes 52.0 sec includes swapping // 347 165 words 24 428 classes 146.0 sec includes swapping // The maximal memory consumption is less than 180 MB. public static SCG.IEnumerable<SCG.IEnumerable<String>> AnagramClasses(SCG.IEnumerable<String> ss) { IDictionary<HashBag<char>, TreeSet<String>> classes = new HashDictionary<HashBag<char>, TreeSet<String>>(); foreach (String s in ss) { HashBag<char> anagram = AnagramClass(s); TreeSet<String> anagramClass; if (!classes.Find(ref anagram, out anagramClass)) classes[anagram] = anagramClass = new TreeSet<String>(); anagramClass.Add(s); } foreach (TreeSet<String> anagramClass in classes.Values) if (anagramClass.Count > 1) yield return anagramClass; }
public void TrySCGColl(SCG.ICollection<double> coll) { // All members of SCG.ICollection<T> Assert.AreEqual(0, coll.Count); double[] arr = { }; coll.CopyTo(arr, 0); Assert.IsFalse(coll.IsReadOnly); coll.Add(2.3); coll.Add(3.2); Assert.AreEqual(2, coll.Count); Assert.IsTrue(coll.Contains(2.3)); Assert.IsFalse(coll.Contains(3.1)); Assert.IsFalse(coll.Remove(3.1)); Assert.IsTrue(coll.Remove(3.2)); Assert.IsFalse(coll.Contains(3.1)); Assert.AreEqual(1, coll.Count); coll.Clear(); Assert.AreEqual(0, coll.Count); Assert.IsFalse(coll.Remove(3.1)); }
/// <summary> /// Initializes a new instance of the <see cref="UsageInfo"/> class. /// </summary> /// <param name="options">The options.</param> /// <param name="optionStyles">The option styles.</param> /// <param name="parser">The parser.</param> internal UsageInfo(SCG.IEnumerable<KeyValuePair<string, IOption>> options, OptionStyles optionStyles, CommandLineParser parser) { mParser = parser; foreach (KeyValuePair<string, IOption> entry in options) { Option option = entry.Value as Option; if (option != null) { if (option.Group != null) { if (!mGroups.Contains(option.Group.Id)) { mGroups.Add(option.Group.Id, new OptionGroupInfo(this, option.Group, optionStyles)); } } else { Debug.Assert(!mOptions.Contains(option.Name)); mOptions.Add(option.Name, new OptionInfo(this, option, optionStyles)); } } } }
public GameState(ManagableMap map, SCG.IReadOnlyList<MobileInstance> mobiles, EventManager eventManager) { Map = map; Mobiles = mobiles; EventManager = eventManager; }
internal void MakeTargetLabel(SCG.List<CILLabel> labs) { uint targetOffset = (uint)(offset + size + target); dest = CILInstructions.GetLabel(labs, targetOffset); }
// Given a sequence of strings, return only the first member of each // anagram class. public static SCG.IEnumerable<String> FirstAnagramOnly(SCG.IEnumerable<String> ss) { SCG.IEqualityComparer<TreeBag<char>> tbh = UnsequencedCollectionEqualityComparer<TreeBag<char>, char>.Default; HashSet<TreeBag<char>> anagrams = new HashSet<TreeBag<char>>(tbh); foreach (String s in ss) { TreeBag<char> anagram = AnagramClass(s); if (!anagrams.Contains(anagram)) { anagrams.Add(anagram); yield return s; } } }
internal static CILLabel GetLabel(SCG.List<CILLabel> labs, uint targetOffset) { CILLabel lab; int i = 0; while ((i < labs.Count) && (((CILLabel)labs[i]).offset < targetOffset)) i++; if (i < labs.Count) { if (((CILLabel)labs[i]).offset == targetOffset) // existing label lab = (CILLabel)labs[i]; else { lab = new CILLabel(targetOffset); labs.Insert(i, lab); } } else { lab = new CILLabel(targetOffset); labs.Add(lab); } return lab; }
internal void MakeTargetLabels(SCG.List<CILLabel> labs) { cases = new CILLabel[numCases]; for (int i = 0; i < numCases; i++) { cases[i] = CILInstructions.GetLabel(labs, (uint)(offset + size + targets[i])); } }
private static bool IsInsideToken(SCG.Dictionary<ParseRecord, bool> memoization, CompositeGrammar compositeGrammar, ParseRecord record) { bool res; if (memoization.TryGetValue(record, out res)) return res; if (record.Sequence.ParsingSequence.SequenceInfo is SequenceInfo.Ast) { var parser = record.Sequence.ParsingSequence.SequenceInfo.Parser; res = compositeGrammar.Tokens.ContainsKey(parser) || compositeGrammar.VoidTokens.ContainsKey(parser); memoization[record] = res; if(res) return res; } foreach (var caller in record.Sequence.Callers) { res = IsInsideToken(memoization, compositeGrammar, caller); if (res) { memoization[record] = true; return true; } } memoization[record] = false; return false; }
private void RandomChitLayout(SCG.IEnumerable<Tile> tiles, SCG.IEnumerable<ProductionChit> chits) { foreach (var t in AllTiles) t.Chits.Clear(); IList<ProductionChit> chitRandom = new ArrayList<ProductionChit>(); chitRandom.AddAll(from chit in chits orderby RNG.Next() select chit); foreach (Tile h in (from t in tiles where t.HexTile != null && t.HexTile.IsLand && t.HexTile.TileType != Core.TileType.Desert select t)) { h.Chits.Add(chitRandom.RemoveLast()); } }
private void RandomBoardLayout(SCG.IEnumerable<Tile> tiles, SCG.IEnumerable<HexTile> hexes, int x, int y, int maxd) { RandomBoardLayout(tiles, hexes, x, y, x, y, maxd, maxd); }
public UserCode(SCG.IReadOnlyDictionary<int, ISubsystem> subsystems, SCG.IReadOnlyList<ICommand> commands) { this.subsystems = subsystems; this.commands = commands; }
public static void ClearAllScripts(SCG.IEnumerable<TransactionInput> tis) { ContractsCommon.NotNull(tis, "tis"); Contract.Requires(Contract.ForAll(tis, ti => ti != null)); Contract.Ensures(Contract.ForAll(tis, ti => ti.Script.Atoms.IsEmpty)); foreach (var ti in tis) { ti.Script.Atoms.Clear(); } }
// Given a sequence of strings, return only the first member of each // anagram class. public static SCG.IEnumerable<String> FirstAnagramOnly(SCG.IEnumerable<String> ss) { HashSet<HashBag<char>> anagrams = new HashSet<HashBag<char>>(); foreach (String s in ss) { HashBag<char> anagram = AnagramClass(s); if (!anagrams.Contains(anagram)) { anagrams.Add(anagram); yield return s; } } }
public static IRobot Create( IterativeRobotConfiguration configuration, SCG.IReadOnlyDictionary<int, ISubsystem> subsystems, SCG.IReadOnlyList<ICommand> commands, IDebugRenderContext debugRenderContext ) { var userCode = new UserCode(subsystems, commands); return new SubsystemCommandBasedIterativeRobot(configuration, userCode, debugRenderContext); }