Browse code

Move sources into src directory

John Hawthorn authored on 21/05/2016 21:56:03
Showing 1 changed files
1 1
deleted file mode 100644
... ...
@@ -1,168 +0,0 @@
1
-#include <stdlib.h>
2
-#include <stdio.h>
3
-#include <string.h>
4
-
5
-#include "choices.h"
6
-#include "match.h"
7
-
8
-/* Initial size of buffer for storing input in memory */
9
-#define INITIAL_BUFFER_CAPACITY 4096
10
-
11
-/* Initial size of choices array */
12
-#define INITIAL_CHOICE_CAPACITY 128
13
-
14
-static int cmpchoice(const void *_idx1, const void *_idx2) {
15
-	const struct scored_result *a = _idx1;
16
-	const struct scored_result *b = _idx2;
17
-
18
-	if (a->score == b->score)
19
-		return 0;
20
-	else if (a->score < b->score)
21
-		return 1;
22
-	else
23
-		return -1;
24
-}
25
-
26
-static void *safe_realloc(void *buffer, size_t size) {
27
-	buffer = realloc(buffer, size);
28
-	if (!buffer) {
29
-		fprintf(stderr, "Error: Can't allocate memory (%zu bytes)\n", size);
30
-		abort();
31
-	}
32
-
33
-	return buffer;
34
-}
35
-
36
-void choices_fread(choices_t *c, FILE *file) {
37
-	/* Save current position for parsing later */
38
-	size_t buffer_start = c->buffer_size;
39
-
40
-	/* Resize buffer to at least one byte more capacity than our current
41
-	 * size. This uses a power of two of INITIAL_BUFFER_CAPACITY.
42
-	 * This must work even when c->buffer is NULL and c->buffer_size is 0
43
-	 */
44
-	size_t capacity = INITIAL_BUFFER_CAPACITY;
45
-	while (capacity <= c->buffer_size)
46
-		capacity *= 2;
47
-	c->buffer = safe_realloc(c->buffer, capacity);
48
-
49
-	/* Continue reading until we get a "short" read, indicating EOF */
50
-	while ((c->buffer_size += fread(c->buffer + c->buffer_size, 1, capacity - c->buffer_size, file)) == capacity) {
51
-		capacity *= 2;
52
-		c->buffer = safe_realloc(c->buffer, capacity);
53
-	}
54
-	c->buffer = safe_realloc(c->buffer, c->buffer_size + 1);
55
-	c->buffer[c->buffer_size++] = '\0';
56
-
57
-	/* Truncate buffer to used size, (maybe) freeing some memory for
58
-	 * future allocations.
59
-	 */
60
-
61
-	/* Tokenize input and add to choices */
62
-	char *line = c->buffer + buffer_start;
63
-	do {
64
-		char *nl = strchr(line, '\n');
65
-		if (nl)
66
-			*nl++ = '\0';
67
-
68
-		/* Skip empty lines */
69
-		if (*line)
70
-			choices_add(c, line);
71
-
72
-		line = nl;
73
-	} while (line);
74
-}
75
-
76
-static void choices_resize(choices_t *c, size_t new_capacity) {
77
-	c->strings = safe_realloc(c->strings, new_capacity * sizeof(const char *));
78
-	c->capacity = new_capacity;
79
-}
80
-
81
-static void choices_reset_search(choices_t *c) {
82
-	free(c->results);
83
-	c->selection = c->available = 0;
84
-	c->results = NULL;
85
-}
86
-
87
-void choices_init(choices_t *c) {
88
-	c->strings = NULL;
89
-	c->results = NULL;
90
-
91
-	c->buffer_size = 0;
92
-	c->buffer = NULL;
93
-
94
-	c->capacity = c->size = 0;
95
-	choices_resize(c, INITIAL_CHOICE_CAPACITY);
96
-
97
-	choices_reset_search(c);
98
-}
99
-
100
-void choices_destroy(choices_t *c) {
101
-	free(c->buffer);
102
-	c->buffer = NULL;
103
-	c->buffer_size = 0;
104
-
105
-	free(c->strings);
106
-	c->strings = NULL;
107
-	c->capacity = c->size = 0;
108
-
109
-	free(c->results);
110
-	c->results = NULL;
111
-	c->available = c->selection = 0;
112
-}
113
-
114
-void choices_add(choices_t *c, const char *choice) {
115
-	/* Previous search is now invalid */
116
-	choices_reset_search(c);
117
-
118
-	if (c->size == c->capacity) {
119
-		choices_resize(c, c->capacity * 2);
120
-	}
121
-	c->strings[c->size++] = choice;
122
-}
123
-
124
-size_t choices_available(choices_t *c) {
125
-	return c->available;
126
-}
127
-
128
-void choices_search(choices_t *c, const char *search) {
129
-	choices_reset_search(c);
130
-
131
-	c->results = malloc(c->size * sizeof(struct scored_result));
132
-	if (!c->results) {
133
-		fprintf(stderr, "Error: Can't allocate memory\n");
134
-		abort();
135
-	}
136
-
137
-	for (size_t i = 0; i < c->size; i++) {
138
-		if (has_match(search, c->strings[i])) {
139
-			c->results[c->available].str = c->strings[i];
140
-			c->results[c->available].score = match(search, c->strings[i]);
141
-			c->available++;
142
-		}
143
-	}
144
-
145
-	qsort(c->results, c->available, sizeof(struct scored_result), cmpchoice);
146
-}
147
-
148
-const char *choices_get(choices_t *c, size_t n) {
149
-	if (n < c->available) {
150
-		return c->results[n].str;
151
-	} else {
152
-		return NULL;
153
-	}
154
-}
155
-
156
-double choices_getscore(choices_t *c, size_t n) {
157
-	return c->results[n].score;
158
-}
159
-
160
-void choices_prev(choices_t *c) {
161
-	if (c->available)
162
-		c->selection = (c->selection + c->available - 1) % c->available;
163
-}
164
-
165
-void choices_next(choices_t *c) {
166
-	if (c->available)
167
-		c->selection = (c->selection + 1) % c->available;
168
-}
Browse code

