Skip to content

Commit

Permalink
stack-ize the repository.
Browse files Browse the repository at this point in the history
  • Loading branch information
nushio3 committed Jan 18, 2016
1 parent d811143 commit 0f9c65a
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 25 deletions.
45 changes: 23 additions & 22 deletions Numeric/Search/Integer.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
-----------------------------------------------------------------------------

module Numeric.Search.Integer (
-- * One-dimensional searches
search, searchFrom, searchTo,
-- * Two-dimensional searches
search2) where
-- * One-dimensional searches
search, searchFrom, searchTo,
-- * Two-dimensional searches
search2) where

import Data.Maybe (fromMaybe)

Expand Down Expand Up @@ -46,9 +46,9 @@ search p = fromMaybe (searchFrom p 1) (searchTo p 0)
searchFrom :: (Integer -> Bool) -> Integer -> Integer
searchFrom p = search_from 1
where search_from step l
| p l' = searchIntegerRange p l (l'-1)
| otherwise = search_from (2*step) (l'+1)
where l' = l + step
| p l' = searchIntegerRange p l (l'-1)
| otherwise = search_from (2*step) (l'+1)
where l' = l + step

-- | /O(log(h-n))/.
-- Search the integers up to a given upper bound.
Expand All @@ -59,10 +59,10 @@ searchTo :: (Integer -> Bool) -> Integer -> Maybe Integer
searchTo p h0
| p h0 = Just (search_to 1 h0)
| otherwise = Nothing
where search_to step h -- @step >= 1 && p h@
| p h' = search_to (2*step) h'
| otherwise = searchSafeRange p (h'+1) h
where h' = h - step
where search_to step h -- @step >= 1 && p h@
| p h' = search_to (2*step) h'
| otherwise = searchSafeRange p (h'+1) h
where h' = h - step

-- | /O(m log(n\/m))/.
-- Two-dimensional search, using an algorithm due described in
Expand All @@ -84,23 +84,24 @@ searchTo p h0
--
search2 :: (Integer -> Integer -> Bool) -> [(Integer,Integer)]
search2 p = search2Rect p 0 0 hx hy []
where hx = searchFrom (\ x -> p x 0) 0
hy = searchFrom (\ y -> p 0 y) 0
where
hx = searchFrom (\ x -> p x 0) 0
hy = searchFrom (\ y -> p 0 y) 0

search2Rect :: (Integer -> Integer -> Bool) ->
Integer -> Integer -> Integer -> Integer ->
[(Integer,Integer)] -> [(Integer,Integer)]
Integer -> Integer -> Integer -> Integer ->
[(Integer,Integer)] -> [(Integer,Integer)]
search2Rect p lx ly hx hy
| lx > hx || ly > hy = id
| lx == hx && ly == hy = if p lx ly then ((lx, ly) :) else id
| hx-lx > hy-ly =
let mx = (lx+hx) `div` 2
my = searchIntegerRange (\ y -> p mx y) ly hy
in search2Rect p lx my mx hy . search2Rect p (mx+1) ly hx (my-1)
let mx = (lx+hx) `div` 2
my = searchIntegerRange (\ y -> p mx y) ly hy
in search2Rect p lx my mx hy . search2Rect p (mx+1) ly hx (my-1)
| otherwise =
let mx = searchIntegerRange (\ x -> p x my) lx hx
my = (ly+hy) `div` 2
in search2Rect p lx (my+1) (mx-1) hy . search2Rect p mx ly hx my
let mx = searchIntegerRange (\ x -> p x my) lx hx
my = (ly+hy) `div` 2
in search2Rect p lx (my+1) (mx-1) hy . search2Rect p mx ly hx my

-- | Search a bounded interval of integers.
--
Expand All @@ -122,4 +123,4 @@ searchSafeRange p l h
| l == h = l
| p m = searchSafeRange p l m
| otherwise = searchSafeRange p (m+1) h
where m = (l + h) `div` 2 -- If l < h, then l <= m < h
where m = (l + h) `div` 2 -- If l < h, then l <= m < h
5 changes: 2 additions & 3 deletions binary-search.cabal
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
Name: binary-search
Version: 0.1
Build-Depends: base
Build-Type: Simple
License: BSD3
license-file: LICENSE
Author: Ross Paterson <ross@soi.city.ac.uk>, Takayuki Muranushi <muranushi@gmail.com>
Maintainer: Takayuki Muranushi <muranushi@gmail.com>
Maintainer: Takayuki Muranushi <muranushi@gmail.com>
Category: Algorithms
Synopsis: Binary and exponential searches
Description: These modules address the problem of finding the boundary
Expand Down Expand Up @@ -44,7 +43,7 @@ Test-Suite spec
Ghc-Options: -Wall
Main-Is: Spec.hs
Other-Modules: PureSpec

Build-Depends: base >=4.5 && < 5
, binary-search

Expand Down
32 changes: 32 additions & 0 deletions stack.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# For more information, see: https://github.com/commercialhaskell/stack/blob/release/doc/yaml_configuration.md

# Specifies the GHC version and set of packages available (e.g., lts-3.5, nightly-2015-09-21, ghc-7.10.2)
resolver: lts-4.0

# Local packages, usually specified by relative directory name
packages:
- '.'

# Packages to be pulled from upstream that are not in the resolver (e.g., acme-missiles-0.3)
extra-deps: []

# Override default flag values for local packages and extra-deps
flags: {}

# Extra package databases containing global packages
extra-package-dbs: []

# Control whether we use the GHC we find on the path
# system-ghc: true

# Require a specific version of stack, using version ranges
# require-stack-version: -any # Default
# require-stack-version: >= 0.1.4.0

# Override the architecture used by stack, especially useful on Windows
# arch: i386
# arch: x86_64

# Extra directories used by stack for building
# extra-include-dirs: [/path/to/dir]
# extra-lib-dirs: [/path/to/dir]

0 comments on commit 0f9c65a

Please sign in to comment.