Esempio n. 1
0
 public Surf.Bitmap Copy()
 {
     Surf.Bitmap B = new Surf.Bitmap(this.Length);
     for (int i = 0; i < this.Length; i++)
     {
         B.Set(i, this.Get(i));
     }
     return(B);
 }
Esempio n. 2
0
 protected override object EvaluateBITMAP(string value)
 {
     value = value.Substring(1, value.Length - 2);
     Surf.Bitmap bitArray = new Surf.Bitmap(value.Length);
     for (int i = 0; i < value.Length; i++)
     {
         bitArray.Set(i, (value[i] == '1'));
     }
     return(bitArray);
 }
Esempio n. 3
0
 public Surf.Bitmap Or(Surf.Bitmap bitmap)
 {
     if (this.Length != bitmap.Length)
     {
         throw new System.Exception("Bitmaps are not the same size.");
     }
     Surf.Bitmap C = new Surf.Bitmap(this.Length);
     for (int i = 0; i < this.Length; i++)
     {
         C.Set(i, this.Get(i) || bitmap.Get(i));
     }
     return(C);
 }
Esempio n. 4
0
        private static void Print(object value, System.IO.TextWriter writer, ref bool canNewline)
        {
            if (value is Node)
            {
                Node node = (Node)value;

                new Surf.Generator().Generate(node, writer);
            }
            else if (value is Surf.Bitmap)
            {
                Surf.Bitmap bitmap = (Surf.Bitmap)value;

                writer.Write("[");
                for (int i = 0; i < bitmap.Length; i++)
                {
                    writer.Write(bitmap.Get(i) ? "1" : "0");
                }
                writer.Write("]");
            }
            else if (value is Tuple)
            {
                Tuple tuple = (Tuple)value;

                writer.Write("( ");
                for (int i = 0; i < tuple.Count; i++)
                {
                    if (i > 0)
                    {
                        writer.Write(", ");
                    }
                    Print(tuple[i], writer, ref canNewline);
                }
                writer.Write(" )");
            }
            else if (value is Set)
            {
                Set set = (Set)value;

                bool isRelation = (canNewline && set.Count > 0 && set[0] is Surf.Tuple) ? true : false;
                if (isRelation)
                {
                    canNewline = false;
                }

                writer.Write("{ ");
                if (isRelation)
                {
                    writer.Write("\n");
                }
                for (int i = 0; i < set.Count; i++)
                {
                    if (i > 0)
                    {
                        writer.Write(", ");
                        if (isRelation)
                        {
                            writer.Write("\n");
                        }
                    }
                    if (isRelation)
                    {
                        writer.Write("   ");
                    }
                    Print(set[i], writer, ref canNewline);
                }
                if (isRelation)
                {
                    writer.Write("\n");
                }
                writer.Write(" }");
            }
            else if (value is string)
            {
                string s = (string)value;

                writer.Write("\"");
                writer.Write(Escape(s));
                writer.Write("\"");
            }
            else if (value is char)
            {
                char ch = (char)value;

                writer.Write("\'");
                writer.Write(Escape(ch.ToString()));
                writer.Write("\'");
            }
            else if (value == null)
            {
                writer.Write("");
            }
            else
            {
                writer.Write(value.ToString());
            }
        }
