From: Michael Orlitzky Date: Sat, 8 May 2010 16:45:46 +0000 (-0400) Subject: Implemented the combine_all function for Cidrs containing one another. X-Git-Tag: 0.0.1~68 X-Git-Url: http://gitweb.michael.orlitzky.com/?p=hath.git;a=commitdiff_plain;h=b22b30048819a3a53517e44370a97a2e37ba3f10 Implemented the combine_all function for Cidrs containing one another. Removed the 'contains' export from the Cidr module. Added a 'contains_proper' convenience function. --- diff --git a/src/Cidr.hs b/src/Cidr.hs index 12d9689..266b7c5 100644 --- a/src/Cidr.hs +++ b/src/Cidr.hs @@ -1,7 +1,7 @@ module Cidr ( Cidr(..), cidr_from_string, - contains + combine_all ) where import IPv4Address @@ -91,3 +91,21 @@ contains (Cidr addr1 mbits1) (Cidr addr2 mbits2) where addr1masked = apply_mask addr1 mbits1 addr2masked = apply_mask addr2 mbits1 + + +contains_proper :: Cidr -> Cidr -> Bool +contains_proper cidr1 cidr2 = + (cidr1 `contains` cidr2) && (not (cidr1 == cidr2)) + + +-- A CIDR range is redundant (with respect to the given list) if +-- another CIDR range in that list properly contains it. +redundant :: [Cidr] -> Cidr -> Bool +redundant cidrlist cidr = any ((flip contains_proper) cidr) cidrlist + + +-- We want to get rid of all the Cidrs that are properly contained +-- in some other Cidr. +combine_all :: [Cidr] -> [Cidr] +combine_all cidrs = + filter (not . (redundant cidrs)) cidrs