<commit_before>{-# LANGUAGE TypeFamilies #-}
module Graph.Graph (
    -- * Types
      Edge
    , EdgeId (..)
    , Node
    , NodeId (..)
    , Graph(..)

    -- * Construction
    , empty
    , build

    -- ** Insertion
    , insertEdge
    , insertNode
    , insertEdgeWithPayload
    , insertNodeWithPayload

    -- ** Delete/Update
    , removeEdge
    , removeNode
    , updateEdgePayload
    , updateNodePayload

    -- * Extraction
    , edges
    , nodes
    , edgesFromNode
    , edgesIntoNode
    , edgesWithPayload
    , incidentEdges
    , neighbourNodes
    , nodesConnectedTo
    , nodesFromNode
    , nodesIntoNode
    , nodesWithPayload

    -- * Query
    , Graph.Graph.null
    , nodePayload
    , edgePayload
    , sourceOf
    , targetOf
    -- ** Predicates
    , isEdgeOf
    , isNodeOf
    , isAdjacentTo
    , isIncidentTo
    
    --
    , newNodes
    , newEdges
) where

import Control.Applicative ((<$>))
import Abstract.Valid
import Data.List
import Data.List.Utils

data Node a = Node { getNodePayload :: Maybe a
              } deriving (Show, Read)

instance Eq (Node a) where
    n == n' = True -- Simplifies all Eq instances that depend upon Node

data Edge a = Edge { getSource   :: NodeId
                   , getTarget   :: NodeId
                   , getEdgePayload :: Maybe a
              } deriving (Show, Read)

instance Eq (Edge a) where
    e == e' = s == s' && t == t'
            where
              s  = getSource e
              t  = getTarget e
              s' = getSource e'
              t' = getTarget e'

data Graph a b = Graph {
    nodeMap :: [(NodeId, Node a)],
    edgeMap :: [(EdgeId, Edge b)]
    } deriving (Read)

-- | Verify equality of two Maps
-- isInfixOf checks if x is contained in y
eq :: (Eq t1, Eq t2) => [(t1, t2)] -> [(t1, t2)] -> Bool
eq a b = isInfixOf a b && isInfixOf b a

instance Eq (Graph a b) where
    (Graph nodeMap1 edgeMap1) == (Graph nodeMap2 edgeMap2) =
         eq nodeMap1 nodeMap2 &&
         eq edgeMap1 edgeMap2

instance Show (Graph a b) where
    show gr@(Graph nm em) =
              "Nodes:\n" ++
              concatMap (\(n, _) -> "\t" ++ show n ++ "\n") nm ++
              "Edges:\n" ++
              concatMap (\(eid, e) -> "\t" ++ show eid ++ "\n") em


newtype NodeId = NodeId Int deriving (Eq, Ord, Read)
newtype EdgeId = EdgeId Int deriving (Eq, Ord, Read)<commit_msg>fixed another bug in equality of graphs<commit_after>{-# LANGUAGE TypeFamilies #-}
module Graph.Graph {-(
    -- * Types
      Edge
    , EdgeId (..)
    , Node
    , NodeId (..)
    , Graph(..)

    -- * Construction
    , empty
    , build

    -- ** Insertion
    , insertEdge
    , insertNode
    , insertEdgeWithPayload
    , insertNodeWithPayload

    -- ** Delete/Update
    , removeEdge
    , removeNode
    , updateEdgePayload
    , updateNodePayload

    -- * Extraction
    , edges
    , nodes
    , edgesFromNode
    , edgesIntoNode
    , edgesWithPayload
    , incidentEdges
    , neighbourNodes
    , nodesConnectedTo
    , nodesFromNode
    , nodesIntoNode
    , nodesWithPayload

    -- * Query
    , Graph.Graph.null
    , nodePayload
    , edgePayload
    , sourceOf
    , targetOf
    -- ** Predicates
    , isEdgeOf
    , isNodeOf
    , isAdjacentTo
    , isIncidentTo
    
    --
    , newNodes
    , newEdges
) -}where

import Control.Applicative ((<$>))
import Abstract.Valid
import Data.List
import Data.List.Utils

data Node a = Node { getNodePayload :: Maybe a
              } deriving (Show, Read)

instance Eq (Node a) where
    n == n' = True -- Simplifies all Eq instances that depend upon Node

data Edge a = Edge { getSource   :: NodeId
                   , getTarget   :: NodeId
                   , getEdgePayload :: Maybe a
              } deriving (Show, Read)

instance Eq (Edge a) where
    e == e' = s == s' && t == t'
            where
              s  = getSource e
              t  = getTarget e
              s' = getSource e'
              t' = getTarget e'

data Graph a b = Graph {
    nodeMap :: [(NodeId, Node a)],
    edgeMap :: [(EdgeId, Edge b)]
    } deriving (Read)

-- | Verify equality of two Maps
eq :: (Eq t1, Eq t2) => [(t1, t2)] -> [(t1, t2)] -> Bool
eq a b = contained a b && contained b a

contain :: Eq t => [t] -> [t] -> Bool
contain a b = False `notElem` (map (\x -> x `elem` b) a)

instance Eq (Graph a b) where
    (Graph nodeMap1 edgeMap1) == (Graph nodeMap2 edgeMap2) =
         eq nodeMap1 nodeMap2 &&
         eq edgeMap1 edgeMap2

instance Show (Graph a b) where
    show gr@(Graph nm em) =
              "Nodes:\n" ++
              concatMap (\(n, _) -> "\t" ++ show n ++ "\n") nm ++
              "Edges:\n" ++
              concatMap (\(eid, e) -> "\t" ++ show eid ++ "\n") em


newtype NodeId = NodeId Int deriving (Eq, Ord, Read)
newtype EdgeId = EdgeId Int deriving (Eq, Ord, Read)
{'fraction_non_alphanumeric': 0.12083591971860129, 'fraction_numerical': 0.005793503000206911, 'mean_word_length': 1.8468786808009423, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1311508', 'n_tokens_mistral': 1589, 'n_tokens_neox': 1534, 'n_words': 474}