1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
|
/**************************************************************************/
/* */
/* The Why platform for program certification */
/* Copyright (C) 2002-2008 */
/* Romain BARDOU */
/* Jean-François COUCHOT */
/* Mehdi DOGGUY */
/* Jean-Christophe FILLIÂTRE */
/* Thierry HUBERT */
/* Claude MARCHÉ */
/* Yannick MOY */
/* Christine PAULIN */
/* Yann RÉGIS-GIANAS */
/* Nicolas ROUSSET */
/* Xavier URBAIN */
/* */
/* This software is free software; you can redistribute it and/or */
/* modify it under the terms of the GNU General Public */
/* License version 2, as published by the Free Software Foundation. */
/* */
/* This software is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. */
/* */
/* See the GNU General Public License version 2 for more details */
/* (enclosed in the file GPL). */
/* */
/**************************************************************************/
//@ lemma mean1 : (\forall integer x y ; x <= y ==> x <= (x + y) / 2) ;
//@ lemma mean2 : (\forall integer x y ; x < y ==> (x + y) / 2 < y) ;
/*@ predicate mem(IntSet s,int n) {
@ \exists integer i; 0 <= i < s.taille && s.t[i] == n
@ }
@*/
public class IntSet {
int taille = 0;
int t[] = new int[10];
/*@ invariant sorted:
@ t != null && 0 <= taille <= t.length &&
@ (\forall integer i j; 0 <= i <= j < taille ==> t[i] <= t[j]);
@*/
/*@ assigns \nothing;
@ ensures
@ 0 <= \result <= taille &&
@ (\forall integer i; 0 <= i < \result ==> t[i] < n) &&
@ (\forall integer i; \result <= i < taille ==> t[i] >= n) ;
@*/
public int index(int n) {
int a = 0, b = taille, m;
/*@ loop_invariant
@ 0 <= a <= b <= taille &&
@ (\forall integer i; 0 <= i < a ==> t[i] < n) &&
@ (\forall integer i; b <= i < taille ==> t[i] >= n) ;
@ decreases b-a;
@*/
while (a<b) {
m = (a+b)/2;
if (t[m] < n) a=m+1; else b=m;
}
return a;
}
/*@ assigns \nothing;
@ ensures \result == true <==>
@ (\exists integer i; 0 <= i < taille && t[i] == n);
@*/
public boolean mem(int n) {
int i = index(n);
return (i < taille && t[i] == n);
}
/*@ assigns t,taille; // ,t[..]; syntax error ??
@ ensures (\forall int k; mem(this,k) <==> \old(mem(this,k)) || k==n);
@*/
public void add(int n) {
int i = index(n);
if (i < taille && t[i] == n) return;
/* ensures taille < t.length ....
*/
if (taille < t.length) {
copy(t,t,i,taille-1,i+1);
}
else {
int old[] = t;
t = new int[2*taille+1];
copy(old,t,0,i-1,0);
copy(old,t,i,taille-1,i+1);
}
t[i] = n;
taille++;
}
/* A version of add with intermediate annotation for automatic
* proof with simplify
*/
/*@ assigns t,taille; // ,t[..]; syntax error ??
@ ensures (\forall int k; mem(this,k) <==> \old(mem(this,k)) || k==n);
@*/
public void add2(int n) {
int i = index(n);
if (i < taille && t[i] == n) return;
/* @ assigns t,t[..];
@ ensures (\forall int j; 0 <= j && j < i; t[j]==\old(t[j]))
@ && (\forall int j; i+1 <= j && j < taille + 1; t[j]==\old(t[j-1]))
&& (\forall int j; i <= j && j < taille; \old(t[j])==t[j+1])
&& t !=null && t[i]==n && taille < t.length && t instanceof int[];
*/
{if (taille < t.length) {
copy(t,t,i,taille-1,i+1);
}
else {
int old[] = t;
t = new int[2*taille+1];
copy(old,t,0,i-1,0);
copy(old,t,i,taille-1,i+1);
}
t[i] = n;};
taille++;
}
/*
copy(src,dest,a,b,c) copies src[a..b] to dest[c..c+b-a]
if src and dest are the same, c is assumed greater than or equal to a.
*/
/*@ requires src != null && dest != null &&
@ 0 <= a && a-1 <= b && b < src.length && 0 <= c &&
@ c+b-a < dest.length && (dest == src ==> c >= a);
@ assigns dest[c..c+b-a];
@ ensures (\forall integer i; c <= i <= c+b-a ==> dest[i] == \old(src[i+a-c]));
@*/
public static void copy(int src[], int dest[],int a, int b, int c) {
/*@ loop_invariant
@ a-1 <= j <= b &&
@ (\forall integer i; j < i <= b ==> dest[i+c-a] == \at(src[i],Pre)) &&
@ (\forall integer i; c <= i <= j+c-a ==> dest[i] == \at(dest[i],Pre));
@ decreases j;
@*/
for (int j = b; j >= a; j--) {
dest[j+c-a] = src[j];
}
}
}
|