Esempio n. 5
0
        private object Apply(string identifier, System.Collections.ArrayList expressions)
        {
            #region Apply

            switch (identifier)
            {
            case "variables":
            {
                Assert(expressions.Count == 0, "Invalid arguments.");
                return(this._variables.Domain());
            }

            case "definitions":
            {
                Assert(expressions.Count == 0, "Invalid arguments.");
                return(this._definitions);
            }

            case "dependencies":
            {
                Assert(expressions.Count == 0, "Invalid arguments.");
                return(this._dependencies);
            }

            case "equals":
            {
                Assert(expressions.Count == 2, "Invalid arguments.");

                object a = expressions[0];
                object b = expressions[1];
                return(a.Equals(b));
            }

            case "union":
            {
                Assert(expressions.Count == 2, "Invalid arguments.");

                if (expressions[0] == null || expressions[1] == null)
                {
                    return(null);
                }
                else if (expressions[0] is Surf.Set && expressions[1] is Surf.Set)
                {
                    Set a = (Set)expressions[0];
                    Set b = (Set)expressions[1];
                    return(a.Union(b));
                }
                else if (expressions[0] is Surf.Bitmap && expressions[1] is Surf.Bitmap)
                {
                    Surf.Bitmap a = (Surf.Bitmap)expressions[0];
                    Surf.Bitmap b = (Surf.Bitmap)expressions[1];
                    return(a.Or(b));
                }
                else
                {
                    throw new System.Exception("Invalid expression.");
                }
            }

            case "intersection":
            {
                Assert(expressions.Count == 2, "Invalid arguments.");

                if (expressions[0] == null || expressions[1] == null)
                {
                    return(null);
                }
                else if (expressions[0] is Surf.Set && expressions[1] is Surf.Set)
                {
                    Set a = (Set)expressions[0];
                    Set b = (Set)expressions[1];
                    return(a.Intersection(b));
                }
                else if (expressions[0] is Surf.Bitmap && expressions[1] is Surf.Bitmap)
                {
                    Surf.Bitmap a = (Surf.Bitmap)expressions[0];
                    Surf.Bitmap b = (Surf.Bitmap)expressions[1];
                    return(a.And(b));
                }
                else
                {
                    throw new System.Exception("Invalid expression.");
                }
            }

            case "difference":
            {
                Assert(expressions.Count == 2, "Invalid arguments.");

                if (expressions[0] == null || expressions[1] == null)
                {
                    return(null);
                }
                else if (expressions[0] is Surf.Set && expressions[1] is Surf.Set)
                {
                    Set a = (Set)expressions[0];
                    Set b = (Set)expressions[1];
                    return(a.Difference(b));
                }
                else if (expressions[0] is Surf.Bitmap && expressions[1] is Surf.Bitmap)
                {
                    Surf.Bitmap a = (Surf.Bitmap)expressions[0];
                    Surf.Bitmap b = (Surf.Bitmap)expressions[1];
                    return(a.And(b.Not()));
                }
                else
                {
                    throw new System.Exception("Invalid expression.");
                }
            }

            case "cartesian_product":
            {
                Assert(expressions.Count == 2, "Invalid arguments.");

                Set a = (Set)expressions[0];
                Set b = (Set)expressions[1];
                if (a == null || b == null)
                {
                    return(null);
                }
                return(a.CartesianProduct(b));
            }

            case "domain":
            {
                Assert(expressions.Count == 1, "Invalid arguments.");

                Set a = (Set)expressions[0];
                if (a == null)
                {
                    return(null);
                }
                return(a.Domain());
            }

            case "range":
            {
                Assert(expressions.Count == 1, "Invalid arguments.");

                Set a = (Set)expressions[0];
                if (a == null)
                {
                    return(null);
                }
                return(a.Range());
            }

            case "transpose":
            {
                Assert(expressions.Count == 1, "Invalid arguments.");

                Set a = (Set)expressions[0];
                if (a == null)
                {
                    return(null);
                }
                return(a.Transpose());
            }

            case "compose":
            {
                Assert(expressions.Count == 2, "Invalid arguments.");

                Set a = (Set)expressions[0];
                Set b = (Set)expressions[1];
                if (a == null || b == null)
                {
                    return(null);
                }
                return(a.Compose(b));
            }

            case "nest":
            {
                Assert(expressions.Count == 1, "Invalid arguments.");

                Set a = (Set)expressions[0];
                if (a == null)
                {
                    return(null);
                }
                return(a.Nest());
            }

            case "unnest":
            {
                Assert(expressions.Count == 1, "Invalid arguments.");

                Set a = (Set)expressions[0];
                if (a == null)
                {
                    return(null);
                }
                return(a.Unnest());
            }

            case "in":
            {
                Assert(expressions.Count == 2, "Invalid arguments.");

                Set    S   = (Set)expressions[0];
                object key = expressions[1];
                if (S == null || key == null)
                {
                    return(null);
                }
                return(S.IsDefined(key));
            }

            case "method":
            {
                Assert(expressions.Count == 4, "Invalid arguments.");

                string name          = (string)expressions[0];
                string type_name     = (string)expressions[1];
                string assembly_name = (string)expressions[2];
                string method_name   = (string)expressions[3];

                Water.Operators.LoadMethodOperator.LoadMethod(name, type_name, assembly_name, method_name);
                return(null);
            }

            default:
            {
                if (Water.Environment.IsConstant(identifier))
                {
                    Water.Operator op = (Water.Operator)Water.Environment.Identify(identifier);
                    return(op.Evaluate(new Water.List(expressions)));
                }
                else if (this._variables.IsDefined(identifier))
                {
                    Assert(expressions.Count > 0, "Invalid arguments.");
                    Set S = (Set)this._variables.Apply(identifier);
                    if (expressions.Count == 1)
                    {
                        return(S.Apply(expressions[0]));
                    }
                    else
                    {
                        return(S.Apply(new Surf.Tuple(expressions)));
                    }
                }
                else
                {
                    throw new System.Exception("Function does not exist.");
                }
            }
            }

            #endregion
        }