1 patch for repository http://darcs.haskell.org/packages/containers: Thu Apr 28 20:23:16 PDT 2011 Luis Casillas * Add Int index functions to Data.Set, to match those already in Data.Map New patches: [Add Int index functions to Data.Set, to match those already in Data.Map Luis Casillas **20110429032316 Ignore-this: e0ff20825922d8005f34d9ed8aaaa573 ] { hunk ./Data/Set.hs 96 -- * Fold , fold + -- * Indexed + , lookupIndex + , findIndex + , elemAt + , deleteAt + -- * Min\/Max , findMin , findMax hunk ./Data/Set.hs 164 -- We do not use BangPatterns, because they are not in any standard and we -- want the compilers to be compiled by as many compilers as possible. #define STRICT_1_OF_2(fn) fn arg _ | arg `seq` False = undefined +#define STRICT_1_OF_3(fn) fn arg _ _ | arg `seq` False = undefined +#define STRICT_2_OF_3(fn) fn _ arg _ | arg `seq` False = undefined {-------------------------------------------------------------------- Operators hunk ./Data/Set.hs 353 #endif +{-------------------------------------------------------------------- + Indexing +--------------------------------------------------------------------} +-- | /O(log n)/. Return the /index/ of an element. The index is a +-- number from /0/ up to, but not including, the 'size' of the +-- set. Calls 'error' when the element is not a 'member' of the map. +-- +-- > findIndex 2 (fromList [5, 3]) Error: element is not in the set +-- > findIndex 3 (fromList [5, 3]) == 0 +-- > findIndex 5 (fromList [5, 3]) == 1 +-- > findIndex 6 (fromList [5, 3]) Error: element is not in the set + +findIndex :: Ord a => a -> Set a -> Int +findIndex k t + = case lookupIndex k t of + Nothing -> error "Set.findIndex: element is not in the set" + Just idx -> idx +#if __GLASGOW_HASKELL__ >= 700 +{-# INLINABLE findIndex #-} +#endif + +-- | /O(log n)/. Lookup the /index/ of an element. The index is a +-- number from /0/ up to, but not including, the 'size' of the set. +-- +-- > isJust (lookupIndex 2 (fromList [5, 3])) == False +-- > fromJust (lookupIndex 3 (fromList [5, 3])) == 0 +-- > fromJust (lookupIndex 5 (fromList [5, 3])) == 1 +-- > isJust (lookupIndex 6 (fromList [5, 3])) == False + +lookupIndex :: Ord a => a -> Set a -> Maybe Int +lookupIndex k = lkp k 0 + where + STRICT_1_OF_3(lkp) + STRICT_2_OF_3(lkp) + lkp _ _ Tip = Nothing + lkp key idx (Bin _ kx l r) + = case compare key kx of + LT -> lkp key idx l + GT -> lkp key (idx + size l + 1) r + EQ -> let idx' = idx + size l in idx' `seq` Just idx' +#if __GLASGOW_HASKELL__ >= 700 +{-# INLINABLE lookupIndex #-} +#endif + +-- | /O(log n)/. Retrieve an element by /index/. Calls 'error' when an +-- invalid index is used. +-- +-- > elemAt 0 (fromList [5, 3]) == (3,"b") +-- > elemAt 1 (fromList [5, 3]) == (5, "a") +-- > elemAt 2 (fromList [5, 3]) Error: index out of range + +elemAt :: Int -> Set a -> a +STRICT_1_OF_2(elemAt) +elemAt _ Tip = error "Set.elemAt: index out of range" +elemAt i (Bin _ kx l r) + = case compare i sizeL of + LT -> elemAt i l + GT -> elemAt (i-sizeL-1) r + EQ -> kx + where + sizeL = size l +#if __GLASGOW_HASKELL__ >= 700 +{-# INLINABLE elemAt #-} +#endif + +-- | /O(log n)/. Delete the element at /index/. +-- +-- > deleteAt 0 (fromList [5, 3]) == singleton 5 +-- > deleteAt 1 (fromList [5, 3]) == singleton 3 +-- > deleteAt 2 (fromList [5, 3]) Error: index out of range +-- > deleteAt (-1) (fromList [5, 3]) Error: index out of range + +deleteAt :: Int -> Set a -> Set a +deleteAt i t = i `seq` + case t of + Tip -> error "Set.deleteAt: index out of range" + Bin sx kx l r -> case compare i sizeL of + LT -> balanceR kx (deleteAt i l) r + GT -> balanceL kx l (deleteAt (i-sizeL-1) r) + EQ -> glue l r + where + sizeL = size l +-- deleteAt i0 t = i0 `seq` go i0 t +-- where +-- go _ Tip = error "Set.deleteAt: index out of range" +-- go i (Bin sx kx l r) = case compare i sizeL of +-- LT -> balance kx (go i l) r +-- GT -> balance kx l (go (i-sizeL-1) r) +-- EQ -> glue l r +-- where +-- sizeL = size l +#if __GLASGOW_HASKELL__ >= 700 +{-# INLINABLE deleteAt #-} +#endif + + {-------------------------------------------------------------------- Minimal, Maximal --------------------------------------------------------------------} } Context: [TAG git migration Ian Lynagh **20110331135042 Ignore-this: cd283e33454a9c5b01e034ddc6a871a ] Patch bundle hash: f5fae40759f955b661ae51b8a22804bb67034684