Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add ucd_get_value, inflate code for enumerated/catalog properties
  • Loading branch information
sorear committed Dec 19, 2011
1 parent 5abbd8a commit d9e5c69
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions lib/UCD.cs
Expand Up @@ -16,6 +16,7 @@
namespace Niecza.UCD {
abstract class Property {
public abstract int[] GetRanges(Variable filter);
public abstract string GetValue(int cp);

protected static bool DoMatch(string value, Variable filter) {
Variable r = Kernel.RunInferior(filter.Fetch().InvokeMethod(
Expand All @@ -34,6 +35,23 @@ class LimitedProperty : Property {
this.values = values;
}

public override string GetValue(int cp) {
int lix = 0;
int hix = data.Length / 2;

while (true) {
if ((hix - lix) <= 1) {
return values[data[lix*2+1]][0];
}
int mix = (lix + hix) / 2;
if (cp >= data[mix*2]) {
lix = mix;
} else {
hix = mix;
}
}
}

public override int[] GetRanges(Variable filter) {
bool[] cfilter = new bool[values.Length];
for (int i = 0; i < values.Length; i++) {
Expand Down Expand Up @@ -142,6 +160,26 @@ static class DataSet {
new string[] { "N" }, new string[] { "Y" } });
}

static object InflateEnum(int[] loc) {
List<string[]> names = new List<string[]>();
int rpos2 = loc[6];
while (rpos2 < loc[7]) {
names.Add(new string[] { AsciiZ(ref rpos2) });
}
int rpos0 = loc[2];
int rpos1 = loc[4];
int[] data = new int[(loc[5] - loc[4]) * 2];
int lix = 0;
for (int i = 0; i < data.Length / 2; i++) {
lix += (int)BER(ref rpos0);
data[2*i] = lix;
data[2*i+1] = bits[rpos1++];
//if (Trace) Console.WriteLine("inflate: {0} = {1}", data[2*i], names[data[2*i+1]][0]);
}

return new LimitedProperty(data, names.ToArray());
}

[MethodImpl(MethodImplOptions.Synchronized)]
public static object GetTable(string name) {
if (cache == null)
Expand All @@ -163,6 +201,9 @@ static class DataSet {
case (byte)'B':
r = InflateBinary(loc);
break;
case (byte)'E':
r = InflateEnum(loc);
break;
default:
throw new NieczaException("Unhandled type code " + (char)bits[loc[0]]);
}
Expand All @@ -183,4 +224,11 @@ public partial class Builtins {
cranges[i] = Builtins.MakeInt(rranges[i]);
return Builtins.MakeParcel(cranges);
}

public static Variable ucd_get_value(Variable tbl, Variable ch) {
Property p = (Property)DataSet.GetTable(
tbl.Fetch().mo.mro_raw_Str.Get(tbl));
return MakeStr(p.GetValue(
(int) ch.Fetch().mo.mro_raw_Numeric.Get(ch)));
}
}

0 comments on commit d9e5c69

Please sign in to comment.