mirror of
https://github.com/dvorka/hstr.git
synced 2025-02-24 06:47:16 +08:00
Improved ranking function.
This commit is contained in:
parent
35513e47d0
commit
ed9ab79887
1 changed files with 15 additions and 10 deletions
|
@ -12,6 +12,9 @@
|
||||||
#include "include/hashmap.h"
|
#include "include/hashmap.h"
|
||||||
#include "include/radixsort.h"
|
#include "include/radixsort.h"
|
||||||
|
|
||||||
|
#define NDEBUG
|
||||||
|
#include <assert.h>
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
char *item;
|
char *item;
|
||||||
unsigned rank;
|
unsigned rank;
|
||||||
|
@ -20,18 +23,19 @@ typedef struct {
|
||||||
static HistoryItems *history;
|
static HistoryItems *history;
|
||||||
static HistoryItems *prioritizedHistory;
|
static HistoryItems *prioritizedHistory;
|
||||||
|
|
||||||
|
unsigned history_ranking_function(unsigned rank, unsigned newOccurenceOrder, unsigned lng) {
|
||||||
|
rank+=newOccurenceOrder/10 + lng;
|
||||||
|
return rank;
|
||||||
|
}
|
||||||
|
|
||||||
char *get_history_file() {
|
char *get_history_file() {
|
||||||
char *home = getenv(ENV_VAR_HOME);
|
char *home = getenv(ENV_VAR_HOME);
|
||||||
char *fileName = (char*) malloc(
|
char *fileName = (char*) malloc(
|
||||||
strlen(home) + 1 + strlen(FILE_HISTORY) + 1);
|
strlen(home) + 1 + strlen(FILE_HISTORY) + 1);
|
||||||
strcpy(fileName, home);
|
strcat(strcat(strcpy(fileName, home), "/"), FILE_HISTORY);
|
||||||
strcat(fileName, "/");
|
|
||||||
strcat(fileName, FILE_HISTORY);
|
|
||||||
return fileName;
|
return fileName;
|
||||||
}
|
}
|
||||||
|
|
||||||
#define history_ranking_function(RANK, NEWORDEROCCURENCE) (RANK?RANK+NEWORDEROCCURENCE:NEWORDEROCCURENCE)
|
|
||||||
|
|
||||||
void dump_prioritized_history(HistoryItems *ph) {
|
void dump_prioritized_history(HistoryItems *ph) {
|
||||||
printf("\n\nPrioritized history:");
|
printf("\n\nPrioritized history:");
|
||||||
int i;
|
int i;
|
||||||
|
@ -68,7 +72,7 @@ HistoryItems *prioritize_history(HistoryItems *historyFileItems) {
|
||||||
}
|
}
|
||||||
if((r=hashmap_get(&rankmap, historyFileItems->items[i]))==NULL) {
|
if((r=hashmap_get(&rankmap, historyFileItems->items[i]))==NULL) {
|
||||||
r=(RankedHistoryItem *)malloc(sizeof(RankedHistoryItem));
|
r=(RankedHistoryItem *)malloc(sizeof(RankedHistoryItem));
|
||||||
r->rank=history_ranking_function(0, i);
|
r->rank=history_ranking_function(0, i, strlen(historyFileItems->items[i]));
|
||||||
r->item=historyFileItems->items[i];
|
r->item=historyFileItems->items[i];
|
||||||
|
|
||||||
hashmap_put(&rankmap, historyFileItems->items[i], r);
|
hashmap_put(&rankmap, historyFileItems->items[i], r);
|
||||||
|
@ -79,14 +83,15 @@ HistoryItems *prioritize_history(HistoryItems *historyFileItems) {
|
||||||
radixItem->next=NULL;
|
radixItem->next=NULL;
|
||||||
radixsort_add(&rs, radixItem);
|
radixsort_add(&rs, radixItem);
|
||||||
} else {
|
} else {
|
||||||
//printf("\n>>> %s ", r->item); fflush(stdout);
|
|
||||||
radixItem=radix_cut(&rs, r->rank, r);
|
radixItem=radix_cut(&rs, r->rank, r);
|
||||||
|
|
||||||
if(radixItem!=NULL) {
|
assert(radixItem);
|
||||||
r->rank=history_ranking_function(r->rank, i);
|
|
||||||
|
if(radixItem) {
|
||||||
|
r->rank=history_ranking_function(r->rank, i, strlen(historyFileItems->items[i]));
|
||||||
radixItem->key=r->rank;
|
radixItem->key=r->rank;
|
||||||
radixsort_add(&rs, radixItem);
|
radixsort_add(&rs, radixItem);
|
||||||
} // TODO else assert
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue