Added levenshtein.c3 to working files. Fixes to reverse indexing. Added min/max functions. Tentatively removed "opaque"

This commit is contained in:
Christoffer Lerno
2022-01-26 17:39:30 +01:00
parent 8eb295bf5b
commit e5bcb74822
21 changed files with 223 additions and 134 deletions

View File

@@ -0,0 +1,26 @@
module levenshtein;
import std::math;
// This levenshtein exercises C3 subarrays.
fn int levenshtein(char[] s, char[] t)
{
// if either string is empty, difference is inserting all chars
// from the other
if (!s.len) return t.len;
if (!t.len) return s.len;
// if last letters are the same, the difference is whatever is
// required to edit the rest of the strings
if (s[^1] == t[^1]) return levenshtein(s[0..^2], t[0..^2]);
// else try:
// changing last letter of s to that of t; or
// remove last letter of s; or
// remove last letter of t,
// any of which is 1 edit plus editing the rest of the strings
int a = levenshtein(s[0..^2], t[0..^2]);
int b = levenshtein(s, t[0..^2]);
int c = levenshtein(s[0..^2], t);
return @min(@min(a, b), c) + 1;
}

View File

@@ -1,24 +0,0 @@
module levenshtein;
fn int levenshtein(String s, String t)
{
// if either string is empty, difference is inserting all chars
// from the other
if (!s.len) return t.len;
if (!t.len) return s.len;
// if last letters are the same, the difference is whatever is
// required to edit the rest of the strings
if (s[^1] == t[^1]) return levenshtein(s[0..^2], t[0..^2]);
// else try:
// changing last letter of s to that of t; or
// remove last letter of s; or
// remove last letter of t,
// any of which is 1 edit plus editing the rest of the strings
int a = levenshtein(s[0..^2], t[0..^2]);
int b = levenshtein(s, t[0..^2]);
int c = levenshtein(s[0..^2], t);
return @max(@max(a, b), c) + 1;
}

View File

@@ -373,7 +373,7 @@ fn void Parser.expect(Parser* p, TokenKind k)
const u32 MaxDiag = 128;
public struct TomlReader @opaque
public struct TomlReader
{
char[MaxDiag] message;
Blocks* blocks;

View File

@@ -67,6 +67,16 @@ const QUAD_MIN_EXP = -16481;
const QUAD_EPSILON = 1.92592994438723585305597794258492732e-34;
*/
macro max(x, y) @autoimport
{
return x > y ? x : y;
}
macro min(x, y) @autoimport
{
return x < y ? x : y;
}
fn double log10(double x) @inline
{
return $$log10(x);

View File

@@ -6,7 +6,7 @@ macro max(a, b)
}
// Horribly bad implementation of BigInt with add/sub.
public struct BigInt @opaque
public struct BigInt
{
byte* number;
uint length;

View File

@@ -6,7 +6,7 @@ macro @max(a, b)
}
// Horribly bad implementation of BigInt with add/sub.
public struct BigInt @opaque
public struct BigInt
{
byte& number;
uint length;