Use '\0' instead of 0 when we mean a char.

John Hawthorn authored on 17/05/2016 02:28:32
Showing 1 changed files
... ...
@@ -52,7 +52,7 @@ void choices_fread(choices_t *c, FILE *file) {
52 52
 		c->buffer = safe_realloc(c->buffer, capacity);
53 53
 	}
54 54
 	c->buffer = safe_realloc(c->buffer, c->buffer_size + 1);
55
-	c->buffer[c->buffer_size++] = 0;
55
+	c->buffer[c->buffer_size++] = '\0';
56 56
 
57 57
 	/* Truncate buffer to used size, (maybe) freeing some memory for
58 58
 	 * future allocations.
Browse code

Rename choices_free to choices_destroy

To clarify that we aren't freeing the choices structure, just the
buffers associated with it.

John Hawthorn authored on 03/05/2016 04:45:26
Showing 1 changed files
... ...
@@ -97,7 +97,7 @@ void choices_init(choices_t *c) {
97 97
 	choices_reset_search(c);
98 98
 }
99 99
 
100
-void choices_free(choices_t *c) {
100
+void choices_destroy(choices_t *c) {
101 101
 	free(c->buffer);
102 102
 	c->buffer = NULL;
103 103
 	c->buffer_size = 0;
Browse code

Store read buffer on choices structure

This would allow reading from multiple files (which we don't need) as
well as allowing us to free the buffer with the rest of the choices
structure (which is nice).

John Hawthorn authored on 01/05/2016 00:28:55
Showing 1 changed files
... ...
@@ -6,7 +6,7 @@
6 6
 #include "match.h"
7 7
 
8 8
 /* Initial size of buffer for storing input in memory */
9
-#define INITIAL_BUFFER_SIZE     4096
9
+#define INITIAL_BUFFER_CAPACITY 4096
10 10
 
11 11
 /* Initial size of choices array */
12 12
 #define INITIAL_CHOICE_CAPACITY 128
... ...
@@ -34,23 +34,32 @@ static void *safe_realloc(void *buffer, size_t size) {
34 34
 }
35 35
 
