tp3.ml 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. type 'a file_array = {contenu: 'a array; mutable debut : int; mutable fin: int; mutable vide: bool};;
  2. let is_empty1 file =
  3. file.vide
  4. ;;
  5. let is_plein1 file =
  6. not(file.vide) && (file.debut = file.fin)
  7. ;;
  8. let add_tail1 x file =
  9. if is_plein1 file
  10. then failwith "file pleine"
  11. else
  12. begin
  13. file.contenu.(file.fin)<-x;
  14. file.fin <- ((file.fin +1) mod (Array.length (file.contenu)));
  15. file.vide <- false;
  16. end;
  17. ;;
  18. let pop_head1 file =
  19. if is_empty1 file
  20. then failwith "file pleine"
  21. else
  22. begin
  23. file.debut <- (file.debut + 1) mod (Array.length (file.contenu));
  24. file.vide <- (file.debut=file.fin);
  25. file
  26. end;
  27. ;;
  28. let length1 file =
  29. if is_empty1 file
  30. then 0
  31. else
  32. let result = (file.fin - file.debut) in
  33. if result>0 then result
  34. else result + Array.length file.contenu
  35. ;;
  36. let appartenance1 x file =
  37. if is_empty1 file
  38. then false
  39. else
  40. begin
  41. let t = (length1 file) and n = Array.length file.contenu in
  42. let b = ref false and i = ref 0 in
  43. while !i<t || !b do
  44. if file.contenu.((file.debut + !i) mod n)=x then b:= true;
  45. i:=!i+1
  46. done;
  47. !b
  48. end
  49. ;;
  50. type 'a file_list = {mutable sortie: 'a list; mutable entree: 'a list}
  51. let is_empty2 file = match (file.sortie,file.entree) with
  52. | ([],[]) -> true
  53. | _-> false
  54. ;;
  55. let add_tail2 x file =
  56. file.entree <- x::file.entree;
  57. ;;
  58. let rec aux file = match file.entree with
  59. | [] -> ()
  60. |t::q -> file.sortie <- t::file.sortie;
  61. file.entree <- q;
  62. aux file
  63. ;;
  64. let rec normalize file =
  65. if file.sortie = [] then aux file
  66. ;;
  67. let pop_head2 file =
  68. normalize file;
  69. if file.sortie = [] then failwith "liste vide"
  70. else
  71. begin
  72. let t::q = file.sortie in
  73. file.sortie <- q;
  74. t
  75. end
  76. ;;