happstack-ixset-6.1.0: Efficient relational queries on Haskell sets.

Safe HaskellSafe-Infered

Happstack.Data.IxSet

Contents

Synopsis

Set type

data IxSet a

Set with associatex indexes.

Instances

Typeable1 IxSet 
(Data ctx a, Data ctx [a], Sat (ctx (IxSet a)), Sat (ctx [a]), Indexable a, Data a, Ord a) => Data ctx (IxSet a) 
(Eq a, Ord a, Typeable a) => Eq (IxSet a) 
Data a => Data (IxSet a) 
(Eq a, Ord a, Typeable a) => Ord (IxSet a) 
(Ord a, Read a, Typeable a, Indexable a) => Read (IxSet a) 
(Ord a, Show a) => Show (IxSet a) 
Version (IxSet a) 
(Serialize a, Ord a, Typeable a, Indexable a) => Serialize (IxSet a) 
(Indexable a, Ord a, Data a, Default a) => Default (IxSet a) 
(Indexable a, Typeable a, Ord a) => Monoid (IxSet a) 
(SafeCopy a, Ord a, Typeable a, Indexable a) => SafeCopy (IxSet a) 

class Indexable a where

Indexable class defines objects that can be members of IxSet.

Methods

empty :: IxSet a

Method empty defines what an empty IxSet for this particular type should look like. It should have all necessary indexes. Use ixSet function to create the set and fill it in with ixFun and ixGen.

noCalcs :: t -> ()

Function to be used for calcs in inferIxSet when you don't want any calculated values.

inferIxSet :: String -> Name -> Name -> [Name] -> Q [Dec]

Template Haskell helper function for automatically building an Indexable instance from a data type, e.g.

 data Foo = Foo Int String

and

 $(inferIxSet "FooDB" ''Foo 'noCalcs [''Int,''String])

will build a type synonym

 type FooDB = IxSet Foo

with Int and String as indexes.

WARNING: The type specified as the first index must be a type which appears in all values in the IxSet or toList, toSet and serialization will not function properly. You will be warned not to do this by runtime error. You can always use the element type itself. For example:

 $(inferIxSet "FooDB" ''Foo 'noCalcs [''Foo, ''Int, ''String])

ixSet :: [Ix a] -> IxSet a

Create an IxSet using a list of indexes. Useful in Indexable empty method. Use ixFun and ixGen as list elements.

 instance Indexable Type where
     empty = ixSet [ ...
                     ixFun getIndex1
                     ixGen (Proxy :: Proxy Index2Type)
                   ]

First index in the list must contain all objects in set, doing otherwise result in runtime error.

ixFun :: (Ord b, Typeable b) => (a -> [b]) -> Ix a

Create a functional index. Provided function should return a list of indexes where value should be found.

 getIndexes value = [...indexes...]
 instance Indexable Type where
     empty = ixSet [ ixFun getIndexes ]

This is the recommended way to create indexes.

ixGen :: forall a b. (Data a, Ord b, Typeable b) => Proxy b -> Ix aSource

Create a generic index. Provided example is used only as type source so you may use a Proxy. The ixGen uses flatten to traverse value using its Data instance.

 instance Indexable Type where
     empty = ixSet [ ixGen (Proxy :: Proxy Type) ]

In production systems consider using ixFun in place of ixGen as the former one is much faster.

Changes to set

type IndexOp = forall k a. (Ord k, Ord a) => k -> a -> Map k (Set a) -> Map k (Set a)

change :: (Typeable a, Indexable a, Ord a) => IndexOp -> a -> IxSet a -> IxSet a

Higher order operator for modifying IxSets. Use this when your final function should have the form a -> IxSet a -> IxSet a, e.g. insert or delete.

insert :: (Typeable a, Ord a, Indexable a) => a -> IxSet a -> IxSet a

Inserts an item into the IxSet. If your data happens to have primary key this function might not be what you want. See updateIx.

delete :: (Typeable a, Ord a, Indexable a) => a -> IxSet a -> IxSet a

Removes an item from the IxSet.

updateIx :: (Indexable a, Ord a, Typeable a, Typeable k) => k -> a -> IxSet a -> IxSet a

Will replace the item with index k. Only works if there is at most one item with that index in the IxSet. Will not change IxSet if you have more then 1 item with given index.

deleteIx :: (Indexable a, Ord a, Typeable a, Typeable k) => k -> IxSet a -> IxSet a

Will delete the item with index k. Only works if there is at most one item with that index in the IxSet. Will not change IxSet if you have more then 1 item with given index.

Creation

fromSet :: (Indexable a, Ord a, Typeable a) => Set a -> IxSet a

Converts a Set to an IxSet.

fromList :: (Indexable a, Ord a, Typeable a) => [a] -> IxSet a

Converts a list to an IxSet.

Conversion

toSet :: Ord a => IxSet a -> Set a

Converts an IxSet to a Set of its elements.

toList :: Ord a => IxSet a -> [a]

Converts an IxSet to its list of elements.

toAscList :: forall k a. (Indexable a, Typeable a, Typeable k) => Proxy k -> IxSet a -> [a]Source

Converts an IxSet to its list of elements.

List will be sorted in ascending order by the index k.

The list may contain duplicate entries if a single value produces multiple keys.

toDescList :: forall k a. (Indexable a, Typeable a, Typeable k) => Proxy k -> IxSet a -> [a]Source

Converts an IxSet to its list of elements.

List will be sorted in descending order by the index k.

The list may contain duplicate entries if a single value produces multiple keys.

getOne :: Ord a => IxSet a -> Maybe a

If the IxSet is a singleton it will return the one item stored in it. If IxSet is empty or has many elements this function returns Nothing.

getOneOr :: Ord a => a -> IxSet a -> a

Like getOne with a user provided default.

Size checking

size :: Ord a => IxSet a -> Int

Returns the number of unique items in the IxSet.

null :: IxSet a -> Bool

Return True if the IxSet is empty, False otherwise.

Set operations

(&&&) :: (Ord a, Typeable a, Indexable a) => IxSet a -> IxSet a -> IxSet a

An infix intersection operation.

(|||) :: (Ord a, Typeable a, Indexable a) => IxSet a -> IxSet a -> IxSet a

An infix union operation.

union :: (Ord a, Typeable a, Indexable a) => IxSet a -> IxSet a -> IxSet a

Takes the union of the two IxSets.

intersection :: (Ord a, Typeable a, Indexable a) => IxSet a -> IxSet a -> IxSet a

Takes the intersection of the two IxSets.

Indexing

(@=) :: (Indexable a, Typeable a, Ord a, Typeable k) => IxSet a -> k -> IxSet a

Infix version of getEQ.

(@<) :: (Indexable a, Typeable a, Ord a, Typeable k) => IxSet a -> k -> IxSet a

Infix version of getLT.

(@>) :: (Indexable a, Typeable a, Ord a, Typeable k) => IxSet a -> k -> IxSet a

Infix version of getGT.

(@<=) :: (Indexable a, Typeable a, Ord a, Typeable k) => IxSet a -> k -> IxSet a

Infix version of getLTE.

(@>=) :: (Indexable a, Typeable a, Ord a, Typeable k) => IxSet a -> k -> IxSet a

Infix version of getGTE.

(@><) :: (Indexable a, Typeable a, Ord a, Typeable k) => IxSet a -> (k, k) -> IxSet a

Returns the subset with indexes in the open interval (k,k).

(@>=<) :: (Indexable a, Typeable a, Ord a, Typeable k) => IxSet a -> (k, k) -> IxSet a

Returns the subset with indexes in [k,k).

(@><=) :: (Indexable a, Typeable a, Ord a, Typeable k) => IxSet a -> (k, k) -> IxSet a

Returns the subset with indexes in (k,k].

(@>=<=) :: (Indexable a, Typeable a, Ord a, Typeable k) => IxSet a -> (k, k) -> IxSet a

Returns the subset with indexes in [k,k].

(@+) :: (Indexable a, Typeable a, Ord a, Typeable k) => IxSet a -> [k] -> IxSet a

Creates the subset that has an index in the provided list.

(@*) :: (Indexable a, Typeable a, Ord a, Typeable k) => IxSet a -> [k] -> IxSet a

Creates the subset that matches all the provided indexes.

getEQ :: (Indexable a, Typeable a, Ord a, Typeable k) => k -> IxSet a -> IxSet a

Returns the subset with an index equal to the provided key. The set must be indexed over key type, doing otherwise results in runtime error.

getLT :: (Indexable a, Typeable a, Ord a, Typeable k) => k -> IxSet a -> IxSet a

Returns the subset with an index less than the provided key. The set must be indexed over key type, doing otherwise results in runtime error.

getGT :: (Indexable a, Typeable a, Ord a, Typeable k) => k -> IxSet a -> IxSet a

Returns the subset with an index greater than the provided key. The set must be indexed over key type, doing otherwise results in runtime error.

getLTE :: (Indexable a, Typeable a, Ord a, Typeable k) => k -> IxSet a -> IxSet a

Returns the subset with an index less than or equal to the provided key. The set must be indexed over key type, doing otherwise results in runtime error.

getGTE :: (Indexable a, Typeable a, Ord a, Typeable k) => k -> IxSet a -> IxSet a

Returns the subset with an index greater than or equal to the provided key. The set must be indexed over key type, doing otherwise results in runtime error.

getRange :: (Indexable a, Typeable k, Ord a, Typeable a) => k -> k -> IxSet a -> IxSet a

Returns the subset with an index within the interval provided. The bottom of the interval is closed and the top is open, i. e. [k1;k2). The set must be indexed over key type, doing otherwise results in runtime error.

groupBy :: (Typeable k, Typeable t) => IxSet t -> [(k, [t])]

Returns lists of elements paired with the indexes determined by type inference.

groupAscBy :: (Typeable k, Typeable t) => IxSet t -> [(k, [t])]

Returns lists of elements paired with the indexes determined by type inference.

The resulting list will be sorted in ascending order by k. The values in '[t]' will be sorted in ascending order as well.

groupDescBy :: (Typeable k, Typeable t) => IxSet t -> [(k, [t])]

Returns lists of elements paired with the indexes determined by type inference.

The resulting list will be sorted in descending order by k.

NOTE: The values in '[t]' are currently sorted in ascending order. But this may change if someone bothers to add toDescList. So do not rely on the sort order of '[t]'.

Index creation helpers

flatten :: (Typeable a, Data a, Typeable b) => a -> [b]

Generically traverses the argument to find all occurences of values of type b and returns them as a list.

This function properly handles String as String not as [Char].

flattenWithCalcs :: (Data c, Typeable a, Data a, Typeable b) => (a -> c) -> a -> [b]

Generically traverses the argument and calculated values to find all occurences of values of type b and returns them as a list. Equivalent to:

 flatten (x,calcs x)

This function properly handles String as String not as [Char].

Debugging and optimisation

stats :: Ord a => IxSet a -> (Int, Int, Int, Int)

Statistics about IxSet. This function returns quadruple consisting of 1. total number of elements in the set 2. number of declared indexes 3. number of keys in all indexes 4. number of values in all keys in all indexes. This can aid you in debugging and optimisation.