Skip to content

Commit 253537f

Browse files
committed
insert() cannot fail and should not return a value
1 parent 3204145 commit 253537f

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

m100-decomment.lex

+11-8
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,22 @@
1212

1313
#include <string.h>
1414
#include <ctype.h>
15-
int insert(int set[], int n);
15+
void insert(int set[], int n);
1616
void print_set(int set[]);
1717
void print_intersection(int seta[], int setb[]);
1818

19-
int insert(int set[], int n) {
20-
set[0]++;
21-
int len=set[0], i=1;
22-
for (i=1; i<len; i++) {
23-
if (set[i] > n) break;
24-
if (set[i] == n) { set[0]--; return 0; }
19+
/* Insert a number into the set, if it isn't already there. */
20+
/* Minor optimization: start at the end of the array since it is sorted. */
21+
void insert(int set[], int n) {
22+
int i, len=set[0];
23+
for (i=len; i>0; i--) {
24+
if (set[i] == n) return;
25+
if (set[i] < n) break;
2526
}
26-
memmove(set+i+1, set+i, (len-i)*sizeof(set[0]));
27+
i++;
28+
memmove(set+i+1, set+i, len*sizeof(set[0]));
2729
set[i] = n;
30+
set[0]++;
2831
}
2932

3033
/* Define states that simply copy text instead of lexing */

m100-jumps.lex

+3-3
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ LINERANGE {LINENUM}?[ \t]*-[ \t]*{LINENUM}?
3737
int parse_linelist(char *);
3838
int parse_linerange(char *);
3939
int parse_erl(char *);
40-
int insert(int set[], int n);
40+
void insert(int set[], int n);
4141
void print_set(int set[]);
4242
void print_intersection(int seta[], int setb[]);
4343

@@ -50,10 +50,10 @@ LINERANGE {LINENUM}?[ \t]*-[ \t]*{LINENUM}?
5050

5151
/* Insert a number into the set, if it isn't already there. */
5252
/* Minor optimization: start at the end of the array since it is sorted. */
53-
int insert(int set[], int n) {
53+
void insert(int set[], int n) {
5454
int i, len=set[0];
5555
for (i=len; i>0; i--) {
56-
if (set[i] == n) return 0;
56+
if (set[i] == n) return;
5757
if (set[i] < n) break;
5858
}
5959
i++;

0 commit comments

Comments
 (0)