~ubuntu-branches/ubuntu/lucid/camomile/lucid

« back to all changes in this revision

Viewing changes to internal/bytesvect.ml

  • Committer: Bazaar Package Importer
  • Author(s): Sylvain Le Gall
  • Date: 2005-12-03 01:18:55 UTC
  • Revision ID: james.westby@ubuntu.com-20051203011855-qzvwlld1xyqnl62t
Tags: upstream-0.6.3
ImportĀ upstreamĀ versionĀ 0.6.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
(* $Id: bytesvect.ml,v 1.5 2003/04/27 17:47:59 yori Exp $ *)
 
2
(* Copyright 2002, 2003 Yamagata Yoriyuki *)
 
3
 
 
4
type t = 
 
5
    {len : int; 
 
6
     mutable bytes : int; 
 
7
     mutable contents : string;
 
8
     mutable id : int}
 
9
 
 
10
(* get b v i : read b-bytes from the k-th byte of v *)
 
11
(* b <= 4 *) 
 
12
let rec get_raw acc b v k =
 
13
  if b = 0 then acc else
 
14
  let acc' = (acc lsl 8) lor (Char.code v.[k]) in
 
15
  get_raw acc' (pred b) v (succ k)
 
16
 
 
17
let get v i = get_raw 0 v.bytes v.contents (i * v.bytes)
 
18
 
 
19
let rec unsafe_get_raw acc b v k =
 
20
  if b = 0 then acc else
 
21
  let acc' = (acc lsl 8) lor (Char.code (String.unsafe_get v k)) in
 
22
  unsafe_get_raw acc' (pred b) v (succ k)
 
23
 
 
24
let unsafe_get v i = unsafe_get_raw 0 v.bytes v.contents (i * v.bytes)
 
25
 
 
26
let rec set_bytes_raw b v k n =
 
27
  let c = (n lsr ((b - 1) lsl 3)) land 255 in
 
28
  v.[k] <- Char.chr c;
 
29
  if b > 1 then set_bytes_raw (b - 1) v (k + 1) n
 
30
 
 
31
let rec bytes n = if n = 0 then 0 else 1 + bytes (n lsr 8)
 
32
 
 
33
let set v i n =
 
34
  let b = bytes n in
 
35
  if v.bytes < b then
 
36
    let save = {len = v.len; bytes = v.bytes; contents = v.contents; id = 0} in
 
37
    let len = (String.length v.contents) / v.bytes in
 
38
    v.contents <- String.make (len * b) (Char.chr 0);
 
39
    v.bytes <- b;
 
40
    for i = 0 to len - 1 do 
 
41
      set_bytes_raw b v.contents (b * i) (get save i)
 
42
    done
 
43
  else ();
 
44
  set_bytes_raw v.bytes v.contents (i * v.bytes) n
 
45
 
 
46
let make i0 df =
 
47
  let b = max (bytes df) 1 in
 
48
  let v = 
 
49
    {len = i0; 
 
50
     bytes = b; 
 
51
     contents = String.make (b * i0) (Char.chr 0); 
 
52
     id = 0} 
 
53
  in
 
54
  for i = 0 to i0 - 1 do set v i df done; v
 
55
 
 
56
let copy v =
 
57
  {len = v.len;
 
58
   bytes = v.bytes;
 
59
   contents = String.copy v.contents;
 
60
   id = 0}
 
61
 
 
62
let iteri proc v =
 
63
  let len = (String.length v.contents) / v.bytes in
 
64
  for i = 0 to len - 1 do
 
65
    proc i (get v i)
 
66
  done
 
67
 
 
68
let length v = v.len
 
69
 
 
70
let set_id v id = v.id <- id
 
71
let id v = v.id