Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

A Simulator for Knuth's MIX Computer

, 2 Feb 2011 CPOL
Assembler and Simulator for Don Knuth's MIX Computer from The Art of Computer Programming.
MIXWare.zip
Examples
easter.mixal
maximum.mixal
perms.mixal
perms-input.deck
primes.mixal
MIX
MIX.csproj.user
Properties
MIXAsm
MIXAsm.csproj.user
Properties
MIXLib
Devices.cd
Machine.cd
MIXLib.csproj.user
Parser
Properties
MIXWare.pdf
using System;
using System.Collections.Generic;
using System.Text;

namespace MIXLib.Parser
{
    public class Set<TEnum>
    {
        #region "Data"

        bool flagsEnum;
        System.Collections.BitArray members;
        System.Collections.Generic.Dictionary<TEnum, int> mapMember2Index;
        #endregion

        #region "ctors"
        public Set()
        {
            Initialize();
            members = new System.Collections.BitArray(mapMember2Index.Count);
        }

        private Set(System.Collections.BitArray members)
        {
            Initialize();
            this.members = members;
        }

        private void Initialize()
        {
            if (typeof(TEnum).BaseType != typeof(System.Enum))
                throw new ApplicationException(string.Format("Generic type parameter <{0}> is not an enum type!",
                                                             typeof(TEnum).FullName));
            flagsEnum = typeof(TEnum).GetCustomAttributes(typeof(System.FlagsAttribute), true).Length > 0;

            int i = 0;
            mapMember2Index = new Dictionary<TEnum, int>();
            foreach (TEnum m in Enum.GetValues(typeof(TEnum)))
                mapMember2Index.Add(m, i++);
        }

        #endregion

        #region "Working with the set"
        public void Clear()
        {
            members.SetAll(false);
        }

        public Set<TEnum> Add(TEnum member)
        {
            members.Set(mapMember2Index[member], true);
            return this;
        }

        public Set<TEnum> Add(Set<TEnum> otherSet)
        {
            if (otherSet != null) members.Or(otherSet.members);
            return this;
        }

        public Set<TEnum> Remove(TEnum member)
        {
            members.Set(mapMember2Index[member], false);
            return this;
        }

        public Set<TEnum> Remove(Set<TEnum> otherSet)
        {
            if (otherSet != null)
                for (int i = 0; i < members.Count; i++)
                    if (otherSet.members[i])
                        members[i] = false;
            return this;
        }

        public Set<TEnum> Intersect(Set<TEnum> otherSet)
        {
            if (otherSet != null) members.And(otherSet.members);
            return this;
        }

        public bool Contains(TEnum member)
        {
            return members[mapMember2Index[member]];
        }

        public int Cardinality
        {
            get
            {
                return members.Length;
            }
        }

        #endregion

        #region "Overrides"

        public override bool Equals(object obj)
        {
            return this == (Set<TEnum>)obj;
        }

        public override int GetHashCode()
        {
            return members.GetHashCode();
        }

        public override string ToString()
        {
            string[] names = Enum.GetNames(typeof(TEnum));

            StringBuilder memberNames = new StringBuilder("[");
            for (int i = 0; i < members.Count; i++)
                if (members[i])
                {
                    if (memberNames.Length > 1) memberNames.Append(",");
                    memberNames.Append(names[i]);
                }
            memberNames.Append("]");

            return memberNames.ToString();
        }

        #endregion

        #region "Operators"

        // intersection
        public static Set<TEnum> operator &(Set<TEnum> left, Set<TEnum> right)
        {
            System.Collections.BitArray result = new System.Collections.BitArray(new bool[left.members.Count]);
            result.Or(left.members);
            if (right != null) result.And(right.members);
            return new Set<TEnum>(result);
        }

        // union
        public static Set<TEnum> operator |(Set<TEnum> left, Set<TEnum> right)
        {
            System.Collections.BitArray result = new System.Collections.BitArray(new bool[left.members.Count]);
            result.Or(left.members);
            if (right != null) result.Or(right.members);
            return new Set<TEnum>(result);
        }

        public static bool operator ==(Set<TEnum> left, Set<TEnum> right)
        {
            if (right != null)
            {
                for (int i = 0; i < left.members.Count; i++)
                    if (left.members[i] != right.members[i]) return false;
                return true;
            }
            else
                return false;
        }

        public static bool operator !=(Set<TEnum> left, Set<TEnum> right)
        {
            return !(left == right);
        }

        #endregion

        #region "Iterator"

        public IEnumerator<TEnum> GetEnumerator()
        {
            TEnum[] memberValues = (TEnum[])Enum.GetValues(typeof(TEnum));

            for (int i = 0; i < members.Count; i++)
                if (members[i])
                    yield return memberValues[i];
        }

        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

George Tryfonas
Engineer
Greece Greece
I am a software developer (mainly in C# and T-SQL) for a project management company in Athens, Greece. I have been working with computers since early 1987. I am adept at Pascal, C, C++, Java (my MSc was sponsored by Sun Microsystems), Lisp, Scheme, F#, C# VB.Net, Perl and some others that are too obscure to mention. When I want a quick and dirty solution to a programming problem I use a functional language, such as Haskell, Scheme or, more recently, F#.
 
I also play the keyboards and compose music.
 
---------------------------------------------------------
 
MSc Distributed Systems and Networks - University of Kent at Canterbury
BEng Computer Systems Engineering - University of Kent at Canterbury

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150327.1 | Last Updated 2 Feb 2011
Article Copyright 2011 by George Tryfonas
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid