Changeset 63 for Deliverables
- Timestamp:
- Sep 13, 2010, 11:04:55 AM (10 years ago)
- Location:
- Deliverables/D4.1
- Files:
-
- 2 added
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
Deliverables/D4.1/Bit.ml
r62 r63 1 1 (*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*) 2 (* FILENAME: Bit.ml*)2 (* FILENAME: Bit.ml *) 3 3 (* DESCRIPTION: An ADT implementing bits, and common operations on them. *) 4 (* CREATED: 10/09/2010, Dominic Mulligan*)4 (* CREATED: 10/09/2010, Dominic Mulligan *) 5 5 (* BUGS: *) 6 6 (*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*) … … 12 12 val from_bool: bool -> bit 13 13 val from_int: int -> bit 14 val from_string: string -> bit option 15 16 val to_bool: bit -> bool 17 val to_int: bit -> int 18 val to_string: bit -> string 14 19 15 20 val (-&-): bit -> bit -> bit … … 18 23 val not: bit -> bit 19 24 20 val pretty: bit -> string21 25 end;; 22 26 … … 31 35 else 32 36 false 37 let from_string = 38 function 39 "0" -> Some false 40 | "1" -> Some true 41 | _ -> None 42 43 let to_int = 44 function 45 false -> 0 46 | true -> 1 47 let to_string l = 48 match l with 49 true -> "1" 50 | false -> "0" 51 52 let to_bool b = b 33 53 34 54 let (-&-) l r = … … 48 68 true -> false 49 69 | false -> true 50 51 let pretty l =52 match l with53 true -> "1b"54 | false -> "0b"55 70 end;; -
Deliverables/D4.1/Byte.ml
r62 r63 1 1 (*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*) 2 (* FILENAME: Byte.ml *) 3 (* DESCRIPTION: An ADT implementing bytes, and common operations on them. *) 4 (* CREATED: 10/09/2010, Dominic Mulligan *) 2 (* FILENAME: Byte.ml *) 3 (* DESCRIPTION: An ADT implementing standard 8 bit bytes, and common *) 4 (* operations on them. *) 5 (* CREATED: 10/09/2010, Dominic Mulligan *) 5 6 (* BUGS: *) 6 7 (*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*) … … 16 17 (* 17 18 val from_bits: bit -> bit -> bit -> bit -> bit -> bit -> bit -> bit -> byte 19 val from_bit: bit -> byte 18 20 val from_nibbles: nibble -> nibble -> byte 21 val from_nibble: nibble -> byte 19 22 val from_int: int -> byte 23 24 val to_bit: byte -> bit option 25 val to_nibble: byte -> nibble option 26 val to_int: byte -> int 20 27 21 28 val get_nibble_at: int -> byte -> nibble … … 45 52 type nibble = Nibble.nibble 46 53 type byte = nibble * nibble 54 55 let from_bits b1 b2 b3 b4 b5 b6 b7 b8 = 56 let nibble 47 57 end -
Deliverables/D4.1/Nibble.ml
r62 r63 1 1 (*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*) 2 (* FILENAME: Nibble.ml *) 3 (* DESCRIPTION: An ADT implementing nibbles, and common operations on them. *) 4 (* CREATED: 10/09/2010, Dominic Mulligan *) 2 (* FILENAME: Nibble.ml *) 3 (* DESCRIPTION: An ADT implementing 4 bit nibbles, and common operations on *) 4 (* them. *) 5 (* CREATED: 10/09/2010, Dominic Mulligan *) 5 6 (* BUGS: *) 6 7 (*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*) … … 13 14 type nibble 14 15 15 val from_bits: bit -> bit -> nibble 16 val from_bits: bit -> bit -> bit -> bit -> nibble 17 val from_bit: bit -> nibble 16 18 val from_int: int -> nibble option 19 val from_string: string -> nibble option 20 21 val to_bits: nibble -> (bit * bit * bit * bit) 22 val to_bit: nibble -> bit option 23 val to_int: nibble -> int 24 val to_string: nibble -> string 17 25 18 26 val get_bit_at: int -> nibble -> bit option … … 29 37 val iter_bit: (bit -> string) -> nibble -> string 30 38 val zip_bit: (bit -> bit -> bit) -> nibble -> nibble -> nibble 31 val pretty: nibble -> string32 39 end 33 40 … … 35 42 struct 36 43 type bit = Bit.bit 37 type nibble = bit * bit 44 type nibble = bit * bit * bit * bit 38 45 39 let from_bits l h = (l, h) 46 let from_bits b1 b2 b3 b4 = (b1, b2, b3, b4) 47 let from_bit h = (Bit.from_bool false, Bit.from_bool false, Bit.from_bool false, h) 40 48 let from_int int_val = 41 49 match int_val with 42 0 -> Some (Bit.from_int 0, Bit.from_int 0) 43 | 1 -> Some (Bit.from_int 0, Bit.from_int 1) 44 | 2 -> Some (Bit.from_int 1, Bit.from_int 0) 45 | 3 -> Some (Bit.from_int 1, Bit.from_int 1) 50 0 -> Some (Bit.from_int 0, Bit.from_int 0, Bit.from_int 0, Bit.from_int 0) 51 | 1 -> Some (Bit.from_int 0, Bit.from_int 0, Bit.from_int 0, Bit.from_int 1) 52 | 2 -> Some (Bit.from_int 0, Bit.from_int 0, Bit.from_int 1, Bit.from_int 0) 53 | 3 -> Some (Bit.from_int 0, Bit.from_int 0, Bit.from_int 1, Bit.from_int 1) 54 | 4 -> Some (Bit.from_int 0, Bit.from_int 1, Bit.from_int 0, Bit.from_int 0) 55 | 5 -> Some (Bit.from_int 0, Bit.from_int 1, Bit.from_int 0, Bit.from_int 1) 56 | 6 -> Some (Bit.from_int 0, Bit.from_int 1, Bit.from_int 1, Bit.from_int 0) 57 | 7 -> Some (Bit.from_int 0, Bit.from_int 1, Bit.from_int 1, Bit.from_int 1) 58 | 8 -> Some (Bit.from_int 1, Bit.from_int 0, Bit.from_int 0, Bit.from_int 0) 59 | 9 -> Some (Bit.from_int 1, Bit.from_int 0, Bit.from_int 0, Bit.from_int 1) 60 | 10 -> Some (Bit.from_int 1, Bit.from_int 0, Bit.from_int 1, Bit.from_int 0) 61 | 11 -> Some (Bit.from_int 1, Bit.from_int 0, Bit.from_int 1, Bit.from_int 1) 62 | 12 -> Some (Bit.from_int 1, Bit.from_int 1, Bit.from_int 0, Bit.from_int 0) 63 | 13 -> Some (Bit.from_int 1, Bit.from_int 1, Bit.from_int 0, Bit.from_int 1) 64 | 14 -> Some (Bit.from_int 1, Bit.from_int 1, Bit.from_int 1, Bit.from_int 0) 65 | 15 -> Some (Bit.from_int 1, Bit.from_int 1, Bit.from_int 1, Bit.from_int 1) 46 66 | _ -> None 67 let from_string 68 69 let to_bit (l, h) = 70 let (bl, bh) = Bit.to_bool l, Bit.to_bool h in 71 match (bl, bh) with 72 (false, b) -> Some (Bit.from_bool b) 73 | (true, b) -> None 74 let to_int (l, h) = (2 * Bit.to_int l) + Bit.to_int h 75 let to_string = iter_bit Bit.to_string 47 76 48 77 let get_bit_at index (l, h) = … … 79 108 str_l ^ str_h 80 109 let zip_bit f (l1, h1) (l2, h2) = (f l1 l2, f l2 h2) 81 let pretty = iter_bit Bit.pretty82 110 end 83 111
Note: See TracChangeset
for help on using the changeset viewer.