Skip to content

Commit b981312

Browse files
committed
Add numset versions for lecture.
1 parent f1ed8ac commit b981312

13 files changed

+58
-47
lines changed

future/numset.h

-11
This file was deleted.
File renamed without changes.

future/GNUmakefile storage1/GNUmakefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ numset-%: numset.o %.o
1515
$(CC) $(CFLAGS) -o $@ $^
1616

1717
clean:
18-
rm -f $(ALLPROGRAMS) *.o
18+
rm -f *.o $(ALLPROGRAMS)
1919
rm -rf $(DEPSDIR) *.dSYM
2020

2121
.PHONY: all clean

future/array.c storage1/array.c

+5-5
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ void numset_add(numset* s, unsigned value) {
3939
++s->size;
4040
}
4141

42-
unsigned numset_remove_index(numset* s, unsigned index) {
43-
if (index < s->size) {
44-
unsigned value = s->v[index];
45-
memmove(&s->v[index], &s->v[index + 1],
46-
sizeof(unsigned) * (s->size - index - 1));
42+
unsigned numset_remove_index(numset* s, unsigned pos) {
43+
if (pos < s->size) {
44+
unsigned value = s->v[pos];
45+
memmove(&s->v[pos], &s->v[pos + 1],
46+
sizeof(unsigned) * (s->size - pos - 1));
4747
--s->size;
4848
return value;
4949
} else

future/basiclist.c storage1/basiclist.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,13 @@ void numset_add(numset* s, unsigned value) {
3939
s->head = n;
4040
}
4141

42-
unsigned numset_remove_index(numset* s, unsigned index) {
42+
unsigned numset_remove_index(numset* s, unsigned pos) {
4343
node* prev = NULL;
4444
node* trav = s->head;
45-
while (trav && index != 0) {
45+
while (trav && pos != 0) {
4646
prev = trav;
4747
trav = trav->next;
48-
--index;
48+
--pos;
4949
}
5050

5151
if (trav) {

future/bsearchvector.c storage1/bsearchvector.c

+5-5
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,11 @@ void numset_add(numset* s, unsigned value) {
5454
++s->size;
5555
}
5656

57-
unsigned numset_remove_index(numset* s, unsigned index) {
58-
if (index < s->size) {
59-
unsigned value = s->values[index];
60-
memmove(&s->values[index], &s->values[index + 1],
61-
sizeof(unsigned) * (s->size - index - 1));
57+
unsigned numset_remove_index(numset* s, unsigned pos) {
58+
if (pos < s->size) {
59+
unsigned value = s->values[pos];
60+
memmove(&s->values[pos], &s->values[pos + 1],
61+
sizeof(unsigned) * (s->size - pos - 1));
6262
--s->size;
6363
return value;
6464
} else

future/file.c storage1/file.c

+5-5
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ void numset_add(numset* s, unsigned value) {
5959
fclose(out);
6060
}
6161

62-
unsigned numset_remove_index(numset* s, unsigned index) {
62+
unsigned numset_remove_index(numset* s, unsigned pos) {
6363
(void) s; // avoid uninitialized-variable warning
6464

6565
// rename old file to backup
@@ -73,17 +73,17 @@ unsigned numset_remove_index(numset* s, unsigned index) {
7373

7474
// read values from old file until the right position;
7575
// then write value
76-
unsigned cur_index = 0, value = 0;
76+
unsigned cur_pos = 0, value = 0;
7777
while (1) {
7878
unsigned x;
7979
r = fscanf(in, "%u", &x);
8080
if (r == 0 || r == EOF)
8181
break;
82-
if (index == cur_index)
82+
if (pos == cur_pos)
8383
value = x;
84-
if (index != cur_index)
84+
if (pos != cur_pos)
8585
fprintf(out, "%u\n", x);
86-
++cur_index;
86+
++cur_pos;
8787
}
8888

8989
// close files

future/list.c storage1/list.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ void numset_add(numset* s, unsigned value) {
3939
*pprev = n;
4040
}
4141

42-
unsigned numset_remove_index(numset* s, unsigned index) {
42+
unsigned numset_remove_index(numset* s, unsigned pos) {
4343
node** pprev = &s->head;
44-
while (*pprev && index != 0) {
44+
while (*pprev && pos != 0) {
4545
pprev = &(*pprev)->next;
46-
--index;
46+
--pos;
4747
}
4848

4949
if (*pprev) {

future/numset.c storage1/numset.c

File renamed without changes.

storage1/numset.h

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#ifndef CS61_NUMSET_H
2+
#define CS61_NUMSET_H
3+
4+
typedef struct numset numset;
5+
6+
// numset_new()
7+
// Return a new numset, which is a multiset of unsigned values.
8+
numset *numset_new(void);
9+
10+
// numset_add(s, x)
11+
// Add value `x` to numset `s`.
12+
void numset_add(numset *s, unsigned x);
13+
14+
// numset_remove_index(s, pos)
15+
// Remove and return the `pos`th largest element in `s`. For
16+
// instance, `numset_remove_sorted(s, 0)` will remove and return
17+
// the numerically lowest value in `s`. If there is no such
18+
// element (if `pos` is greater than or equal to the number of
19+
// values in `s`), returns 0.
20+
unsigned numset_remove_index(numset *s, unsigned pos);
21+
22+
#endif

future/plot.pl storage1/plot.pl

File renamed without changes.

future/tree.c storage1/tree.c

+8-8
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
typedef struct node {
1414
unsigned value;
15-
bool color;
15+
int color;
1616
struct node* child[2];
1717
struct node* parent;
1818
unsigned count;
@@ -31,11 +31,11 @@ static inline void tree_free(node* t) {
3131
free(t);
3232
}
3333

34-
static inline bool tree_isred(node* t) {
34+
static inline int tree_isred(node* t) {
3535
return t && t->color;
3636
}
3737

38-
static inline bool tree_children_same_color(node* t) {
38+
static inline int tree_children_same_color(node* t) {
3939
return tree_isred(t->child[0]) == tree_isred(t->child[1]);
4040
}
4141

@@ -290,18 +290,18 @@ void numset_add(numset* s, unsigned value) {
290290
tree_insert_node(&s->root, new_node);
291291
}
292292

293-
unsigned numset_remove_index(numset* s, unsigned index) {
293+
unsigned numset_remove_index(numset* s, unsigned pos) {
294294
node* t = s->root;
295295
while (t) {
296-
unsigned t_index = tree_count(t->child[0]);
297-
if (t_index > index)
296+
unsigned t_pos = tree_count(t->child[0]);
297+
if (t_pos > pos)
298298
t = t->child[0];
299-
else if (t_index == index) {
299+
else if (t_pos == pos) {
300300
unsigned value = t->value;
301301
tree_remove_node(&s->root, t);
302302
return value;
303303
} else {
304-
index -= t_index + 1;
304+
pos -= t_pos + 1;
305305
t = t->child[1];
306306
}
307307
}

future/vector.c storage1/vector.c

+6-6
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
// Numset implementation that uses a smart array data structure.
77
// This code reallocates the array much less frequently than array.c does.
88
// It uses the "vector pattern" explained on the C Patterns page:
9-
// http://cs61.seas.harvard.edu/wiki/2013/Patterns
9+
// http://cs61.seas.harvard.edu/wiki/2016/Patterns
1010

1111
struct numset {
1212
unsigned* v;
@@ -50,11 +50,11 @@ void numset_add(numset* s, unsigned value) {
5050
++s->size;
5151
}
5252

53-
unsigned numset_remove_index(numset* s, unsigned index) {
54-
if (index < s->size) {
55-
unsigned value = s->v[index];
56-
memmove(&s->v[index], &s->v[index + 1],
57-
sizeof(unsigned) * (s->size - index - 1));
53+
unsigned numset_remove_index(numset* s, unsigned pos) {
54+
if (pos < s->size) {
55+
unsigned value = s->v[pos];
56+
memmove(&s->v[pos], &s->v[pos + 1],
57+
sizeof(unsigned) * (s->size - pos - 1));
5858
--s->size;
5959
return value;
6060
} else

0 commit comments

Comments
 (0)