Advertisement
MagnusArias

SBO | Kolejne funkcje C#

Nov 16th, 2019
349
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.73 KB | None | 0 0
  1.  [Microsoft.SqlServer.Server.SqlFunction]
  2.     public static int? GetHammingDistance(SqlString s, SqlString t)
  3.     {
  4.         if (!s.IsNull && !t.IsNull)
  5.         {
  6.             if (s.ToString().Length != t.ToString().Length) return -1;
  7.  
  8.             int distance =
  9.                 s.ToString().ToCharArray()
  10.                 .Zip(t.ToString().ToCharArray(), (c1, c2) => new { c1, c2 })
  11.                 .Count(m => m.c1 != m.c2);
  12.  
  13.             return distance;
  14.         }
  15.         else return null;
  16.     }
  17.  
  18.     [Microsoft.SqlServer.Server.SqlFunction]
  19.     public static int? GetDamerauLevenshteinDistance(SqlString inS, SqlString inT)
  20.     {
  21.         if (!inS.IsNull && !inT.IsNull)
  22.         {
  23.             string s = inS.ToString();
  24.             string t = inT.ToString();
  25.  
  26.             var bounds = new { Height = s.Length + 1, Width = t.Length + 1 };
  27.  
  28.             int[,] matrix = new int[bounds.Height, bounds.Width];
  29.  
  30.             for (int height = 0; height < bounds.Height; height++) { matrix[height, 0] = height; };
  31.             for (int width = 0; width < bounds.Width; width++) { matrix[0, width] = width; };
  32.  
  33.             for (int height = 1; height < bounds.Height; height++)
  34.             {
  35.                 for (int width = 1; width < bounds.Width; width++)
  36.                 {
  37.                     int cost = (s[height - 1] == t[width - 1]) ? 0 : 1;
  38.                     int insertion = matrix[height, width - 1] + 1;
  39.                     int deletion = matrix[height - 1, width] + 1;
  40.                     int substitution = matrix[height - 1, width - 1] + cost;
  41.  
  42.                     int distance = Math.Min(insertion, Math.Min(deletion, substitution));
  43.  
  44.                     if (height > 1 && width > 1 && s[height - 1] == t[width - 2] && s[height - 2] == t[width - 1])
  45.                     {
  46.                         distance = Math.Min(distance, matrix[height - 2, width - 2] + cost);
  47.                     }
  48.  
  49.                     matrix[height, width] = distance;
  50.                 }
  51.             }
  52.  
  53.             return matrix[bounds.Height - 1, bounds.Width - 1];
  54.         }
  55.         else return null;
  56.     }
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65. public class Wynik
  66. {
  67.     [SqlFunction(FillRowMethodName = "FillRow")]
  68.     public static IEnumerable InitMethod(String logname)
  69.     {
  70.         return new EventLog(logname).Entries;
  71.     }
  72.  
  73.     public static void FillRow(Object obj, out SqlDateTime timeWritten, out SqlChars message, out SqlChars category, out long instanceId)
  74.     {
  75.         EventLogEntry eventLogEntry = obj as EventLogEntry;
  76.         timeWritten = new SqlDateTime(eventLogEntry.TimeWritten);
  77.         message = new SqlChars(eventLogEntry.Message);
  78.         category = new SqlChars(eventLogEntry.Category);
  79.         instanceId = eventLogEntry.InstanceId;
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement