Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ///<summary>
- ///This will be the code file to hold any CLR funcitons that the apex team uses.
- ///</summmary>
- /******Compiler Flags:******
- /fullpaths
- /platform:x64
- /nologo
- /target:library
- ****************************/
- using System;
- using System.Data.SqlTypes;
- public partial class UserDefinedFunctions
- {
- [Microsoft.SqlServer.Server.SqlFunction(IsDeterministic = true, IsPrecise = false)]
- public static SqlInt32 Levenshtein(SqlString S1, SqlString S2)
- {
- if (S1.IsNull || S2.IsNull)
- throw (new ArgumentNullException());
- int n = S1.Value.Length;
- int m = S2.Value.Length;
- int[,] d = new int[n + 1, m + 1];
- int cost = 0;
- if (n == 0)
- return m;
- if (m == 0)
- return n;
- for (int i = 0; i <= n; i++)
- d[i, 0] = i;
- for (int j = 0; j <= m; j++)
- d[0, j] = j;
- for (int i = 1; i <= n; i++)
- {
- for (int j = 1; j <= m; j++)
- {
- if (S1.Value[i-1] == S2.Value[j-1])
- cost = 0;
- else
- cost = 1;
- d[i, j] = System.Math.Min(System.Math.Min(d[i - 1, j] + 1, d[i, j - 1] + 1), d[i - 1, j - 1] + cost);
- }
- }
- return d[n, m];
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement