packages feed

zcache (empty) → 0.0.0

raw patch · 4 files changed

+103/−0 lines, 4 filesdep +arraydep +basedep +containerssetup-changed

Dependencies added: array, base, containers, mersenne-random-pure64

Files

+ Data/ZCache.hs view
@@ -0,0 +1,64 @@+{-# LANGUAGE ExistentialQuantification #-}+{-| Zobrist keys compactly represent the state of perfect information games. -}+module Data.ZCache (+    ZSet(..), ZMap(..), zSet, zMap,+    flipPos, zArray) where+import Data.Array.Unboxed+import Data.Word+import Data.Bits+import System.Random.Mersenne.Pure64+import Data.Set (Set)+import qualified Data.Set as Set+import Data.Map (Map)+import Data.List+import qualified Data.Map as Map++type ZArray a = UArray a Word64++makeRandom :: (PureMT,Int) -> Maybe (Word64,(PureMT,Int))+makeRandom (_,0) = Nothing+makeRandom (p,i) = Just (w,(p',i-1)) where+    (w,p') = randomWord64 p++-- |Create the internal array used for the zobrist key creation+zArray :: Ix a => Word64 -> (a,a) -> Int -> ZArray a+zArray seed dim l = listArray dim (unfoldr makeRandom (pureMT seed,l))++-- |Sets of board states, indexed by zobrist keys+data ZSet a = ZSet Word64 (ZArray a) (Set Word64) deriving (Eq, Show)++-- |Construct a ZSet+zSet :: Ix a =>+    Word64 -- ^ Seed to use for random number generation+    -> (a,a) -- ^ Bounds for piece position values+    -> Int -- ^ Total number of piece positions+    -> ZSet a+zSet seed dim l = ZSet (0::Word64) (zArray seed dim l) Set.empty++flipPos :: Ix a =>+    a -- ^ Most recent move in the game+    -> ZSet a -- ^ The set to insert into+    -> Maybe (ZSet a) -- ^ Final set or Nothing if the board state exists already+flipPos x (ZSet c a s) = if Set.member c' s then Nothing else Just$ ZSet c' a (Set.insert c' s) where+    c' = xor c (a!x)++-- |Maps from board states, indexed by zobrist keys+data ZMap k v = ZMap Word64 (ZArray k) (Map Word64 v) deriving (Eq, Show)++-- |Construct a ZMap+zMap :: forall a b . Ix a =>+    Word64 -- ^ Seed to use for random number generation+    -> (a,a) -- ^ Bounds for piece position values+    -> Int -- ^ Total number of piece positions+    -> ZMap a b+zMap seed dim l = ZMap (0::Word64) (zArray seed dim l) Map.empty++-- |Store a value into a ZMap at a board state+storePos :: Ix a =>+    a -- ^ Most recent move in the game+    -> b -- ^ Value to be associated with the board state+    -> (ZMap a b) -- ^ ZMap to use for storage+    -> Maybe (ZMap a b) -- ^ Final map or Nothing if the board state exists already++storePos k v (ZMap c a m) = if Map.member c' m then Nothing else Just$ ZMap c' a (Map.insert c' v m) where+    c' = xor c (a!k)
+ LICENSE view
@@ -0,0 +1,20 @@+Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++1. Redistributions of source code must retain the above copyright notice,+   this list of conditions and the following disclaimer.++2. Redistributions in binary form must reproduce the above copyright notice,+   this list of conditions and the following disclaimer in the documentation+   and/or other materials provided with the distribution.++THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,+INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND+FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE+DEVELOPERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,+PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;+OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,+WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR+OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF+ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ Setup.lhs view
@@ -0,0 +1,3 @@+#!/usr/bin/env runhaskell+> import Distribution.Simple+> main = defaultMain
+ zcache.cabal view
@@ -0,0 +1,16 @@+name:                zcache+version:             0.0.0+synopsis:            Zobrist keys for game state tracking+description:         ZCache provides zobrist key generation for perfect information games.+                     It also includes convenience data structures  (maps and sets) that+                     use zobrist keys internally. +category:            Data+license:             BSD3+license-file:        LICENSE+author:              Sam Anklesaria+maintainer:          amsay@amsay.net+build-depends:       base+build-type:          Simple+exposed-modules:     Data.ZCache+build-depends:       base >= 3 && < 5, array < 1, mersenne-random-pure64 < 1, containers < 1+Homepage:            https://patch-tag.com/r/salazar/zcache/