File: linearsearch.c

package info (click to toggle)
boolector 3.2.4-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 20,744 kB
  • sloc: ansic: 83,136; cpp: 18,159; sh: 3,668; python: 2,889; makefile: 210
file content (80 lines) | stat: -rw-r--r-- 2,614 bytes parent folder | download
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
#include <stdio.h>
#include <stdlib.h>
#include "boolector.h"
#include "utils/btorutil.h"

int
main (int argc, char **argv)
{
  int num_bits, num_bits_index, num_elements, i;
  Btor *btor;
  BoolectorNode **indices, *array, *eq;
  BoolectorNode *temp, *read, *formula, *val, *index, *found;
  BoolectorSort isort, esort, asort;
  if (argc != 3)
  {
    printf ("Usage: ./linearsearch <num-bits> <num-elements>\n");
    return 1;
  }
  num_bits = atoi (argv[1]);
  if (num_bits <= 0)
  {
    printf ("Number of bits must be greater than zero\n");
    return 1;
  }
  num_elements = atoi (argv[2]);
  if (num_elements <= 1)
  {
    printf ("Number of elements must be greater than one\n");
    return 1;
  }
  if (!btor_util_is_power_of_2 (num_elements))
  {
    printf ("Number of elements must be a power of two\n");
    return 1;
  }
  num_bits_index = btor_util_log_2 (num_elements);
  btor           = boolector_new ();
  boolector_set_opt (btor, BTOR_OPT_REWRITE_LEVEL, 0);
  indices = (BoolectorNode **) malloc (sizeof (BoolectorNode *) * num_elements);
  isort   = boolector_bitvec_sort (btor, num_bits_index);
  esort   = boolector_bitvec_sort (btor, num_bits);
  asort   = boolector_array_sort (btor, isort, esort);
  for (i = 0; i < num_elements; i++)
    indices[i] = boolector_int (btor, i, isort);
  array = boolector_array (btor, asort, "array");
  /* we write arbitrary search value into array at an arbitrary index */
  val   = boolector_var (btor, esort, "search_val");
  index = boolector_var (btor, isort, "search_index");
  temp  = boolector_write (btor, array, index, val);
  boolector_release (btor, array);
  array = temp;
  found = boolector_const (btor, "0");
  /* we search */
  for (i = 0; i < num_elements; i++)
  {
    read = boolector_read (btor, array, indices[i]);
    eq   = boolector_eq (btor, read, val);
    temp = boolector_or (btor, found, eq);
    boolector_release (btor, found);
    found = temp;
    boolector_release (btor, read);
    boolector_release (btor, eq);
  }
  /* we negate the formula and show that it is unsatisfiable */
  formula = boolector_not (btor, found);
  boolector_dump_btor_node (btor, stdout, formula);
  /* clean up */
  for (i = 0; i < num_elements; i++) boolector_release (btor, indices[i]);
  boolector_release (btor, formula);
  boolector_release (btor, index);
  boolector_release (btor, val);
  boolector_release (btor, found);
  boolector_release (btor, array);
  boolector_release_sort (btor, isort);
  boolector_release_sort (btor, esort);
  boolector_release_sort (btor, asort);
  boolector_delete (btor);
  free (indices);
  return 0;
}