36 36
 void choices_fread(choices_t *c, FILE *file) {
37
-	size_t bufsize = INITIAL_BUFFER_SIZE, pos = 0;
38
-	char *buf = safe_realloc(NULL, bufsize);
37
+	/* Save current position for parsing later */
38
+	size_t buffer_start = c->buffer_size;
39
+
40
+	/* Resize buffer to at least one byte more capacity than our current
41
+	 * size. This uses a power of two of INITIAL_BUFFER_CAPACITY.
42
+	 * This must work even when c->buffer is NULL and c->buffer_size is 0
43
+	 */
44
+	size_t capacity = INITIAL_BUFFER_CAPACITY;
45
+	while (capacity <= c->buffer_size)
46
+		capacity *= 2;
47
+	c->buffer = safe_realloc(c->buffer, capacity);
39 48
 
40 49
 	/* Continue reading until we get a "short" read, indicating EOF */
41
-	while ((pos += fread(buf + pos, 1, bufsize - pos, file)) == bufsize) {
42
-		bufsize *= 2;
43
-		buf = safe_realloc(buf, bufsize);
50
+	while ((c->buffer_size += fread(c->buffer + c->buffer_size, 1, capacity - c->buffer_size, file)) == capacity) {
51
+		capacity *= 2;
52
+		c->buffer = safe_realloc(c->buffer, capacity);
44 53
 	}
45
-	buf[pos] = 0;
54
+	c->buffer = safe_realloc(c->buffer, c->buffer_size + 1);
55
+	c->buffer[c->buffer_size++] = 0;
46 56
 
47 57
 	/* Truncate buffer to used size, (maybe) freeing some memory for
48 58
 	 * future allocations.
49 59
 	 */
50
-	buf = safe_realloc(buf, pos + 1);
51 60
 
52 61
 	/* Tokenize input and add to choices */
53
-	char *line = buf;
62
+	char *line = c->buffer + buffer_start;
54 63
 	do {
55 64
 		char *nl = strchr(line, '\n');
56 65
 		if (nl)
... ...
@@ -78,14 +87,28 @@ static void choices_reset_search(choices_t *c) {
78 87
 void choices_init(choices_t *c) {
79 88
 	c->strings = NULL;
80 89
 	c->results = NULL;
90
+
91
+	c->buffer_size = 0;
92
+	c->buffer = NULL;
93
+
81 94
 	c->capacity = c->size = 0;
82
-	choices_reset_search(c);
83 95
 	choices_resize(c, INITIAL_CHOICE_CAPACITY);
96
+
97
+	choices_reset_search(c);
84 98
 }
85 99
 
86 100
 void choices_free(choices_t *c) {
101
+	free(c->buffer);
102
+	c->buffer = NULL;
103
+	c->buffer_size = 0;
104
+
87 105
 	free(c->strings);
106
+	c->strings = NULL;
107
+	c->capacity = c->size = 0;
108
+
88 109
 	free(c->results);
110
+	c->results = NULL;
111
+	c->available = c->selection = 0;
89 112
 }
90 113
 
91 114
 void choices_add(choices_t *c, const char *choice) {
Browse code

Set sensible initial sizes for growing arrays

Any values should work here, and it doesn't seem to make a siginificant
difference to startup time, as this only adds or reduces a few realloc
calls.

Starts input buffer at 4k of memory, which is nice for being the size of
a page and the size usually used in the read syscall. Initial choice
capacity set to 128, by assuming that candidates average 32 characters
long.

John Hawthorn authored on 24/04/2016 20:37:36
Showing 1 changed files
... ...
@@ -5,7 +5,11 @@
5 5
 #include "choices.h"
6 6
 #include "match.h"
7 7
 
8
-#define INITIAL_CAPACITY 1
8
+/* Initial size of buffer for storing input in memory */
9
+#define INITIAL_BUFFER_SIZE     4096
10
+
11
+/* Initial size of choices array */
12
+#define INITIAL_CHOICE_CAPACITY 128
9 13
 
10 14
 static int cmpchoice(const void *_idx1, const void *_idx2) {
11 15
 	const struct scored_result *a = _idx1;
... ...
@@ -30,7 +34,7 @@ static void *safe_realloc(void *buffer, size_t size) {
30 34
 }
31 35
 
32 36
 void choices_fread(choices_t *c, FILE *file) {
33
-	size_t bufsize = 65536, pos = 0;
37
+	size_t bufsize = INITIAL_BUFFER_SIZE, pos = 0;
34 38
 	char *buf = safe_realloc(NULL, bufsize);
35 39
 
36 40
 	/* Continue reading until we get a "short" read, indicating EOF */
... ...
@@ -76,7 +80,7 @@ void choices_init(choices_t *c) {
76 80
 	c->results = NULL;
77 81
 	c->capacity = c->size = 0;
78 82
 	choices_reset_search(c);
79
-	choices_resize(c, INITIAL_CAPACITY);
83
+	choices_resize(c, INITIAL_CHOICE_CAPACITY);
80 84
 }
81 85
 
82 86
 void choices_free(choices_t *c) {
Browse code

Move input reading to choices.c

Also refactored and added comments.

John Hawthorn authored on 23/04/2016 18:25:47
Showing 1 changed files
... ...
@@ -1,5 +1,6 @@
1 1
 #include <stdlib.h>
2 2
 #include <stdio.h>
3
+#include <string.h>
3 4
 
4 5
 #include "choices.h"
5 6
 #include "match.h"
... ...
@@ -18,14 +19,49 @@ static int cmpchoice(const void *_idx1, const void *_idx2) {
18 19
 		return -1;
19 20
 }
20 21
 
21
-static void choices_resize(choices_t *c, size_t new_capacity) {
22
-	c->strings = realloc(c->strings, new_capacity * sizeof(const char *));
23
-
24
-	if (!c->strings) {
25
-		fprintf(stderr, "Error: Can't allocate memory\n");
22
+static void *safe_realloc(void *buffer, size_t size) {
23
+	buffer = realloc(buffer, size);
24
+	if (!buffer) {
25
+		fprintf(stderr, "Error: Can't allocate memory (%zu bytes)\n", size);
26 26
 		abort();
27 27
 	}
28 28
 
29
+	return buffer;
30
+}
31
+
32
+void choices_fread(choices_t *c, FILE *file) {
33
+	size_t bufsize = 65536, pos = 0;
34
+	char *buf = safe_realloc(NULL, bufsize);
35
+
36
+	/* Continue reading until we get a "short" read, indicating EOF */
37
+	while ((pos += fread(buf + pos, 1, bufsize - pos, file)) == bufsize) {
38
+		bufsize *= 2;
39
+		buf = safe_realloc(buf, bufsize);
40
+	}
41
+	buf[pos] = 0;
42
+
43
+	/* Truncate buffer to used size, (maybe) freeing some memory for
44
+	 * future allocations.
45
+	 */
46
+	buf = safe_realloc(buf, pos + 1);
47
+
48
+	/* Tokenize input and add to choices */
49
+	char *line = buf;
50
+	do {
51
+		char *nl = strchr(line, '\n');
52
+		if (nl)
53
+			*nl++ = '\0';
54
+
55
+		/* Skip empty lines */
56
+		if (*line)
57
+			choices_add(c, line);
58
+
59
+		line = nl;
60
+	} while (line);
61
+}
62
+
63
+static void choices_resize(choices_t *c, size_t new_capacity) {
64
+	c->strings = safe_realloc(c->strings, new_capacity * sizeof(const char *));
29 65
 	c->capacity = new_capacity;
30 66
 }
31 67
 
Browse code

Use size_t where appropriate

John Hawthorn authored on 07/11/2015 09:58:09
Showing 1 changed files
... ...
@@ -18,7 +18,7 @@ static int cmpchoice(const void *_idx1, const void *_idx2) {
18 18
 		return -1;
19 19
 }
20 20
 
21
-static void choices_resize(choices_t *c, int new_capacity) {
21
+static void choices_resize(choices_t *c, size_t new_capacity) {
22 22
 	c->strings = realloc(c->strings, new_capacity * sizeof(const char *));
23 23
 
24 24
 	if (!c->strings) {
Browse code

Allow customization of config.h via config.def.h

John Hawthorn authored on 07/11/2015 09:37:03
Showing 1 changed files
... ...
@@ -89,6 +89,7 @@ const char *choices_get(choices_t *c, size_t n) {
89 89
 		return NULL;
90 90
 	}
91 91
 }
92
+
92 93
 double choices_getscore(choices_t *c, size_t n) {
93 94
 	return c->results[n].score;
94 95
 }
Browse code

Apply clang-format to all files

Apologies that this uses my preferred formatting style: mostly the same
as Linux, but without a break between function and brace. Adds spaces in
a few places they weren't before.

John Hawthorn authored on 07/11/2015 05:45:02
Showing 1 changed files
... ...
@@ -10,18 +10,18 @@ static int cmpchoice(const void *_idx1, const void *_idx2) {
10 10
 	const struct scored_result *a = _idx1;
11 11
 	const struct scored_result *b = _idx2;
12 12
 
13
-	if(a->score == b->score)
13
+	if (a->score == b->score)
14 14
 		return 0;
15
-	else if(a->score < b->score)
15
+	else if (a->score < b->score)
16 16
 		return 1;
17 17
 	else
18 18
 		return -1;
19 19
 }
20 20
 
21
-static void choices_resize(choices_t *c, int new_capacity){
21
+static void choices_resize(choices_t *c, int new_capacity) {
22 22
 	c->strings = realloc(c->strings, new_capacity * sizeof(const char *));
23 23
 
24
-	if(!c->strings){
24
+	if (!c->strings) {
25 25
 		fprintf(stderr, "Error: Can't allocate memory\n");
26 26
 		abort();
27 27
 	}
... ...
@@ -29,13 +29,13 @@ static void choices_resize(choices_t *c, int new_capacity){
29 29
 	c->capacity = new_capacity;
30 30
 }
31 31
 
32
-static void choices_reset_search(choices_t *c){
32
+static void choices_reset_search(choices_t *c) {
33 33
 	free(c->results);
34 34
 	c->selection = c->available = 0;
35 35
 	c->results = NULL;
36 36
 }
37 37
 
38
-void choices_init(choices_t *c){
38
+void choices_init(choices_t *c) {
39 39
 	c->strings = NULL;
40 40
 	c->results = NULL;
41 41
 	c->capacity = c->size = 0;
... ...
@@ -43,36 +43,36 @@ void choices_init(choices_t *c){
43 43
 	choices_resize(c, INITIAL_CAPACITY);
44 44
 }
45 45
 
46
-void choices_free(choices_t *c){
46
+void choices_free(choices_t *c) {
47 47
 	free(c->strings);
48 48
 	free(c->results);
49 49
 }
50 50
 
51
-void choices_add(choices_t *c, const char *choice){
51
+void choices_add(choices_t *c, const char *choice) {
52 52
 	/* Previous search is now invalid */
53 53
 	choices_reset_search(c);
54 54
 
55
-	if(c->size == c->capacity){
55
+	if (c->size == c->capacity) {
56 56
 		choices_resize(c, c->capacity * 2);
57 57
 	}
58 58
 	c->strings[c->size++] = choice;
59 59
 }
60 60
 
61
-size_t choices_available(choices_t *c){
61
+size_t choices_available(choices_t *c) {
62 62
 	return c->available;
63 63
 }
64 64
 
65
-void choices_search(choices_t *c, const char *search){
65
+void choices_search(choices_t *c, const char *search) {
66 66
 	choices_reset_search(c);
67 67
 
68 68
 	c->results = malloc(c->size * sizeof(struct scored_result));
69
-	if(!c->results){
69
+	if (!c->results) {
70 70
 		fprintf(stderr, "Error: Can't allocate memory\n");
71 71
 		abort();
72 72
 	}
73 73
 
74
-	for(size_t i = 0; i < c->size; i++){
75
-		if(has_match(search, c->strings[i])){
74
+	for (size_t i = 0; i < c->size; i++) {
75
+		if (has_match(search, c->strings[i])) {
76 76
 			c->results[c->available].str = c->strings[i];
77 77
 			c->results[c->available].score = match(search, c->strings[i]);
78 78
 			c->available++;
... ...
@@ -82,24 +82,23 @@ void choices_search(choices_t *c, const char *search){
82 82
 	qsort(c->results, c->available, sizeof(struct scored_result), cmpchoice);
83 83
 }
84 84
 
85
-const char *choices_get(choices_t *c, size_t n){
86
-	if(n < c->available){
85
+const char *choices_get(choices_t *c, size_t n) {
86
+	if (n < c->available) {
87 87
 		return c->results[n].str;
88
-	}else{
88
+	} else {
89 89
 		return NULL;
90 90
 	}
91 91
 }
92
-double choices_getscore(choices_t *c, size_t n){
92
+double choices_getscore(choices_t *c, size_t n) {
93 93
 	return c->results[n].score;
94 94
 }
95 95
 
96
-void choices_prev(choices_t *c){
97
-	if(c->available)
96
+void choices_prev(choices_t *c) {
97
+	if (c->available)
98 98
 		c->selection = (c->selection + c->available - 1) % c->available;
99 99
 }
100 100
 
101
-void choices_next(choices_t *c){
102
-	if(c->available)
101
+void choices_next(choices_t *c) {
102
+	if (c->available)
103 103
 		c->selection = (c->selection + 1) % c->available;
104 104
 }
105
-
Browse code

Store string in result instead of position

John Hawthorn authored on 21/09/2014 21:40:36
Showing 1 changed files
... ...
@@ -7,8 +7,8 @@
7 7
 #define INITIAL_CAPACITY 1
8 8
 
9 9
 static int cmpchoice(const void *_idx1, const void *_idx2) {
10
-	const struct scored_position *a = _idx1;
11
-	const struct scored_position *b = _idx2;
10
+	const struct scored_result *a = _idx1;
11
+	const struct scored_result *b = _idx2;
12 12
 
13 13
 	if(a->score == b->score)
14 14
 		return 0;
... ...
@@ -65,7 +65,7 @@ size_t choices_available(choices_t *c){
65 65
 void choices_search(choices_t *c, const char *search){
66 66
 	choices_reset_search(c);
67 67
 
68
-	c->results = malloc(c->size * sizeof(struct scored_position));
68
+	c->results = malloc(c->size * sizeof(struct scored_result));
69 69
 	if(!c->results){
70 70
 		fprintf(stderr, "Error: Can't allocate memory\n");
71 71
 		abort();
... ...
@@ -73,18 +73,18 @@ void choices_search(choices_t *c, const char *search){
73 73
 
74 74
 	for(size_t i = 0; i < c->size; i++){
75 75
 		if(has_match(search, c->strings[i])){
76
-			c->results[c->available].position = i;
76
+			c->results[c->available].str = c->strings[i];
77 77
 			c->results[c->available].score = match(search, c->strings[i]);
78 78
 			c->available++;
79 79
 		}
80 80
 	}
81 81
 
82
-	qsort(c->results, c->available, sizeof(struct scored_position), cmpchoice);
82
+	qsort(c->results, c->available, sizeof(struct scored_result), cmpchoice);
83 83
 }
84 84
 
85 85
 const char *choices_get(choices_t *c, size_t n){
86 86
 	if(n < c->available){
87
-		return c->strings[c->results[n].position];
87
+		return c->results[n].str;
88 88
 	}else{
89 89
 		return NULL;
90 90
 	}
Browse code

Allocate results array when search is run.

John Hawthorn authored on 21/09/2014 21:23:44
Showing 1 changed files
... ...
@@ -20,24 +20,26 @@ static int cmpchoice(const void *_idx1, const void *_idx2) {
20 20
 
21 21
 static void choices_resize(choices_t *c, int new_capacity){
22 22
 	c->strings = realloc(c->strings, new_capacity * sizeof(const char *));
23
-	c->results = realloc(c->results, new_capacity * sizeof(struct scored_position));
24 23
 
25
-	if(!c->strings || !c->results){
24
+	if(!c->strings){
26 25
 		fprintf(stderr, "Error: Can't allocate memory\n");
27 26
 		abort();
28 27
 	}
29 28
 
30
-	for(int i = c->capacity; i < new_capacity; i++){
31
-		c->strings[i] = NULL;
32
-	}
33 29
 	c->capacity = new_capacity;
34 30
 }
35 31
 
32
+static void choices_reset_search(choices_t *c){
33
+	free(c->results);
34
+	c->selection = c->available = 0;
35
+	c->results = NULL;
36
+}
37
+
36 38
 void choices_init(choices_t *c){
37 39
 	c->strings = NULL;
38 40
 	c->results = NULL;
39 41
 	c->capacity = c->size = 0;
40
-	c->selection = c->available = 0;
42
+	choices_reset_search(c);
41 43
 	choices_resize(c, INITIAL_CAPACITY);
42 44
 }
43 45
 
... ...
@@ -47,6 +49,9 @@ void choices_free(choices_t *c){
47 49
 }
48 50
 
49 51
 void choices_add(choices_t *c, const char *choice){
52
+	/* Previous search is now invalid */
53
+	choices_reset_search(c);
54
+
50 55
 	if(c->size == c->capacity){
51 56
 		choices_resize(c, c->capacity * 2);
52 57
 	}
... ...
@@ -58,8 +63,13 @@ size_t choices_available(choices_t *c){
58 63
 }
59 64
 
60 65
 void choices_search(choices_t *c, const char *search){
61
-	c->selection = 0;
62
-	c->available = 0;
66
+	choices_reset_search(c);
67
+
68
+	c->results = malloc(c->size * sizeof(struct scored_position));
69
+	if(!c->results){
70
+		fprintf(stderr, "Error: Can't allocate memory\n");
71
+		abort();
72
+	}
63 73
 
64 74
 	for(size_t i = 0; i < c->size; i++){
65 75
 		if(has_match(search, c->strings[i])){
Browse code

Remove stray ;

John Hawthorn authored on 18/09/2014 07:24:24
Showing 1 changed files
... ...
@@ -80,7 +80,7 @@ const char *choices_get(choices_t *c, size_t n){
80 80
 	}
81 81
 }
82 82
 double choices_getscore(choices_t *c, size_t n){
83
-	return c->results[n].score;;
83
+	return c->results[n].score;
84 84
 }
85 85
 
86 86
 void choices_prev(choices_t *c){
Browse code

Add -pedantic, Remove stray ;

John Hawthorn authored on 18/09/2014 03:22:41
Showing 1 changed files
... ...
@@ -40,10 +40,11 @@ void choices_init(choices_t *c){
40 40
 	c->selection = c->available = 0;
41 41
 	choices_resize(c, INITIAL_CAPACITY);
42 42
 }
43
+
43 44
 void choices_free(choices_t *c){
44 45
 	free(c->strings);
45 46
 	free(c->results);
46
-};
47
+}
47 48
 
48 49
 void choices_add(choices_t *c, const char *choice){
49 50
 	if(c->size == c->capacity){
Browse code

Use AoS instead of SoA; don't use qsort_r

Fixes Bus Error on BSDs due to qsort_r not being the same qsort_r as
linux.

John Hawthorn authored on 17/09/2014 17:43:01 • John Hawthorn committed on 18/09/2014 02:11:14
Showing 1 changed files
... ...
@@ -1,4 +1,3 @@
1
-#define _GNU_SOURCE
2 1
 #include <stdlib.h>
3 2
 #include <stdio.h>
4 3
 
... ...
@@ -7,13 +6,13 @@
7 6
 
8 7
 #define INITIAL_CAPACITY 1
9 8
 
10
-static int cmpchoice(size_t *idx1, size_t *idx2, double *choices_score) {
11
-	double score1 = choices_score[*idx1];
12
-	double score2 = choices_score[*idx2];
9
+static int cmpchoice(const void *_idx1, const void *_idx2) {
10
+	const struct scored_position *a = _idx1;
11
+	const struct scored_position *b = _idx2;
13 12
 
14
-	if(score1 == score2)
13
+	if(a->score == b->score)
15 14
 		return 0;
16
-	else if(score1 < score2)
15
+	else if(a->score < b->score)
17 16
 		return 1;
18 17
 	else
19 18
 		return -1;
... ...
@@ -21,10 +20,9 @@ static int cmpchoice(size_t *idx1, size_t *idx2, double *choices_score) {
21 20
 
22 21
 static void choices_resize(choices_t *c, int new_capacity){
23 22
 	c->strings = realloc(c->strings, new_capacity * sizeof(const char *));
24
-	c->scores  = realloc(c->scores,  new_capacity * sizeof(double));
25
-	c->sorted  = realloc(c->sorted,  new_capacity * sizeof(size_t));
23
+	c->results = realloc(c->results, new_capacity * sizeof(struct scored_position));
26 24
 
27
-	if(!c->strings || !c->scores || !c->sorted){
25
+	if(!c->strings || !c->results){
28 26
 		fprintf(stderr, "Error: Can't allocate memory\n");
29 27
 		abort();
30 28
 	}
... ...
@@ -37,16 +35,14 @@ static void choices_resize(choices_t *c, int new_capacity){
37 35
 
38 36
 void choices_init(choices_t *c){
39 37
 	c->strings = NULL;
40
-	c->scores  = NULL;
41
-	c->sorted  = NULL;
38
+	c->results = NULL;
42 39
 	c->capacity = c->size = 0;
43 40
 	c->selection = c->available = 0;
44 41
 	choices_resize(c, INITIAL_CAPACITY);
45 42
 }
46 43
 void choices_free(choices_t *c){
47 44
 	free(c->strings);
48
-	free(c->scores);
49
-	free(c->sorted);
45
+	free(c->results);
50 46
 };
51 47
 
52 48
 void choices_add(choices_t *c, const char *choice){
... ...
@@ -66,23 +62,24 @@ void choices_search(choices_t *c, const char *search){
66 62
 
67 63
 	for(size_t i = 0; i < c->size; i++){
68 64
 		if(has_match(search, c->strings[i])){
69
-			c->scores[i] = match(search, c->strings[i]);
70
-			c->sorted[c->available++] = i;
65
+			c->results[c->available].position = i;
66
+			c->results[c->available].score = match(search, c->strings[i]);
67
+			c->available++;
71 68
 		}
72 69
 	}
73 70
 
74
-	qsort_r(c->sorted, c->available, sizeof(size_t), (int (*)(const void *, const void *, void *))cmpchoice, c->scores);
71
+	qsort(c->results, c->available, sizeof(struct scored_position), cmpchoice);
75 72
 }
76 73
 
77 74
 const char *choices_get(choices_t *c, size_t n){
78 75
 	if(n < c->available){
79
-		return c->strings[c->sorted[n]];
76
+		return c->strings[c->results[n].position];
80 77
 	}else{
81 78
 		return NULL;
82 79
 	}
83 80
 }
84 81
 double choices_getscore(choices_t *c, size_t n){
85
-	return c->scores[c->sorted[n]];;
82
+	return c->results[n].score;;
86 83
 }
87 84
 
88 85
 void choices_prev(choices_t *c){
Browse code

Abort on allocation errors

John Hawthorn authored on 17/09/2014 02:05:26
Showing 1 changed files
... ...
@@ -1,5 +1,6 @@
1 1
 #define _GNU_SOURCE
2 2
 #include <stdlib.h>
3
+#include <stdio.h>
3 4
 
4 5
 #include "choices.h"
5 6
 #include "match.h"
... ...
@@ -23,6 +24,11 @@ static void choices_resize(choices_t *c, int new_capacity){
23 24
 	c->scores  = realloc(c->scores,  new_capacity * sizeof(double));
24 25
 	c->sorted  = realloc(c->sorted,  new_capacity * sizeof(size_t));
25 26
 
27
+	if(!c->strings || !c->scores || !c->sorted){
28
+		fprintf(stderr, "Error: Can't allocate memory\n");
29
+		abort();
30
+	}
31
+
26 32
 	for(int i = c->capacity; i < new_capacity; i++){
27 33
 		c->strings[i] = NULL;
28 34
 	}
Browse code

Fix divide by zero on empty choices list

John Hawthorn authored on 15/09/2014 03:26:09
Showing 1 changed files
... ...
@@ -80,10 +80,12 @@ double choices_getscore(choices_t *c, size_t n){
80 80
 }
81 81
 
82 82
 void choices_prev(choices_t *c){
83
-	c->selection = (c->selection + c->available - 1) % c->available;
83
+	if(c->available)
84
+		c->selection = (c->selection + c->available - 1) % c->available;
84 85
 }
85 86
 
86 87
 void choices_next(choices_t *c){
87
-	c->selection = (c->selection + 1) % c->available;
88
+	if(c->available)
89
+		c->selection = (c->selection + 1) % c->available;
88 90
 }
89 91
 
Browse code

Cleanup headers

John Hawthorn authored on 15/09/2014 02:42:43
Showing 1 changed files
... ...
@@ -2,7 +2,7 @@
2 2
 #include <stdlib.h>
3 3
 
4 4
 #include "choices.h"
5
-#include "fzy.h"
5
+#include "match.h"
6 6
 
7 7
 #define INITIAL_CAPACITY 1
8 8
 
Browse code

Refactor into choices.c

John Hawthorn authored on 14/09/2014 05:19:52
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,89 @@
1
+#define _GNU_SOURCE
2
+#include <stdlib.h>
3
+
4
+#include "choices.h"
5
+#include "fzy.h"
6
+
7
+#define INITIAL_CAPACITY 1
8
+
9
+static int cmpchoice(size_t *idx1, size_t *idx2, double *choices_score) {
10
+	double score1 = choices_score[*idx1];
11
+	double score2 = choices_score[*idx2];
12
+
13
+	if(score1 == score2)
14
+		return 0;
15
+	else if(score1 < score2)
16
+		return 1;
17
+	else
18
+		return -1;
19
+}
20
+
21
+static void choices_resize(choices_t *c, int new_capacity){
22
+	c->strings = realloc(c->strings, new_capacity * sizeof(const char *));
23
+	c->scores  = realloc(c->scores,  new_capacity * sizeof(double));
24
+	c->sorted  = realloc(c->sorted,  new_capacity * sizeof(size_t));
25
+
26
+	for(int i = c->capacity; i < new_capacity; i++){
27
+		c->strings[i] = NULL;
28
+	}
29
+	c->capacity = new_capacity;
30
+}
31
+
32
+void choices_init(choices_t *c){
33
+	c->strings = NULL;
34
+	c->scores  = NULL;
35
+	c->sorted  = NULL;
36
+	c->capacity = c->size = 0;
37
+	c->selection = c->available = 0;
38
+	choices_resize(c, INITIAL_CAPACITY);
39
+}
40
+void choices_free(choices_t *c){
41
+	free(c->strings);
42
+	free(c->scores);
43
+	free(c->sorted);
44
+};
45
+
46
+void choices_add(choices_t *c, const char *choice){
47
+	if(c->size == c->capacity){
48
+		choices_resize(c, c->capacity * 2);
49
+	}
50
+	c->strings[c->size++] = choice;
51
+}
52
+
53
+size_t choices_available(choices_t *c){
54
+	return c->available;
55
+}
56
+
57
+void choices_search(choices_t *c, const char *search){
58
+	c->selection = 0;
59
+	c->available = 0;
60
+
61
+	for(size_t i = 0; i < c->size; i++){
62
+		if(has_match(search, c->strings[i])){
63
+			c->scores[i] = match(search, c->strings[i]);
64
+			c->sorted[c->available++] = i;
65
+		}
66
+	}
67
+
68
+	qsort_r(c->sorted, c->available, sizeof(size_t), (int (*)(const void *, const void *, void *))cmpchoice, c->scores);
69
+}
70
+
71
+const char *choices_get(choices_t *c, size_t n){
72
+	if(n < c->available){
73
+		return c->strings[c->sorted[n]];
74
+	}else{
75
+		return NULL;
76
+	}
77
+}
78
+double choices_getscore(choices_t *c, size_t n){
79
+	return c->scores[c->sorted[n]];;
80
+}
81
+
82
+void choices_prev(choices_t *c){
83
+	c->selection = (c->selection + c->available - 1) % c->available;
84
+}
85
+
86
+void choices_next(choices_t *c){
87
+	c->selection = (c->selection + 1) % c->available;
88
+}
89
+