Show/Hide Toolbars

Waysys on the Web

(*

 

Class:      DateRange

Package:    com.waysysweb.util

Author:     W. Shaffer

Date:       13-Nov-2006

Description:

 

    This structure extends the set class to provide certain funtions specific

    to date sets.

 

Maintenance:

 

Date        Author  Description

----------- ------  -----------------------------------------------------------

13-Nov-2006 Shaffer File created

 

*)

 

structure DATERANGE = struct

 

    (*============  Exceptions ==============================================*)

 

    exception DateSetException of string;

 

    (*============  Imports     =============================================*)

    

    (* Set imports                                                           *)

 

    type ''a Set = ''a SET.Set;

    

    fun add(set : ''a Set, item : ''a) : ''a Set = SET.add(set, item);

    fun fetch(set : ''a Set) : ''a = SET.fetch(set);

    fun remove(set : ''a Set, item : ''a) : ''a Set = SET.remove(set, item);

    fun isEmpty(set : ''a Set) : bool = SET.isEmpty(set);

    val empty = SET.empty;

    

    (* Date imports                                                          *)

    

    type WayDate = WAYDATE.WayDate;

    

    fun increment(date1 : WayDate) : WayDate = WAYDATE.increment(date1);

    fun compare(date1 : WayDate, date2 : WayDate) : order =

        WAYDATE.compare(date1, date2);

 

    (*============  Types       =============================================*)

 

    type DateSet = WayDate Set;

 

    (*============  Constants  ==============================================*)

 

 

 

    (*============  Properties ==============================================*)

 

 

 

    (*============  Attributes  =============================================*)

 

 

 

    (*============  Invariant   =============================================*)

 

 

 

    (*============  Initialization ==========================================*)

 

 

 

    (*============  Operations  =============================================*)

 

    (* return a set of dates with every date starting at the from date to    *)

    (* the through date                                                      *)

    (* Precondition: true                                                    *)

    (* Postcondition: if compare(fromDate, date) != LESS andalso             *)

    (*                   compare(thruDate, date) != GREATER                  *)

    (*                then date inset range(fromDate, thruDate)              *)

    (*                else date notinset range(fromDate, thruDate)           *)

    fun range(fromDate : WayDate, thruDate : WayDate) : DateSet =

        if compare(fromDate, thruDate) = GREATER

        then empty

        else add(range(increment(fromDate), thruDate), fromDate);

        

    (* return the maximum date in a date set that satifies a predicate       *)

    fun maxInSet([] : DateSet, pred : WayDate -> bool, current : WayDate) :

        WayDate = current

      | maxInSet(set : DateSet, pred : WayDate -> bool, current : WayDate) :

        WayDate =

        let

            val nextDate = fetch(set);

            val rest = remove(set, nextDate);

        in

            if pred(nextDate) andalso (compare(nextDate, current) = GREATER)

            then maxInSet(rest, pred, nextDate)

            else maxInSet(rest, pred, current)

        end;

        

    (* return the maximum date in a date set that satifies a predicate       *)

    fun max(set : DateSet, pred : WayDate -> bool) : WayDate =

        if isEmpty(set) then raise DateSetException("max: set is empty")

        else

            let

                val date = fetch(set);

                val rest = remove(set, date)

            in

                maxInSet(rest, pred, date)

            end;

 

end (* DATERANGE *)