Browse code

Store string in result instead of position

John Hawthorn authored on 21/09/2014 21:40:36
Showing 2 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
 	}
... ...
@@ -1,9 +1,9 @@
1 1
 #ifndef CHOICES_H
2 2
 #define CHOICES_H CHOICES_H
3 3
 
4
-struct scored_position {
5
-	size_t position;
4
+struct scored_result {
6 5
 	double score;
6
+	const char *str;
7 7
 };
8 8
 
9 9
 typedef struct {
... ...
@@ -11,7 +11,7 @@ typedef struct {
11 11
 	size_t size;
12 12
 
13 13
 	const char **strings;
14
-	struct scored_position *results;
14
+	struct scored_result *results;
15 15
 
16 16
 	size_t available;
17 17
 	size_t selection;