File src/hstr_history.c changed (mode: 100644) (index a4731c5..cde4a97) |
12 |
12 |
#include "include/hashmap.h" |
#include "include/hashmap.h" |
13 |
13 |
#include "include/radixsort.h" |
#include "include/radixsort.h" |
14 |
14 |
|
|
|
15 |
|
#define NDEBUG |
|
16 |
|
#include <assert.h> |
|
17 |
|
|
15 |
18 |
typedef struct { |
typedef struct { |
16 |
19 |
char *item; |
char *item; |
17 |
20 |
unsigned rank; |
unsigned rank; |
|
... |
... |
typedef struct { |
20 |
23 |
static HistoryItems *history; |
static HistoryItems *history; |
21 |
24 |
static HistoryItems *prioritizedHistory; |
static HistoryItems *prioritizedHistory; |
22 |
25 |
|
|
|
26 |
|
unsigned history_ranking_function(unsigned rank, unsigned newOccurenceOrder, unsigned lng) { |
|
27 |
|
rank+=newOccurenceOrder/10 + lng; |
|
28 |
|
return rank; |
|
29 |
|
} |
|
30 |
|
|
23 |
31 |
char *get_history_file() { |
char *get_history_file() { |
24 |
32 |
char *home = getenv(ENV_VAR_HOME); |
char *home = getenv(ENV_VAR_HOME); |
25 |
33 |
char *fileName = (char*) malloc( |
char *fileName = (char*) malloc( |
26 |
34 |
strlen(home) + 1 + strlen(FILE_HISTORY) + 1); |
strlen(home) + 1 + strlen(FILE_HISTORY) + 1); |
27 |
|
strcpy(fileName, home); |
|
28 |
|
strcat(fileName, "/"); |
|
29 |
|
strcat(fileName, FILE_HISTORY); |
|
|
35 |
|
strcat(strcat(strcpy(fileName, home), "/"), FILE_HISTORY); |
30 |
36 |
return fileName; |
return fileName; |
31 |
37 |
} |
} |
32 |
38 |
|
|
33 |
|
#define history_ranking_function(RANK, NEWORDEROCCURENCE) (RANK?RANK+NEWORDEROCCURENCE:NEWORDEROCCURENCE) |
|
34 |
|
|
|
35 |
39 |
void dump_prioritized_history(HistoryItems *ph) { |
void dump_prioritized_history(HistoryItems *ph) { |
36 |
40 |
printf("\n\nPrioritized history:"); |
printf("\n\nPrioritized history:"); |
37 |
41 |
int i; |
int i; |
|
... |
... |
HistoryItems *prioritize_history(HistoryItems *historyFileItems) { |
68 |
72 |
} |
} |
69 |
73 |
if((r=hashmap_get(&rankmap, historyFileItems->items[i]))==NULL) { |
if((r=hashmap_get(&rankmap, historyFileItems->items[i]))==NULL) { |
70 |
74 |
r=(RankedHistoryItem *)malloc(sizeof(RankedHistoryItem)); |
r=(RankedHistoryItem *)malloc(sizeof(RankedHistoryItem)); |
71 |
|
r->rank=history_ranking_function(0, i); |
|
|
75 |
|
r->rank=history_ranking_function(0, i, strlen(historyFileItems->items[i])); |
72 |
76 |
r->item=historyFileItems->items[i]; |
r->item=historyFileItems->items[i]; |
73 |
77 |
|
|
74 |
78 |
hashmap_put(&rankmap, historyFileItems->items[i], r); |
hashmap_put(&rankmap, historyFileItems->items[i], r); |
|
... |
... |
HistoryItems *prioritize_history(HistoryItems *historyFileItems) { |
79 |
83 |
radixItem->next=NULL; |
radixItem->next=NULL; |
80 |
84 |
radixsort_add(&rs, radixItem); |
radixsort_add(&rs, radixItem); |
81 |
85 |
} else { |
} else { |
82 |
|
//printf("\n>>> %s ", r->item); fflush(stdout); |
|
83 |
86 |
radixItem=radix_cut(&rs, r->rank, r); |
radixItem=radix_cut(&rs, r->rank, r); |
84 |
87 |
|
|
85 |
|
if(radixItem!=NULL) { |
|
86 |
|
r->rank=history_ranking_function(r->rank, i); |
|
|
88 |
|
assert(radixItem); |
|
89 |
|
|
|
90 |
|
if(radixItem) { |
|
91 |
|
r->rank=history_ranking_function(r->rank, i, strlen(historyFileItems->items[i])); |
87 |
92 |
radixItem->key=r->rank; |
radixItem->key=r->rank; |
88 |
93 |
radixsort_add(&rs, radixItem); |
radixsort_add(&rs, radixItem); |
89 |
|
} // TODO else assert |
|
|
94 |
|
} |
90 |
95 |
} |
} |
91 |
96 |
} |
} |
92 |
97 |
|
|