Unchpokable

Try to broke dictionary

Mar 6th, 2021
743
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. namespace RTF2
  2. {
  3.     using System.Collections.Generic;
  4.     using System.Collections;
  5.     using System;
  6.     using BenchmarkDotNet;
  7.     using BenchmarkDotNet.Running;
  8.     using System.IO;
  9.     using System.Runtime.Serialization.Formatters.Binary;
  10.     using System.Linq;
  11.  
  12.     class Program
  13.     {
  14.         static void Main(string[] args)
  15.         {
  16.             var sameHashedNumbers = Enumerable.Range(int.MinValue + 1, int.MaxValue - 1).Select(x => x.WithSameHashes()).MaxBy(x => x.Length);
  17.  
  18.             Console.WriteLine(sameHashedNumbers.Length);
  19.             using (var memory = new FileStream("save.dat", FileMode.OpenOrCreate))
  20.             {
  21.                 var formatter = new BinaryFormatter();
  22.                 formatter.Serialize(memory, sameHashedNumbers);
  23.                 memory.Position = 0;
  24.             }
  25.            
  26.             Console.ReadKey();
  27.  
  28.             BenchmarkRunner.Run<DictionaryBreaker>();
  29.             Console.ReadKey();
  30.         }
  31.     }
  32.  
  33.     public class DictionaryBreaker
  34.     {
  35.         [BenchmarkDotNet.Attributes.Benchmark(Description = "Normal number")]
  36.         public void AddNormalNumber()
  37.         {
  38.             int[] brokenNumbers;
  39.             using (var memory = new FileStream("save.dat", FileMode.OpenOrCreate))
  40.             {
  41.                 var formatter = new BinaryFormatter();
  42.                 brokenNumbers = (int[])formatter.Deserialize(memory);
  43.             }
  44.             var dict = new Dictionary<int, int>();
  45.             for (int i = 0; i < 50000; i++)
  46.                 dict.Add(i, i);
  47.         }
  48.  
  49.         [BenchmarkDotNet.Attributes.Benchmark(Description = "Broken numbers")]
  50.         public void AddFuckedNumbers()
  51.         {
  52.             var dict = new Dictionary<int, int>();
  53.             var value = 0;
  54.             int[] brokenNumbers;
  55.             using (var memory = new FileStream("save.dat", FileMode.OpenOrCreate))
  56.             {
  57.                 var formatter = new BinaryFormatter();
  58.                 brokenNumbers = (int[])formatter.Deserialize(memory);
  59.             }
  60.  
  61.             foreach (var item in brokenNumbers)
  62.                 dict.Add(item, value++);
  63.         }
  64.  
  65.        
  66.     }
  67.  
  68.     public static class Extensions
  69.     {
  70.         public static int[] WithSameHashes(this int src)
  71.         {
  72.             var stdHash = src.GetHashCode();
  73.             var sameHashedNumbers = new List<int>();
  74.             for (int i = int.MinValue; i < int.MaxValue; i++)
  75.             {
  76.                 if (i.GetHashCode() == stdHash)
  77.                 {
  78.                     //Console.WriteLine(i);
  79.                     sameHashedNumbers.Add(i);
  80.                 }
  81.             }
  82.             return sameHashedNumbers.ToArray();
  83.         }
  84.  
  85.         public static T[] MaxBy<T, TOut>(this IEnumerable<T[]> src, Func<T[], TOut> selector)
  86.             where T: IComparable<T>, IComparable
  87.             where TOut: IComparable<T>, IComparable
  88.         {
  89.             var tmpMax = src.First();
  90.             using (IEnumerator<T[]> enumerator = src.GetEnumerator())
  91.             {
  92.                 while (enumerator.MoveNext())
  93.                 {
  94.                     if (selector(enumerator.Current).CompareTo(selector(tmpMax)) > 0)
  95.                         tmpMax = enumerator.Current;
  96.                 }
  97.             }
  98.             return tmpMax;
  99.         }
  100.     }
  101. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×