File size: 7,365 Bytes
bff4ec7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*@ Import Lean
import Algorithms.discretize.lean.helper_lib
open scoped SimpleC
*/

/*@ Extern Coq (permutation : list Z -> list Z -> Prop) */
/*@ Extern Coq (increasing : list Z -> Prop) */
/*@ Extern Coq (same_outside_range : list Z -> list Z -> Z -> Z -> Prop) */
/*@ Extern Coq (partitioned_at : list Z -> Z -> Z -> Z -> Prop) */
/*@ Extern Coq (sorted_range : list Z -> Z -> Z -> Prop) */
/*@ Extern Coq (partition_scan_inv : list Z -> list Z -> Z -> Z -> Z -> Z -> Z -> Prop) */
/*@ Extern Coq (strict_increasing : list Z -> Prop) */
/*@ Extern Coq (dedup_scan_inv : list Z -> list Z -> list Z -> Z -> Z -> Prop) */
/*@ Extern Coq (discretize_result : list Z -> Z -> list Z -> Z -> Prop) */
/*@ Extern Coq (query_forward_result : list Z -> Z -> Z -> Z -> Prop) */
/*@ Extern Coq (query_forward_search_inv : list Z -> Z -> Z -> Z -> Z -> Prop) */

void swap(int *arr, int i, int j)
/*@ With n l
    Require 0 <= i && i < n && 0 <= j && j < n &&
            IntArray::full(arr, n, l)
    Ensure IntArray::full(arr, n,
             replace_Znth(j, Znth(i, l, 0), replace_Znth(i, Znth(j, l, 0), l)))
*/
{
    int tmp = arr[i];
    arr[i] = arr[j];
    arr[j] = tmp;
}

int partition(int *arr, int n, int low, int high)
/*@ With l
    Require 0 <= low && low <= high && high < n &&
            IntArray::full(arr, n, l)
    Ensure low <= __return && __return <= high &&
            exists l1,
              permutation(l, l1) &&
              same_outside_range(l, l1, low, high) &&
              partitioned_at(l1, low, high, __return) &&
              IntArray::full(arr, n, l1)
*/
{
    int pivot = arr[high];
    int i = low - 1;
    /*@ Inv Assert
        exists l1,
          arr == arr@pre && n == n@pre &&
          low == low@pre && high == high@pre &&
          pivot == Znth(high, l, 0) &&
          0 <= low && low <= high && high < n &&
          low - 1 <= i && i < j && j <= high &&
          partition_scan_inv(l, l1, low, high, pivot, i, j) &&
          IntArray::full(arr, n, l1)
    */
    for (int j = low; j < high; j++) {
        if (arr[j] <= pivot) {
            i++;
            swap(arr, i, j);
        }
    }
    swap(arr, i + 1, high);
    return i + 1;
}

void quicksort_range(int *arr, int n, int left, int right)
/*@ With l
    Require 0 <= n && 0 <= left && -1 <= right && right < n &&
            IntArray::full(arr, n, l)
    Ensure exists l1,
            permutation(l, l1) &&
            same_outside_range(l, l1, left, right) &&
            sorted_range(l1, left, right) &&
            IntArray::full(arr, n, l1)
*/
{
    if (left < right) {
        int p = partition(arr, n, left, right);
        if (p > left) {
            quicksort_range(arr, n, left, p - 1);
        }
        if (p < right) {
            quicksort_range(arr, n, p + 1, right);
        }
    }
}

void int_array_quicksort(int *arr, int n)
/*@ With l
    Require 1 <= n && n <= 50000 &&
            IntArray::full(arr, n, l)
    Ensure exists l1,
            permutation(l, l1) &&
            increasing(l1) &&
            IntArray::full(arr, n, l1)
*/
{
    quicksort_range(arr, n, 0, n - 1);
}

