<commit_before>
module Ivory.Tower.AST.Thread where

import Ivory.Tower.Types.Time
import Ivory.Tower.AST.Period
import Ivory.Tower.AST.Signal
import Ivory.Tower.AST.Chan

data Thread = SignalThread Signal
            | PeriodThread Period
            deriving (Eq, Show, Ord)

threadName :: Thread -> String
threadName (SignalThread s) = "thread_signal_" ++ signal_name s
threadName (PeriodThread p) = "thread_period_" ++ t
  where
  us = toMicroseconds (period_dt p)
  t = case us `mod` 1000 of
    0 -> (show (us `div` 1000)) ++ "ms"
    _ -> (show us) ++ "us"


threadChan :: Thread -> Chan
threadChan (PeriodThread p) = ChanPeriod p
threadChan (SignalThread s) = ChanSignal s

threadDeadline :: Thread -> Microseconds
threadDeadline (PeriodThread p) = period_dt p
threadDeadline (SignalThread s) = signal_deadline s<commit_msg>tower: thread Ord based on deadline with appropriate tiebreakers<commit_after>
module Ivory.Tower.AST.Thread
  ( Thread(..)
  , threadName
  , threadChan
  , threadDeadline
  ) where

import Ivory.Tower.Types.Time
import Ivory.Tower.AST.Period
import Ivory.Tower.AST.Signal
import Ivory.Tower.AST.Chan

data Thread = SignalThread Signal
            | PeriodThread Period
            deriving (Eq, Show)

threadName :: Thread -> String
threadName (SignalThread s) = "thread_signal_" ++ signal_name s
threadName (PeriodThread p) = "thread_period_" ++ t
  where
  us = toMicroseconds (period_dt p)
  t = case us `mod` 1000 of
    0 -> (show (us `div` 1000)) ++ "ms"
    _ -> (show us) ++ "us"


threadChan :: Thread -> Chan
threadChan (PeriodThread p) = ChanPeriod p
threadChan (SignalThread s) = ChanSignal s

threadDeadline :: Thread -> Microseconds
threadDeadline (PeriodThread p) = period_dt p
threadDeadline (SignalThread s) = signal_deadline s

instance Ord Thread where
  compare a b
    | threadDeadline a == threadDeadline b = tiebreak a b
    | otherwise = compare (threadDeadline a) (threadDeadline b)

tiebreak :: Thread -> Thread -> Ordering
-- Break tie between signal and period - period is greater
tiebreak (SignalThread _)  (PeriodThread _) = LT
tiebreak (PeriodThread _) (SignalThread _) = GT
-- Break tie between signals - based on signal name
tiebreak (SignalThread (Signal a _)) (SignalThread (Signal b _)) = compare a b
-- Periods are a set, should not need to break ties.
tiebreak (PeriodThread _) (PeriodThread _) = EQ
{'fraction_non_alphanumeric': 0.09911429776465626, 'fraction_numerical': 0.007591733445803458, 'mean_word_length': 3.8706365503080082, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5307173', 'n_tokens_mistral': 771, 'n_tokens_neox': 720, 'n_words': 247}