nested-sets 0.0.1.0 → 0.0.1.1
raw patch · 2 files changed
+30/−18 lines, 2 filesPVP: minor bump suggested
API additions: PVP suggests at least a minor version bump
API changes (from Hackage documentation)
+ Data.NestedSet: isNestedSetsPositionParent :: Position -> Position -> Bool
+ Data.NestedSet: nestedSetsPositionSetValue :: NestedSets a -> Position -> a -> NestedSets a
Files
- nested-sets.cabal +2/−1
- src/lib/Data/NestedSet.hs +28/−17
nested-sets.cabal view
@@ -1,5 +1,5 @@ name: nested-sets-version: 0.0.1.0+version: 0.0.1.1 cabal-version: >=1.10 build-type: Simple license: GPL-3@@ -12,6 +12,7 @@ Supported functionality includes: . * conversion from and to Forest structure from Data.Tree;+ . * position support with moving to parent, sibling and child nodes. homepage: stability: alpha
src/lib/Data/NestedSet.hs view
@@ -9,6 +9,8 @@ nestedSetsParentPosition, nestedSetsFirstChildPosition, nestedSetsPositionValue,+ nestedSetsPositionSetValue,+ isNestedSetsPositionParent, ) where import Data.Tree (Forest, Tree (..), rootLabel, subForest)@@ -50,7 +52,7 @@ nestedSetsNextSiblingPosition [] _ = Nothing nestedSetsNextSiblingPosition (first : ds) pos | position first == pos = firstPositionOf ds- | isPositionParent (position first) pos = nestedSetsNextSiblingPosition (children first) pos+ | isNestedSetsPositionParent (position first) pos = nestedSetsNextSiblingPosition (children first) pos | otherwise = nestedSetsNextSiblingPosition ds pos where firstPositionOf [] = Nothing firstPositionOf (firstSet : _) = Just . position $ firstSet@@ -59,34 +61,43 @@ nestedSetsParentPosition :: NestedSets a -> Position -> Maybe Position nestedSetsParentPosition [] _ = Nothing nestedSetsParentPosition (firstSet:ds) pos- | isPositionParent (position firstSet) pos = descendToChildren firstSet+ | isNestedSetsPositionParent (position firstSet) pos = descendToChildren firstSet | otherwise = nestedSetsParentPosition ds pos where findParentPos [] _ = Nothing findParentPos (x : xs) currentParent | position x == pos = Just . position $ currentParent- | isPositionParent (position x) pos = descendToChildren x+ | isNestedSetsPositionParent (position x) pos = descendToChildren x | otherwise = findParentPos xs currentParent descendToChildren set = findParentPos (children set) set -- | Advance the position to the first child node. nestedSetsFirstChildPosition :: NestedSets a -> Position -> Maybe Position-nestedSetsFirstChildPosition [] _ = Nothing-nestedSetsFirstChildPosition (first : ds) pos- | position first == pos = firstPosition . children $ first- | isPositionParent (position first) pos = nestedSetsFirstChildPosition (children first) pos- | otherwise = nestedSetsFirstChildPosition ds pos- where firstPosition [] = Nothing- firstPosition (x : _) = Just . position $ x+nestedSetsFirstChildPosition sets pos = firstPosition . (fmap children) $ findPosition sets pos+ where firstPosition Nothing = Nothing+ firstPosition (Just []) = Nothing+ firstPosition (Just (x : _)) = Just . position $ x -- | Retrieve the value for the given 'Position'. nestedSetsPositionValue :: NestedSets a -> Position -> Maybe a-nestedSetsPositionValue [] _ = Nothing-nestedSetsPositionValue (first : ds) pos- | position first == pos = Just . content $ first- | isPositionParent (position first) pos = nestedSetsPositionValue (children first) pos- | otherwise = nestedSetsPositionValue ds pos+nestedSetsPositionValue sets pos = fmap content $ findPosition sets pos +-- | Set value for the 'Position' on the given nested set.+-- Does not modify the given 'NestedSets' if the 'Position' cannot be found.+nestedSetsPositionSetValue :: NestedSets a -> Position -> a -> NestedSets a+nestedSetsPositionSetValue [] _ _ = []+nestedSetsPositionSetValue (first : ds) pos value+ | position first == pos = (first {content = value}) : ds+ | isNestedSetsPositionParent (position first) pos = (first {children = nestedSetsPositionSetValue (children first) pos value }) : ds+ | otherwise = first:(nestedSetsPositionSetValue ds pos value) -isPositionParent :: Position -> Position -> Bool-isPositionParent (parentL, parentR) (childL, childR) = parentL < childL && parentR > childR+isNestedSetsPositionParent :: Position -> Position -> Bool+isNestedSetsPositionParent (parentL, parentR) (childL, childR) = parentL < childL && parentR > childR+++findPosition :: NestedSets a -> Position -> Maybe (NestedSetsNode a)+findPosition [] _ = Nothing+findPosition (first : ds) pos+ | position first == pos = Just first+ | isNestedSetsPositionParent (position first) pos = findPosition (children first) pos+ | otherwise = findPosition ds pos