Click here to Skip to main content
15,894,097 members
Articles / Programming Languages / C#

Concurrent Programming - Investigating Task Messaging To Achieve Synchronization Free Inter-Task Communication

Rate me:
Please Sign up or sign in to vote.
4.80/5 (15 votes)
7 Jan 2008CPOL17 min read 47.4K   208   51  
Further studies of Parallel FX.
/* Copyright (c) 2006-2007 Coskun Oba
 * *****************************************************************************
 * 
 * SCI - Scientific Software Platform
 * Copyright (c) 2006-2007
 * All rights reserved.
 * 
 * Coskun Oba
 * oba.coskun@hotmail.com
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the <organization> nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY COSKUN OBA ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL COSKUN OBA BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * *****************************************************************************/

using System;
using System.Collections.Generic;
using System.Text;

namespace Sci.Math
{
    #region Fraction
    /// <summary>
    /// Represents the fractional numbers. 
    /// A fraction is a quotient of numbers, representing the quantity obtained 
    /// when the numerator is divided by the denominator.
    /// </summary>
    public struct Fraction
    {
        /// <summary>
        /// Represents the numerator of the fractional scalar.
        /// </summary>
        private int numerator;

        /// <summary>
        /// Represents the denominator of the fractional scalar.
        /// </summary>
        private int denominator;

        /// <summary>
        /// Constructs a Fraction from the given value. 
        /// The numerator of the fraction is equal to the given value, and the denominator = 1.
        /// </summary>
        /// <param name="number">Numerator.</param>
        public Fraction(int number)
        {
            this.numerator = number;
            this.denominator = 1;
            ReduceFraction(ref this);
        }

        /// <summary>
        /// Constructs a Fraction from the given values.
        /// </summary>
        /// <param name="numerator">Numerator.</param>
        /// <param name="denominator">Denominator.</param>
        public Fraction(int numerator, int denominator)
        {
            this.numerator = numerator;
            this.denominator = denominator;

            if (denominator < 0)
            {
                this.numerator = -numerator;
                this.denominator = -denominator;
            }
            ReduceFraction(ref this);
        }

        #region Properties
        /// <summary>
        /// Gets/Sets the numerator.
        /// </summary>
        public int Numerator
        {
            get { return numerator; }
            set { numerator = value; }
        }

        /// <summary>
        /// Gets/Sets the denominator.
        /// </summary>
        public int Denominator
        {
            get { return denominator; }
            set 
            {
                denominator = value;
                if (denominator < 0)
                {
                    numerator = -numerator;
                    denominator = -denominator;
                }
            }
        }
        #endregion

        #region Special Values
        /// <summary>
        /// Represents the scalar zero (0) as a Fraction.
        /// </summary>
        public static readonly Fraction Zero = new Fraction(0, 1);
        #endregion

        #region Static Methods
        /// <summary>
        /// Returns the inverse of the given fraction.
        /// </summary>
        /// <param name="f">Fraction.</param>
        /// <returns>Returns the inverse of the given fraction.</returns>
        public static Fraction Inverse(Fraction f)
        {
            return new Fraction(f.denominator, f.numerator);
        }

        /// <summary>
        /// Reduces the given fraction using 
        /// the greatest common divisor of the numerator and the denominator.
        /// </summary>
        /// <param name="f">Fraction to be reduced.</param>
        public static void ReduceFraction(ref Fraction f)
        {
            int gcd = Sci.Math.Function.GCD(f.numerator, f.denominator);
            f.numerator /= gcd;
            f.denominator /= gcd;
        }
        #endregion

        #region Operator Overloading
        /// <summary>
        /// Represents the implicit coversion of an integer value to a Fraction.
        /// </summary>
        /// <param name="number">An integer scalar to be converted.</param>
        /// <returns>Returns a Fraction.</returns>
        public static implicit operator Fraction(int number)
        {
            return new Fraction(number, 1);
        }

        /// <summary>
        /// Equality operator.
        /// Checks the equality of two given Fractions.
        /// </summary>
        /// <param name="rhs">Fraction on the Right-Hand Side of the equality operator.</param>
        /// <param name="lhs">Fraction on the Left-Hand Side of the equality operator.</param>
        /// <returns>Returns true if the given Fractions are equal, false otherwise.</returns>
        public static bool operator ==(Fraction rhs, Fraction lhs)
        {
            int left = rhs.numerator * lhs.denominator;
            int right = rhs.denominator * lhs.numerator;

            return (left.Equals(right));
        }

