Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add support for property value aliases
  • Loading branch information
sorear committed Dec 19, 2011
1 parent 808b2c1 commit e4cb8d0
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 6 deletions.
32 changes: 26 additions & 6 deletions lib/UCD.cs
Expand Up @@ -85,6 +85,7 @@ static class DataSet {
static byte[] bits;
static Dictionary<string,int[]> directory;
static Dictionary<string,string> aliases;
static Dictionary<Prod<string,string>,string[]> val_aliases;
static bool Trace;

const int FILES = 4;
Expand Down Expand Up @@ -149,6 +150,7 @@ static class DataSet {
static void InflateAliases() {
int[] loc = directory["!PropertyAlias"];
aliases = new Dictionary<string, string>();
val_aliases = new Dictionary<Prod<string,string>,string[]>();

int rpos = loc[2];
while (rpos < loc[3]) {
Expand All @@ -159,9 +161,26 @@ static class DataSet {
if (Trace) Console.WriteLine("Alias {0} -> {1}", alias, main);
}
}

loc = directory["!pva"];
rpos = loc[2];
List<string> aset = new List<string>();
while (rpos < loc[3]) {
string tbl = AsciiZ(ref rpos);
string canon = AsciiZ(ref rpos);
if (canon == "n/a")
canon = AsciiZ(ref rpos);
string alias;
aset.Add(canon);
while ((alias = AsciiZ(ref rpos)).Length != 0)
aset.Add(alias);
if (Trace) Console.WriteLine("Alias {0},{1} -> {2}", tbl, canon, Kernel.JoinS(", ", aset));
val_aliases[Prod.C(tbl, canon)] = aset.ToArray();
aset.Clear();
}
}

static object InflateBinary(int[] loc) {
static object InflateBinary(string name, int[] loc) {
List<int> vec = new List<int>();
int rpos = loc[2];
int last = 0;
Expand All @@ -173,14 +192,15 @@ static class DataSet {
ntyp = 1 - ntyp;
}
return new LimitedProperty(vec.ToArray(), new string[][] {
new string[] { "N" }, new string[] { "Y" } });
val_aliases[Prod.C(name,"N")],
val_aliases[Prod.C(name,"Y")] });
}

static object InflateEnum(int[] loc) {
static object InflateEnum(string name, int[] loc) {
List<string[]> names = new List<string[]>();
int rpos2 = loc[6];
while (rpos2 < loc[7]) {
names.Add(new string[] { AsciiZ(ref rpos2) });
names.Add(val_aliases[Prod.C(name, AsciiZ(ref rpos2))]);
}
int rpos0 = loc[2];
int rpos1 = loc[4];
Expand Down Expand Up @@ -220,10 +240,10 @@ static class DataSet {

switch (bits[loc[0]]) {
case (byte)'B':
r = InflateBinary(loc);
r = InflateBinary(name, loc);
break;
case (byte)'E':
r = InflateEnum(loc);
r = InflateEnum(name, loc);
break;
default:
throw new NieczaException("Unhandled type code " + (char)bits[loc[0]]);
Expand Down
38 changes: 38 additions & 0 deletions lib/Utils.cs
Expand Up @@ -7,6 +7,44 @@
using Niecza.Serialization;

namespace Niecza {
// Partially taken from mono's System.Tuple by Zoltan Varga and Marek Safar
public static class Prod {
public static Prod<T1,T2> C<T1,T2>(T1 v1, T2 v2) { return new Prod<T1,T2>(v1, v2); }
}
public class Prod<T1,T2> {
T1 v1;
T2 v2;
public Prod(T1 v1, T2 v2) {
this.v1 = v1;
this.v2 = v2;
}

public override bool Equals (object other) {
var t = other as Prod<T1, T2>;
if (t == null)
return false;
if (t.v1 != null || v1 != null) {
if (t.v1 == null || v1 == null || !v1.Equals(t.v1))
return false;
}
if (t.v2 != null || v2 != null) {
if (t.v2 == null || v2 == null || !v2.Equals(t.v2))
return false;
}
return true;
}

public override int GetHashCode() {
int h = (v1 == null ? 0 : v1.GetHashCode());
h = (h << 5) - h + (v2 == null ? 0 : v2.GetHashCode());
return h;
}

public override string ToString() {
return string.Format("({0}, {1})", v1, v2);
}
}

public sealed class VarDeque : IFreeze {
private Variable[] data;
private int head;
Expand Down

0 comments on commit e4cb8d0

Please sign in to comment.