diff options
author | Marc Sunet <jeannekamikaze@gmail.com> | 2012-08-08 21:15:41 +0200 |
---|---|---|
committer | Marc Sunet <jeannekamikaze@gmail.com> | 2012-08-08 21:15:41 +0200 |
commit | ad2c98ba8a1380baad5b648acb2c2b7f7c474101 (patch) | |
tree | bd89c984a21b982246919e53564536de9d7c3330 | |
parent | 5bd3c77b0e75051a5e9ae0490cef5b8abc167838 (diff) |
Moved IDStore to Store.ID. Created generic Store
-rw-r--r-- | Spear.cabal | 3 | ||||
-rw-r--r-- | Spear.lkshs | 12 | ||||
-rw-r--r-- | Spear.lkshw | 2 | ||||
-rw-r--r-- | Spear/Sys/Store.hs | 114 | ||||
-rw-r--r-- | Spear/Sys/Store/ID.hs (renamed from Spear/IDStore.hs) | 2 |
5 files changed, 124 insertions, 9 deletions
diff --git a/Spear.cabal b/Spear.cabal index f025de2..d30cbae 100644 --- a/Spear.cabal +++ b/Spear.cabal | |||
@@ -16,7 +16,7 @@ library | |||
16 | StateVar -any, base -any, bytestring -any, directory -any, | 16 | StateVar -any, base -any, bytestring -any, directory -any, |
17 | mtl -any, transformers -any, resource-simple -any, parsec >=3.1.3, | 17 | mtl -any, transformers -any, resource-simple -any, parsec >=3.1.3, |
18 | containers -any, vector -any, array -any | 18 | containers -any, vector -any, array -any |
19 | exposed-modules: Spear.Math.Triangle Spear.IDStore | 19 | exposed-modules: Spear.Math.Triangle |
20 | Spear.Physics.Types Spear.Physics.World Spear.App | 20 | Spear.Physics.Types Spear.Physics.World Spear.App |
21 | Spear.App.Application Spear.App.Input Spear.Assets.Image | 21 | Spear.App.Application Spear.App.Input Spear.Assets.Image |
22 | Spear.Assets.Model Spear.Collision Spear.Collision.AABB | 22 | Spear.Assets.Model Spear.Collision Spear.Collision.AABB |
@@ -34,6 +34,7 @@ library | |||
34 | Spear.Render.StaticModel Spear.Render.Texture Spear.Scene.Graph | 34 | Spear.Render.StaticModel Spear.Render.Texture Spear.Scene.Graph |
35 | Spear.Scene.Light Spear.Scene.Loader Spear.Scene.Scene | 35 | Spear.Scene.Light Spear.Scene.Loader Spear.Scene.Scene |
36 | Spear.Scene.SceneResources Spear.Setup Spear.Sys.Timer | 36 | Spear.Scene.SceneResources Spear.Setup Spear.Sys.Timer |
37 | Spear.Sys.Store Spear.Sys.Store.ID | ||
37 | Spear.Updatable | 38 | Spear.Updatable |
38 | exposed: True | 39 | exposed: True |
39 | buildable: True | 40 | buildable: True |
diff --git a/Spear.lkshs b/Spear.lkshs index a3f492f..042ccc8 100644 --- a/Spear.lkshs +++ b/Spear.lkshs | |||
@@ -1,18 +1,18 @@ | |||
1 | Version of session file format: | 1 | Version of session file format: |
2 | 1 | 2 | 1 |
3 | Time of storage: | 3 | Time of storage: |
4 | "Wed Aug 8 13:25:10 CEST 2012" | 4 | "Wed Aug 8 20:32:17 CEST 2012" |
5 | Layout: VerticalP (TerminalP {paneGroups = fromList [], paneTabs = Just TopP, currentPage = 1, detachedId = Nothing, detachedSize = Nothing}) (HorizontalP (TerminalP {paneGroups = fromList [("Browser",HorizontalP (TerminalP {paneGroups = fromList [], paneTabs = Nothing, currentPage = 0, detachedId = Nothing, detachedSize = Nothing}) (HorizontalP (TerminalP {paneGroups = fromList [], paneTabs = Nothing, currentPage = 0, detachedId = Nothing, detachedSize = Nothing}) (TerminalP {paneGroups = fromList [], paneTabs = Nothing, currentPage = 0, detachedId = Nothing, detachedSize = Nothing}) 273) 205)], paneTabs = Just BottomP, currentPage = 0, detachedId = Nothing, detachedSize = Nothing}) (TerminalP {paneGroups = fromList [], paneTabs = Nothing, currentPage = 0, detachedId = Nothing, detachedSize = Nothing}) 716) 954 | 5 | Layout: VerticalP (TerminalP {paneGroups = fromList [], paneTabs = Just TopP, currentPage = 4, detachedId = Nothing, detachedSize = Nothing}) (HorizontalP (TerminalP {paneGroups = fromList [("Browser",HorizontalP (TerminalP {paneGroups = fromList [], paneTabs = Nothing, currentPage = 0, detachedId = Nothing, detachedSize = Nothing}) (HorizontalP (TerminalP {paneGroups = fromList [], paneTabs = Nothing, currentPage = 0, detachedId = Nothing, detachedSize = Nothing}) (TerminalP {paneGroups = fromList [], paneTabs = Nothing, currentPage = 0, detachedId = Nothing, detachedSize = Nothing}) 267) 200)], paneTabs = Just BottomP, currentPage = 0, detachedId = Nothing, detachedSize = Nothing}) (TerminalP {paneGroups = fromList [], paneTabs = Nothing, currentPage = 0, detachedId = Nothing, detachedSize = Nothing}) 702) 954 |
6 | Population: [(Just (ErrorsSt ErrorsState),[SplitP RightP,SplitP TopP]),(Just (FilesSt FilesState),[SplitP RightP,SplitP TopP]),(Just (BufferSt (BufferState "/home/jeanne/programming/haskell/Spear/demos/simple-scene/GameObject.hs" 3129)),[SplitP LeftP]),(Just (BufferSt (BufferState "/home/jeanne/programming/haskell/Spear/Spear/IDStore.hs" 186)),[SplitP LeftP]),(Just (InfoSt (InfoState Nothing)),[SplitP RightP,SplitP TopP,GroupP "Browser",SplitP BottomP,SplitP BottomP]),(Just (LogSt LogState),[SplitP RightP,SplitP BottomP]),(Just (ModulesSt (ModulesState 286 (PackageScope False,False) (Nothing,Nothing) (ExpanderState {packageExp = ([],[]), packageExpNoBlack = ([],[]), packageDExp = ([],[]), packageDExpNoBlack = ([],[]), workspaceExp = ([],[]), workspaceExpNoBlack = ([[5]],[]), workspaceDExp = ([],[]), workspaceDExpNoBlack = ([],[]), systemExp = ([],[]), systemExpNoBlack = ([],[])}))),[SplitP RightP,SplitP TopP,GroupP "Browser",SplitP BottomP,SplitP TopP]),(Just (BufferSt (BufferState "/home/jeanne/programming/haskell/Spear/Spear/Physics.hs" 207)),[SplitP LeftP]),(Just (BufferSt (BufferState "/home/jeanne/programming/haskell/Spear/Spear/Physics/Rigid.hs" 2175)),[SplitP LeftP]),(Just (BufferSt (BufferState "/home/jeanne/programming/haskell/Spear/Spear/Math/Spatial.hs" 0)),[SplitP LeftP]),(Just (WorkspaceSt WorkspaceState),[SplitP RightP,SplitP TopP,GroupP "Browser",SplitP TopP]),(Just (BufferSt (BufferState "/home/jeanne/programming/haskell/Spear/Spear/Physics/World.hs" 3781)),[SplitP LeftP]),(Just (BufferSt (BufferState "/home/jeanne/programming/haskell/Spear/demos/simple-scene/main.hs" 1603)),[SplitP LeftP])] | 6 | Population: [(Just (ErrorsSt ErrorsState),[SplitP RightP,SplitP TopP]),(Just (FilesSt FilesState),[SplitP RightP,SplitP TopP]),(Just (BufferSt (BufferState "/home/jeanne/programming/haskell/Spear/demos/simple-scene/GameObject.hs" 8164)),[SplitP LeftP]),(Just (BufferSt (BufferState "/home/jeanne/programming/haskell/Spear/demos/simple-scene/GameState.hs" 670)),[SplitP LeftP]),(Just (InfoSt (InfoState (Just (Real (RealDescr {dscName' = "UpdateGO", dscMbTypeStr' = Just "type UpdateGO =\n Bool -> Input -> Float -> State [GameMessage] GameObject", dscMbModu' = Just (PM {pack = PackageIdentifier {pkgName = PackageName "simple-scene", pkgVersion = Version {versionBranch = [0,1,0,0], versionTags = []}}, modu = ModuleName ["GameObject"]}), dscMbLocation' = Just (Location {locationSLine = 43, locationSCol = 1, locationELine = 43, locationECol = 73}), dscMbComment' = Nothing, dscTypeHint' = TypeDescr, dscExported' = False}))))),[SplitP RightP,SplitP TopP,GroupP "Browser",SplitP BottomP,SplitP BottomP]),(Just (LogSt LogState),[SplitP RightP,SplitP BottomP]),(Just (ModulesSt (ModulesState 328 (PackageScope False,False) (Just (ModuleName ["Spear","IDStore"]),Nothing) (ExpanderState {packageExp = ([],[]), packageExpNoBlack = ([[0]],[]), packageDExp = ([],[]), packageDExpNoBlack = ([],[]), workspaceExp = ([],[]), workspaceExpNoBlack = ([[5]],[]), workspaceDExp = ([],[]), workspaceDExpNoBlack = ([],[]), systemExp = ([],[]), systemExpNoBlack = ([],[])}))),[SplitP RightP,SplitP TopP,GroupP "Browser",SplitP BottomP,SplitP TopP]),(Just (BufferSt (BufferState "/home/jeanne/programming/haskell/Spear/Spear/Physics.hs" 207)),[SplitP LeftP]),(Just (BufferSt (BufferState "/home/jeanne/programming/haskell/Spear/Spear/Physics/Rigid.hs" 2175)),[SplitP LeftP]),(Just (WorkspaceSt WorkspaceState),[SplitP RightP,SplitP TopP,GroupP "Browser",SplitP TopP]),(Just (BufferSt (BufferState "/home/jeanne/programming/haskell/Spear/Spear/Physics/World.hs" 1269)),[SplitP LeftP]),(Just (BufferSt (BufferState "/home/jeanne/programming/haskell/Spear/demos/simple-scene/main.hs" 1294)),[SplitP LeftP])] |
7 | Window size: (1841,964) | 7 | Window size: (1841,964) |
8 | Completion size: | 8 | Completion size: |
9 | (750,400) | 9 | (750,400) |
10 | Workspace: Just "/home/jeanne/programming/haskell/Spear/Spear.lkshw" | 10 | Workspace: Just "/home/jeanne/programming/haskell/Spear/Spear.lkshw" |
11 | Active pane: Just "IDStore.hs" | 11 | Active pane: Just "Modules" |
12 | Toolbar visible: | 12 | Toolbar visible: |
13 | True | 13 | True |
14 | FindbarState: (False,FindState {entryStr = "asd", entryHist = ["gravity","asdad","rotSpeed","azimuth","mandatory","mandao","col","forward","MouseButton"], replaceStr = "mandatory'", replaceHist = [], caseSensitive = False, entireWord = False, wrapAround = False, regex = False, lineNr = 1}) | 14 | FindbarState: (False,FindState {entryStr = "asd", entryHist = ["Triangle","transforma","gravity","asdad","rotSpeed","azimuth","mandatory","mandao","col","forward","MouseButton"], replaceStr = "CTriangle", replaceHist = [], caseSensitive = True, entireWord = False, wrapAround = False, regex = False, lineNr = 1}) |
15 | Recently opened files: | 15 | Recently opened files: |
16 | ["/home/jeanne/programming/haskell/Spear/demos/simple-scene/Spear/IDStore.hs","/home/jeanne/programming/haskell/Spear/Spear/Physics/Types.hs","/home/jeanne/programming/haskell/Spear/Spear/Collision/Collision.hs","/home/jeanne/programming/haskell/Spear/Spear/Collision/Collisioner.hs","/home/jeanne/programming/haskell/Spear/Spear/App/Input.hs"] | 16 | ["/home/jeanne/programming/haskell/Spear/Spear/Assets/Model/Model.h","/home/jeanne/programming/haskell/Spear/Spear/Assets/Model.hsc","/home/jeanne/programming/haskell/Spear/Spear/Math/Spatial.hs","/home/jeanne/programming/haskell/Spear/Spear/Math/Triangle.hs","/home/jeanne/programming/haskell/Spear/Spear/Render/Triangle.hs","/home/jeanne/programming/haskell/Spear/Spear/Assets/Model/Model.c","/home/jeanne/programming/haskell/Spear/Spear/Scene/Loader.hs","/home/jeanne/programming/haskell/Spear/Spear/IDStore.hs","/home/jeanne/programming/haskell/Spear/demos/simple-scene/Spear/IDStore.hs","/home/jeanne/programming/haskell/Spear/Spear/Physics/Types.hs","/home/jeanne/programming/haskell/Spear/Spear/Collision/Collision.hs","/home/jeanne/programming/haskell/Spear/Spear/Collision/Collisioner.hs"] |
17 | Recently opened workspaces: | 17 | Recently opened workspaces: |
18 | ["/home/jeanne/leksah.lkshw"] \ No newline at end of file | 18 | ["/home/jeanne/leksah.lkshw"] \ No newline at end of file |
diff --git a/Spear.lkshw b/Spear.lkshw index cc572e4..2291729 100644 --- a/Spear.lkshw +++ b/Spear.lkshw | |||
@@ -1,7 +1,7 @@ | |||
1 | Version of workspace file format: | 1 | Version of workspace file format: |
2 | 1 | 2 | 1 |
3 | Time of storage: | 3 | Time of storage: |
4 | "Wed Aug 8 15:15:08 CEST 2012" | 4 | "Wed Aug 8 21:04:06 CEST 2012" |
5 | Name of the workspace: | 5 | Name of the workspace: |
6 | "Spear" | 6 | "Spear" |
7 | File paths of contained packages: | 7 | File paths of contained packages: |
diff --git a/Spear/Sys/Store.hs b/Spear/Sys/Store.hs new file mode 100644 index 0000000..3d5d794 --- /dev/null +++ b/Spear/Sys/Store.hs | |||
@@ -0,0 +1,114 @@ | |||
1 | module Spear.Sys.Store | ||
2 | ( | ||
3 | Store | ||
4 | , Index | ||
5 | , emptyStore | ||
6 | , store | ||
7 | , storeFree | ||
8 | , element | ||
9 | ) | ||
10 | where | ||
11 | |||
12 | |||
13 | import Data.Maybe (isJust, isNothing) | ||
14 | import Data.Vector as V | ||
15 | import Control.Monad.State -- test | ||
16 | import Text.Printf -- test | ||
17 | |||
18 | |||
19 | type Index = Int | ||
20 | |||
21 | |||
22 | data Store a = Store | ||
23 | { assigned :: Vector (Maybe a) -- ^ An array of objects. | ||
24 | , last :: Index -- ^ The greatest index assigned so far. | ||
25 | } | ||
26 | deriving Show | ||
27 | |||
28 | |||
29 | -- | Create an empty store. | ||
30 | emptyStore :: Store a | ||
31 | emptyStore = Store V.empty (-1) | ||
32 | |||
33 | |||
34 | -- | Store the given element in the store. | ||
35 | store :: a -> Store a -> (Index, Store a) | ||
36 | store elem s@(Store assigned last) = | ||
37 | if last == V.length assigned - 1 | ||
38 | then case findIndex isNothing assigned of | ||
39 | Just i -> assign i elem s | ||
40 | Nothing -> store elem $ Store (assigned V.++ V.replicate (max 1 last + 1) Nothing) last | ||
41 | else | ||
42 | assign (last+1) elem s | ||
43 | |||
44 | |||
45 | -- Assign a slot the given element in the store. | ||
46 | assign :: Index -> a -> Store a -> (Index, Store a) | ||
47 | assign i elem (Store assigned last) = | ||
48 | let assigned' = assigned // [(i,Just elem)] | ||
49 | in (i, Store assigned' (max last i)) | ||
50 | |||
51 | |||
52 | -- | Free the given element from the store. | ||
53 | storeFree :: Index -> Store a -> Store a | ||
54 | storeFree i (Store assigned last) = | ||
55 | let assigned' = assigned // [(i,Nothing)] | ||
56 | in if i == last | ||
57 | then case findLastIndex isJust assigned' of | ||
58 | Just j -> Store assigned' j | ||
59 | Nothing -> Store assigned' 0 | ||
60 | else | ||
61 | Store assigned' last | ||
62 | |||
63 | |||
64 | findLastIndex :: (a -> Bool) -> Vector a -> Maybe Index | ||
65 | findLastIndex p v = findLastIndex' p v Nothing 0 | ||
66 | where | ||
67 | findLastIndex' p v current i = | ||
68 | if i >= V.length v then current | ||
69 | else if p $ v V.! i then let x = Just i in x `seq` findLastIndex' p v x (i+1) | ||
70 | else findLastIndex' p v current (i+1) | ||
71 | |||
72 | |||
73 | -- | Access the element in the given slot. | ||
74 | element :: Index -> Store a -> Maybe a | ||
75 | element index (Store assigned _) = assigned V.! index | ||
76 | |||
77 | |||
78 | -- test | ||
79 | test :: IO () | ||
80 | test = evalStateT test' emptyStore | ||
81 | |||
82 | |||
83 | test' :: StateT (Store Int) IO () | ||
84 | test' = do | ||
85 | x <- store' 1 | ||
86 | y <- store' 2 | ||
87 | z <- store' 3 | ||
88 | w <- store' 4 | ||
89 | free y | ||
90 | store' 5 | ||
91 | free w | ||
92 | store' 6 | ||
93 | a <- store' 7 | ||
94 | free a | ||
95 | store' 8 | ||
96 | return () | ||
97 | |||
98 | |||
99 | store' :: Int -> StateT (Store Int) IO Int | ||
100 | store' elem = do | ||
101 | s <- get | ||
102 | let (i, s') = store elem s | ||
103 | put s' | ||
104 | lift $ printf "%d stored at %d; %s\n" elem i (show s') | ||
105 | return i | ||
106 | |||
107 | |||
108 | free :: Index -> StateT (Store Int) IO () | ||
109 | free i = do | ||
110 | s <- get | ||
111 | let s' = storeFree i s | ||
112 | put s' | ||
113 | lift $ printf "Slot %d freed; %s\n" i (show s') | ||
114 | |||
diff --git a/Spear/IDStore.hs b/Spear/Sys/Store/ID.hs index 8879756..a4da3d0 100644 --- a/Spear/IDStore.hs +++ b/Spear/Sys/Store/ID.hs | |||
@@ -1,4 +1,4 @@ | |||
1 | module Spear.IDStore | 1 | module Spear.Sys.Store.ID |
2 | ( | 2 | ( |
3 | ID | 3 | ID |
4 | , IDStore | 4 | , IDStore |