int discretize(const int *src, int n, int *dest_map)
/*@ With src_l
    Require Zlength(src_l) == n &&
            1 <= n && n <= 50000 &&
            IntArray::full(src, n, src_l) *
            IntArray::undef_full(dest_map, n)
    Ensure exists out_l,
            discretize_result(src_l, n, out_l, __return) &&
            IntArray::full(src, n, src_l) *
            IntArray::full(dest_map, n, out_l)
*/
{
    /*@ Inv Assert
        Zlength(src_l) == n@pre &&
        1 <= n@pre && n@pre <= 50000 &&
        0 <= i && i <= n@pre &&
        src == src@pre && dest_map == dest_map@pre && n == n@pre &&
        IntArray::full(src@pre, n@pre, src_l) *
        IntArray::full(dest_map@pre, i, sublist(0, i, src_l)) *
        IntArray::undef_seg(dest_map@pre, i, n@pre)
    */
    for (int i = 0; i < n; i++) {
        dest_map[i] = src[i];
    }
    /*@ Assert
        Zlength(src_l) == n@pre &&
        1 <= n@pre && n@pre <= 50000 &&
        src == src@pre && dest_map == dest_map@pre && n == n@pre &&
        IntArray::full(src@pre, n@pre, src_l) *
        IntArray::full(dest_map@pre, n@pre, src_l)
    */
    int_array_quicksort(dest_map, n);
    int slow = 0;
    /*@ Inv Assert
        exists sorted_l cur_l,
          Zlength(src_l) == n@pre &&
          1 <= n@pre && n@pre <= 50000 &&
          src == src@pre && dest_map == dest_map@pre && n == n@pre &&
          0 <= slow && slow < fast && 1 <= fast && fast <= n@pre &&
          dedup_scan_inv(src_l, sorted_l, cur_l, slow, fast) &&
          IntArray::full(src@pre, n@pre, src_l) *
          IntArray::full(dest_map@pre, n@pre, cur_l)
    */
    for (int fast = 1; fast < n; fast++) {
        if (dest_map[fast] != dest_map[slow]) {
            slow++;
            dest_map[slow] = dest_map[fast];
        }
    }
    /*@ Assert
        exists out_l,
          Zlength(src_l) == n@pre &&
          1 <= n@pre && n@pre <= 50000 &&
          src == src@pre && dest_map == dest_map@pre && n == n@pre &&
          discretize_result(src_l, n@pre, out_l, slow + 1) &&
          IntArray::full(src@pre, n@pre, src_l) *
          IntArray::full(dest_map@pre, n@pre, out_l)
    */
    return slow + 1;
}

int query_forward(const int *map, int map_size, int target)
/*@ With map_l
    Require Zlength(map_l) == map_size &&
            0 <= map_size && map_size <= 50000 &&
            strict_increasing(map_l) &&
            IntArray::full(map, map_size, map_l)
    Ensure query_forward_result(map_l, map_size, target, __return) &&
           IntArray::full(map, map_size, map_l)
*/
{
    int low = 0;
    int high = map_size - 1;

    /*@ Inv Assert
        Zlength(map_l) == map_size@pre &&
        0 <= map_size@pre && map_size@pre <= 50000 &&
        strict_increasing(map_l) &&
        map == map@pre && map_size == map_size@pre && target == target@pre &&
        0 <= low && low <= high + 1 && high < map_size@pre &&
        query_forward_search_inv(map_l, map_size@pre, target@pre, low, high) &&
        IntArray::full(map@pre, map_size@pre, map_l)
    */
    while (low <= high) {
        int mid = low + (high - low) / 2;
        /*@ Assert
            Zlength(map_l) == map_size@pre &&
            0 <= map_size@pre && map_size@pre <= 50000 &&
            strict_increasing(map_l) &&
            map == map@pre && map_size == map_size@pre && target == target@pre &&
            0 <= low && low <= mid && mid <= high && high < map_size@pre &&
            query_forward_search_inv(map_l, map_size@pre, target@pre, low, high) &&
            IntArray::full(map@pre, map_size@pre, map_l)
        */
        if (map[mid] == target) {
            return mid;
        } else if (map[mid] < target) {
            low = mid + 1;
        } else {
            high = mid - 1;
        }
    }
    /*@ Assert
        Zlength(map_l) == map_size@pre &&
        0 <= map_size@pre && map_size@pre <= 50000 &&
        strict_increasing(map_l) &&
        map == map@pre && map_size == map_size@pre && target == target@pre &&
        0 <= low && low <= high + 1 && high < map_size@pre &&
        query_forward_search_inv(map_l, map_size@pre, target@pre, low, high) &&
        query_forward_result(map_l, map_size@pre, target@pre, -1) &&
        IntArray::full(map@pre, map_size@pre, map_l)
    */
    return -1;
}