~ubuntu-branches/ubuntu/precise/ocaml-batteries/precise

« back to all changes in this revision

Viewing changes to src/core/extlib/extInt.ml

  • Committer: Bazaar Package Importer
  • Author(s): Stefano Zacchiroli
  • Date: 2010-03-06 16:03:38 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20100306160338-spvwiv3uc4jr28hw
Tags: 1.1.0-1
* New upstream release
  - major changes, "diet" version of the library
  - fix old FTBFS error, due to major code changes (Closes: #569455)
* Revamp packaging
  - adapt to new stuff shipped by upstream
  - switch from CDBS to dh
  - adapt dependencies (generally: reduce them)
* debian/patches/
  - remove old debian/patches/{debian-specific-installation-paths,
    debian-specific-info-on-doc-availability} as obsolete
  - new patch 0001-install-fix-for-bytecode-only-build: avoid
    installing *.a files with bytecode only compilation
* debian/libbatteries-ocaml-dev.links: remove file, shortend
  /usr/bin/ocaml-batteries to the top-level no longer exists
* remove debian/README.Debian (previous content is now obsolete)
* bump Standards-Version to 3.8.4 (no changes needed)
* debian/watch: update to match new upstream version convention
* debian/libbatteries-ocaml-{dev,doc}.{docs,examples}: ship only doc
  file from the root dir, other stuff is currently out of date
  (Closes: #514265)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
(* 
2
 
 * ExtInt - Extended integers
3
 
 * Copyright (C) 2007 Bluestorm <bluestorm dot dylc on-the-server gmail dot com>
4
 
 *               2008 David Teller
5
 
 * 
6
 
 * This library is free software; you can redistribute it and/or
7
 
 * modify it under the terms of the GNU Lesser General Public
8
 
 * License as published by the Free Software Foundation; either
9
 
 * version 2.1 of the License, or (at your option) any later version,
10
 
 * with the special exception on linking described in file LICENSE.
11
 
 *
12
 
 * This library is distributed in the hope that it will be useful,
13
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 
 * Lesser General Public License for more details.
16
 
 *
17
 
 * You should have received a copy of the GNU Lesser General Public
18
 
 * License along with this library; if not, write to the Free Software
19
 
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20
 
 *)
21
 
 
22
 
open Sexplib
23
 
TYPE_CONV_PATH "Batteries" (*For Sexplib, Bin-prot...*)
24
 
 
25
 
open Number
26
 
 
27
 
let enum () =
28
 
  let current_value   = ref min_int in
29
 
  let already_through = ref false   in
30
 
  let f  () =
31
 
    if  !current_value = max_int then
32
 
      if !already_through then raise Enum.No_more_elements
33
 
      else ( already_through := true; max_int )
34
 
    else Ref.post_incr current_value
35
 
  in Enum.from f
36
 
 
37
 
module BaseInt = struct
38
 
  
39
 
  type t = int with sexp
40
 
  
41
 
  let zero, one = 0, 1
42
 
 
43
 
  external neg : int -> int        = "%negint"
44
 
  external add : int -> int -> int = "%addint"
45
 
  external sub : int -> int -> int = "%subint"
46
 
  external mul : int -> int -> int = "%mulint"
47
 
  external div : int -> int -> int = "%divint"
48
 
 
49
 
  external ( + ) : int -> int -> int = "%addint"
50
 
  external ( - ) : int -> int -> int = "%subint"
51
 
  external ( * ) : int -> int -> int = "%mulint"
52
 
  external ( / ) : int -> int -> int = "%divint"
53
 
 
54
 
  external pred: int -> int        = "%predint"
55
 
  external succ: int -> int        = "%succint"
56
 
  let abs = abs
57
 
 
58
 
  external modulo : int -> int -> int = "%modint"
59
 
  let pow = generic_pow ~zero ~one ~div_two:(fun n -> n / 2) ~mod_two:(fun n -> n mod 2) ~mul
60
 
 
61
 
  let min_num, max_num = min_int, max_int
62
 
  let compare = ( - )
63
 
 
64
 
  external of_int : int -> int = "%identity"
65
 
  external to_int : int -> int = "%identity"
66
 
 
67
 
 
68
 
  let of_string x = 
69
 
    try int_of_string x
70
 
    with Failure "int_of_string" -> raise (Invalid_argument "int_of_string")
71
 
  let to_string = string_of_int
72
 
 
73
 
  let enum = enum
74
 
 
75
 
  let minus_one = ( - 1)
76
 
 
77
 
  external to_float : int -> float = "%floatofint"
78
 
  external of_float : float -> int = "%intoffloat"
79
 
      
80
 
  external of_string : string -> int = "caml_int_of_string"
81
 
 
82
 
  external rem : int -> int -> int = "%modint"
83
 
 
84
 
  let ( <> ) a b = a <> b
85
 
  let ( <= ) a b = a <= b
86
 
  let ( >= ) a b = a >= b
87
 
  let ( < )  a b = a < b
88
 
  let ( > )  a b = a > b
89
 
  let ( = )  a b = a = b
90
 
 
91
 
  let ( ** ) a b = pow a b
92
 
 
93
 
  let print out t = InnerIO.nwrite out (string_of_int t)
94
 
  let t_printer paren out t = print out t
95
 
 
96
 
  let ( -- )  x y = Enum.seq x (add one) ((>=) y)
97
 
  let ( --- ) x y = 
98
 
    if x <= y then x -- y 
99
 
    else Enum.seq x pred ((<=) y) 
100
 
 
101
 
end
102
 
 
103
 
module Int = struct
104
 
  include BaseInt
105
 
  let operations = let module N = Number.MakeNumeric(BaseInt) in N.operations
106
 
 
107
 
 
108
 
 
109
 
end
110
 
 
111
 
module BaseSafeInt = struct
112
 
  include BaseInt
113
 
 
114
 
  (**
115
 
     Open this module and [SafeInt] to replace traditional integer operators with
116
 
     their safe counterparts
117
 
  *)
118
 
      
119
 
 
120
 
  let ( * ) = mul
121
 
 
122
 
  let add a b =
123
 
    let c = Pervasives.( + ) a b in
124
 
      if a < 0 && b < 0 && c >= 0 || a > 0 && b > 0 && c <= 0   then raise Overflow
125
 
      else c
126
 
  let ( + ) = add
127
 
 
128
 
  let sub a b =
129
 
    let c = Pervasives.( - ) a b in
130
 
      if a < 0 && b > 0 && c >= 0 || a > 0 && b < 0 && c <= 0   then raise Overflow
131
 
      else c
132
 
  let ( - ) a b = sub a b
133
 
 
134
 
  let neg x = 
135
 
    if x <> min_int then ~- x
136
 
    else raise Overflow
137
 
 
138
 
  let succ x =
139
 
    if x <> max_int then succ x
140
 
    else raise Overflow
141
 
 
142
 
  let pred x =
143
 
    if x <> min_int then pred x
144
 
    else raise Overflow
145
 
    
146
 
  let abs x =
147
 
    if x <> min_int then abs x
148
 
    else raise Overflow
149
 
 
150
 
  (*Here, we assume that, in case of overflow the result will be different when computing
151
 
    in 31 bits (resp 63 bits) and in 32 bits (resp 64 bits). Need to check that trick.*)
152
 
  let mul a b =
153
 
    if b = 0 then 0
154
 
    else if (abs a) > max_int / (abs b) then raise Overflow else a * b
155
 
 
156
 
  let pow = Number.generic_pow ~zero ~one ~div_two:(fun n -> n/2) ~mod_two:(fun n -> n mod 20) ~mul
157
 
    
158
 
end
159
 
 
160
 
module Safe_int = struct
161
 
  include BaseSafeInt
162
 
  let operations = let module N = Number.MakeNumeric(BaseSafeInt) in N.operations
163
 
end
164
 
 
165
 
(*
166
 
module Int     = struct
167
 
  include BaseInt
168
 
  module Numeric = struct include Numeric(BaseInt) end
169
 
end
170
 
 
171
 
module SafeInt = struct
172
 
  include BaseSafeInt
173
 
  module Numeric = struct include Numeric(BaseSafeInt) end
174
 
end
175
 
*)