diff --git a/src/hstr.c b/src/hstr.c index ee0a8ea..c1beee4 100644 --- a/src/hstr.c +++ b/src/hstr.c @@ -52,7 +52,7 @@ #endif static char **selection=NULL; -static int selectionSize=0; +static unsigned selectionSize=0; static bool terminalHasColors=FALSE; @@ -80,7 +80,7 @@ void print_history_label(WINDOW *win) { strcpy(message, LABEL_HISTORY); width -= strlen(LABEL_HISTORY); - int i; + unsigned i; for (i=0; i < width; i++) { strcat(message, " "); } @@ -92,12 +92,12 @@ void print_history_label(WINDOW *win) { refresh(); } -int get_max_history_items(WINDOW *win) { +unsigned get_max_history_items(WINDOW *win) { return (getmaxy(win)-(Y_OFFSET_ITEMS+2)); } -void alloc_selection(int size) { +void alloc_selection(unsigned size) { selectionSize=size; if(selection!=NULL) { free(selection); @@ -108,9 +108,9 @@ void alloc_selection(int size) { } } -int make_selection(char *prefix, HistoryItems *history, int maxSelectionCount) { +unsigned make_selection(char *prefix, HistoryItems *history, int maxSelectionCount) { alloc_selection(sizeof(char*) * maxSelectionCount); // TODO realloc - int i, selectionCount=0; + unsigned i, selectionCount=0; HashSet set; hashset_init(&set); @@ -145,15 +145,15 @@ int make_selection(char *prefix, HistoryItems *history, int maxSelectionCount) { return selectionCount; } -char *print_selection(WINDOW *win, int maxHistoryItems, char *prefix, HistoryItems *history) { +char *print_selection(WINDOW *win, unsigned maxHistoryItems, char *prefix, HistoryItems *history) { char *result=""; - int selectionCount=make_selection(prefix, history, maxHistoryItems); + unsigned selectionCount=make_selection(prefix, history, maxHistoryItems); if (selectionCount > 0) { result = selection[0]; } int height=get_max_history_items(win); - int i; + unsigned i; int y=Y_OFFSET_ITEMS; move(Y_OFFSET_ITEMS, 0); @@ -245,7 +245,6 @@ char *selection_loop(HistoryItems *history) { break; case 91: // TODO 91 killed > debug to determine how to distinguish \e and [ - //mvprintw(Y_OFFSET_HELP, 0, "91 killed"); break; case KEY_BACKSPACE: case 127: diff --git a/src/hstr_history.c b/src/hstr_history.c index 7253c22..c6baa03 100644 --- a/src/hstr_history.c +++ b/src/hstr_history.c @@ -1,4 +1,12 @@ #include "include/hstr_history.h" +#include "include/hashset.h" +#include "include/hashmap.h" +#include "include/radixsort.h" + +typedef struct { + char *item; + unsigned rank; +} RankedHistoryItem; static HistoryItems *history; static HistoryItems *prioritizedHistory; @@ -13,16 +21,90 @@ char *get_history_file() { return fileName; } +#define history_ranking_function(RANK, NEWORDEROCCURENCE) (RANK?RANK+NEWORDEROCCURENCE:NEWORDEROCCURENCE) + +void dump_prioritized_history(HistoryItems *ph) { + printf("\n\nPrioritized history:"); + int i; + for(i=0; icount; i++) { + if(ph->items[i]!=NULL) { + printf("\n%s",ph->items[i]); fflush(stdout); + } else { + printf("\n %d NULL",i); fflush(stdout); + } + } + printf("\n"); fflush(stdout); +} + HistoryItems *prioritize_history(HistoryItems *historyFileItems) { - return historyFileItems; + HashMap rankmap; + hashmap_init(&rankmap); + + HashSet blacklist; + hashset_init(&blacklist); + hashset_add(&blacklist, "ls"); + hashset_add(&blacklist, "pwd"); + hashset_add(&blacklist, "cd"); + hashset_add(&blacklist, "hh"); + + RadixSorter rs; + radixsort_init(&rs); + + RankedHistoryItem *r; + RadixItem *radixItem; + int i; + for(i=0; icount; i++) { + if(hashset_contains(&blacklist, historyFileItems->items[i])) { + continue; + } + if((r=hashmap_get(&rankmap, historyFileItems->items[i]))==NULL) { + r=(RankedHistoryItem *)malloc(sizeof(RankedHistoryItem)); + r->rank=history_ranking_function(0, i); + r->item=historyFileItems->items[i]; + + hashmap_put(&rankmap, historyFileItems->items[i], r); + + radixItem=(RadixItem *)malloc(sizeof(RadixItem)); + radixItem->key=r->rank; + radixItem->data=r; + radixItem->next=NULL; + radixsort_add(&rs, radixItem); + } else { + //printf("\n>>> %s ", r->item); fflush(stdout); + radixItem=radix_cut(&rs, r->rank, r); + + if(radixItem!=NULL) { + r->rank=history_ranking_function(r->rank, i); + radixItem->key=r->rank; + radixsort_add(&rs, radixItem); + } // TODO else assert + } + } + + RadixItem **prioritizedRadix=radixsort_dump(&rs); + prioritizedHistory=(HistoryItems *)malloc(sizeof(HistoryItems)); + prioritizedHistory->count=rs.size; + prioritizedHistory->items=malloc(rs.size * sizeof(char*)); + for(i=0; idata); + if(prioritizedRadix[i]->data) { + prioritizedHistory->items[i]=((RankedHistoryItem *)(prioritizedRadix[i]->data))->item; + } + printf("\n %d %s ",i,((RankedHistoryItem *)(prioritizedRadix[i]->data))->item); + } + + radixsort_destroy(&rs); + + return prioritizedHistory; } void free_prioritized_history() { - //free(prioritizedHistory->items); - //free(prioritizedHistory); + // TODO free(prioritizedHistory->items); + // TODO free(prioritizedHistory); } + #ifdef GET_HISTORY_FROM_FILE static char *historyAsString; diff --git a/src/hstr_utils.c b/src/hstr_utils.c index 527dbbe..650f161 100644 --- a/src/hstr_utils.c +++ b/src/hstr_utils.c @@ -12,7 +12,7 @@ void tiocsti() { void fill_terminal_input(char *cmd){ size_t size = strlen(cmd); - int i; + unsigned i; char *c; for (i = 0; i < size; i++) { // terminal I/O control, simulate terminal input @@ -22,9 +22,9 @@ void fill_terminal_input(char *cmd){ printf("\n"); } -void reverse_char_pointer_array(char **array, int length) { - int i; +void reverse_char_pointer_array(char **array, unsigned length) { char *temp; + unsigned i; for (i=0; i