Skip to content
Snippets Groups Projects
Select Git revision
  • 29e6fe27e9ab5ec47896029a4fca7cae91b00dc1
  • master default protected
  • eval-coop-taskrun
  • eval-steal-all-on-notification
  • ma-eval
  • alternative-fsearch-impls
  • waitfd-5.18
  • max-open
  • test-open-concurrency-limit
  • waitfd
  • ripripgrep
  • ci
  • io-stealing
  • sqpoll
  • muhq-ma-results
15 results

summarize.py

Blame
  • PossibleMajorityCandidate.hs 609 B
    {-# LANGUAGE BangPatterns #-}
    {-# LANGUAGE ScopedTypeVariables #-}
    
    module Data.Algorithm.PossibleMajorityCandidate where
    
    import Data.Foldable
    
    -- | Compute the possible majority candidate in t
    --
    -- The result is undefined if t is empty
    --
    -- Runtime is O(n)
    possibleMajorityCandidate :: forall a t. (Eq a, Foldable t) => t a -> a
    possibleMajorityCandidate = snd . foldl' loop (0, undefined)
      where
        loop :: (Int, a) -> a -> (Int, a)
        loop (!count,pmc) !a
          | count == 0  = (1, a)
          | a == pmc    = (count+1, pmc)
          | otherwise   = (count-1, pmc)
    {-# INLINE possibleMajorityCandidate #-}