        /// <summary>
        /// Inequality operator.
        /// Checks the inequality of two given Fractions.
        /// </summary>
        /// <param name="rhs">Fraction on the Right-Hand Side of the equality operator.</param>
        /// <param name="lhs">Fraction on the Left-Hand Side of the equality operator.</param>
        /// <returns>Returns true if the given Fractions are not equal, false otherwise.</returns>
        public static bool operator !=(Fraction rhs, Fraction lhs)
        {
            return !(rhs == lhs);
        }

        /// <summary>
        /// Unary negation operator.
        /// Negates the given Fraction.
        /// </summary>
        /// <param name="f">Fraction to be negated.</param>
        /// <returns>
        /// Returns a Fraction which is the negative equavalent of the given Fraction.
        /// </returns>
        public static Fraction operator -(Fraction f)
        {
            return new Fraction(-f.numerator, f.denominator);
        }

        /// <summary>
        /// Binary plus operator.
        /// </summary>
        /// <param name="op1">First operand.</param>
        /// <param name="op2">Second operand.</param>
        /// <returns>Returns a Fraction which represents the f = op1 + op2 operation.</returns>
        public static Fraction operator +(Fraction op1, Fraction op2)
        {
            if (op1.denominator.Equals(op2.denominator))
            {
                return new Fraction(op1.numerator + op2.numerator, op1.denominator);
            }

            int numerator = op1.numerator * op2.denominator +
                                op1.denominator * op2.numerator;
            int denominator = op1.denominator * op2.denominator;

            return new Fraction(numerator, denominator);
        }

        /// <summary>
        /// Binary minus operator.
        /// </summary>
        /// <param name="op1">First operand.</param>
        /// <param name="op2">Second operand.</param>
        /// <returns>Returns a Fraction which represents the f = op1 - op2 operation.</returns>
        public static Fraction operator -(Fraction op1, Fraction op2)
        {
            return op1 + (-op2);
        }

        /// <summary>
        /// Binary multiplication operator.
        /// </summary>
        /// <param name="op1">First operand.</param>
        /// <param name="op2">Second operand.</param>
        /// <returns>Returns a Fraction which represents the f = op1 * op2 operation.</returns>
        public static Fraction operator *(Fraction op1, Fraction op2)
        {
            int numerator = op1.numerator * op2.numerator;
            int denominator = op1.denominator * op2.denominator;

            return new Fraction(numerator, denominator);
        }

        /// <summary>
        /// Binary division operator.
        /// </summary>
        /// <param name="op1">First operand.</param>
        /// <param name="op2">Second operand.</param>
        /// <returns>Returns a Fraction which represents the f = op1 / op2 operation.</returns>
        public static Fraction operator /(Fraction op1, Fraction op2)
        {
            int numerator = op1.numerator * op2.denominator;
            int denominator = op1.denominator * op2.numerator;

            return new Fraction(numerator, denominator);
        }
        #endregion

        #region Overriden Methods
        /// <summary>
        /// Checks whether the given object is equal to this Fraction.
        /// </summary>
        /// <param name="obj">Object to be checked if it is equal to this Fraction.</param>
        /// <returns>Returns True if the given object is equal to this Fraction, false otherwise.</returns>
        public override bool Equals(object obj)
        {
            return ((obj is Fraction)) ? this == (Fraction)obj : false;
        }

        /// <summary>
        /// Hashcode that reprsents this Fraction.
        /// </summary>
        /// <returns>Returns integer value of the hashcode.</returns>
        public override int GetHashCode()
        {
            return numerator.GetHashCode()^denominator.GetHashCode();
        }

        /// <summary>
        /// String representation of this Fraction
        /// </summary>
        /// <returns>Returns the string representation of this Fraction.</returns>
        public override string ToString()
        {
            StringBuilder s = new StringBuilder(numerator.ToString());
            if (denominator != 0 &&  denominator != 1)
            {
                s.Append("/");
                s.Append(denominator.ToString());
            }
            return s.ToString();
        }
        #endregion
    }
    #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)


Written By
Architect Interacx
United States United States
Blog: https://marcclifton.wordpress.com/
Home Page: http://www.marcclifton.com
Research: http://www.higherorderprogramming.com/
GitHub: https://github.com/cliftonm

All my life I have been passionate about architecture / software design, as this is the cornerstone to a maintainable and extensible application. As such, I have enjoyed exploring some crazy ideas and discovering that they are not so crazy after all. I also love writing about my ideas and seeing the community response. As a consultant, I've enjoyed working in a wide range of industries such as aerospace, boatyard management, remote sensing, emergency services / data management, and casino operations. I've done a variety of pro-bono work non-profit organizations related to nature conservancy, drug recovery and women's health.

Comments and Discussions