Click here to Skip to main content
15,886,017 members
Articles / Programming Languages / C#

An Implementation of Regular Expression Parser in C#

Rate me:
Please Sign up or sign in to vote.
4.91/5 (43 votes)
24 Jun 2009CPOL8 min read 185.3K   7.2K   116  
An article on how one can implement regular expression parser
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

namespace RegularExpression
{
  /// <summary>
  /// this class represent a Set in Mathmatics, nothing else.
  /// Since .NET does not have any built-in class for Set, I decided to create one myself.
  /// Since this programm does many Set opertions, this class will help writing clean code
  /// Many of the methods of this class have not been used in this program.
  /// </summary>
  public class Set : CollectionBase
  {
    public object this[int index]
    {
      get
      {
        return ((object)List[index]);
      }
      set
      {
        List[index] = value;
      }
    }

    public int AddElement(object element)
    {
      if (Contains(element) == false)
      {
        return List.Add(element);
      }
      return -1;
    }

    public void AddElementRange(object[] arrValue)
    {
      foreach (object obj in arrValue)
      {
        if (Contains(obj) == false)
        {
          List.Add(obj);
        }
      }
    }

    public void Union(Set setValue)
    {
      foreach (object obj in setValue)
      {
        if (Contains(obj) == false)
        {
          List.Add(obj);
        }
      }
    }

    public bool IsSubset(Set setValue)
    {
      foreach (object obj in setValue)
      {
        if (Contains(obj) == false)
        {
          return false;
        }
      }
      return true;
    }

    public bool IsProperSubset(Set setValue)
    {
      if (GetCardinality() > setValue.GetCardinality() && IsSubset(setValue) )
      {
        return true;
      }

      return false;

    }

    public void Subtract(Set setValue)
    {
      foreach (object obj in setValue)
      {
        if (Contains(obj) == true)
        {
          RemoveElement(obj);
        }
      }
    }

    public bool IsEqual(Set setValue)
    {
      if (GetCardinality() == setValue.GetCardinality() && IsSubset(setValue))
      {
        return true;
      }

      return false;

    }

    public bool IsEmpty()
    {
      return List.Count == 0 ? true : false;
    }

    public int GetCardinality()
    {
      return List.Count;
    }

    public bool ElementExist(object value)
    {
      return Contains(value);
    }

    public int IndexOf(object value)
    {
      return (List.IndexOf(value));
    }

    public bool RemoveElement(object value)
    {
      if (Contains(value) == true)
      {
        List.Remove(value);
        return true;
      }

      return false;

    }

    private bool Contains(object value)
    {
      // If value is not of type Int16, this will return false.
      return List.Contains(value);
    }
    
    protected override void OnInsert(int index, object value)
    {
      if (value == null)
      {
        throw new ArgumentException("Element cannot not be null.");
      }

      if (Contains(value) == true )
      {
        throw new ArgumentException("Element already exists in the set.", "value: " + value.ToString());
      }

      

    }

    public Array ToArray(Type type)
    {
      return base.InnerList.ToArray(type);
    }


  }
}

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)


Written By
Denmark Denmark
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions