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
/*
* This file is part of the source code of the software program
* Vampire. It is protected by applicable
* copyright laws.
*
* This source code is distributed under the licence found here
* https://vprover.github.io/license.html
* and in the source directory
*/
/**
* @file NameArray.cpp
* Implements ordered immutable arrays of integers.
* Previously they were used in Options.
*
* @since 21/04/2005 Manchester
*/
#include <cstring>
#include "Debug/Assertion.hpp"
#include "Exception.hpp"
#include "NameArray.hpp"
namespace Lib {
NameArray::NameArray (const char* array[],int len)
: length(len),
_array(array)
{
#if VDEBUG
for (int i=1; i<len; i++) {
ASS_REP2(strcmp(array[i-1],array[i]) < 0,
array[i-1],
array[i]);
}
#endif
} // NameArray::NameArray
/**
* Find the index of a string in the name array. Throw a
* ValueNotFoundException if it is not present.
*
* @return the index
* @since 12/11/2004 Manchester
*/
int NameArray::find (const char* value) const
{
int res=tryToFind(value);
if(res==-1) {
throw ValueNotFoundException();
}
return res;
} // Options::find
/**
* Find the index of a string in the name array. Return -1
* if it is not present.
*/
int NameArray::tryToFind(const char* value) const
{
int from = 0;
int to = length;
while (from < to) {
int mid = (from+to)/2;
int comp = strcmp(_array[mid],value);
if (comp < 0) {
from = mid+1;
}
else if (comp == 0) {
return mid;
}
else { // comp > 0
to = mid;
}
}
return -1;
}
} // namespace Lib