From 35513e47d0dd567a824e01d981e2b8b8d7cd5815 Mon Sep 17 00:00:00 2001 From: Martin Dvorak Date: Sun, 15 Dec 2013 11:21:37 +0100 Subject: [PATCH] Polishing, Apache license headers, history from file loading removal, radix sort optimization impl started. --- README.md | 5 -- src/hashmap.c | 9 ++++ src/hashset.c | 9 ++++ src/hstr.c | 6 +-- src/hstr_history.c | 95 ++++++-------------------------------- src/hstr_utils.c | 9 ++++ src/include/hashmap.h | 9 ++++ src/include/hashset.h | 9 ++++ src/include/hstr_history.h | 14 ++++-- src/include/hstr_utils.h | 9 ++++ src/include/radixsort.h | 24 +++++++++- src/radixsort.c | 34 +++++++++++--- 12 files changed, 131 insertions(+), 101 deletions(-) diff --git a/README.md b/README.md index bec98a5..3948310 100644 --- a/README.md +++ b/README.md @@ -41,8 +41,3 @@ BUGS ---- https://github.com/dvorka/hstr/issues - -AUTHOR ------- -martin.dvorak@mindforger.com - diff --git a/src/hashmap.c b/src/hashmap.c index 060751c..b90016e 100644 --- a/src/hashmap.c +++ b/src/hashmap.c @@ -1,3 +1,12 @@ +/* + ============================================================================ + Name : hashmap.c + Author : martin.dvorak@midforger.com + Copyright : Apache 2.0 + Description : Hash map + ============================================================================ +*/ + #include "include/hashmap.h" unsigned int hashmap_hash( const char *str ) { diff --git a/src/hashset.c b/src/hashset.c index 6f174be..1838543 100644 --- a/src/hashset.c +++ b/src/hashset.c @@ -1,3 +1,12 @@ +/* + ============================================================================ + Name : hashset.h + Author : martin.dvorak@midforger.com + Copyright : Apache 2.0 + Description : Hash set + ============================================================================ +*/ + #include "include/hashset.h" unsigned int hash( const char *str ) { diff --git a/src/hstr.c b/src/hstr.c index c1beee4..15ac6f1 100644 --- a/src/hstr.c +++ b/src/hstr.c @@ -1,8 +1,7 @@ /* ============================================================================ Name : hstr.c - Author : Martin Dvorak - Version : 0.2 + Author : martin.dvorak@midforger.com Copyright : Apache 2.0 Description : Shell history completion utility ============================================================================ @@ -325,8 +324,7 @@ char *selection_loop(HistoryItems *history) { } void hstr() { - HistoryItems *historyFileItems=get_history_items(); - HistoryItems *history=prioritize_history(historyFileItems); + HistoryItems *history=get_prioritized_history(); char *command = selection_loop(history); fill_terminal_input(command); free_prioritized_history(); diff --git a/src/hstr_history.c b/src/hstr_history.c index 6393bf3..a4731c5 100644 --- a/src/hstr_history.c +++ b/src/hstr_history.c @@ -1,3 +1,12 @@ +/* + ============================================================================ + Name : hstr_history.h + Author : martin.dvorak@midforger.com + Copyright : Apache 2.0 + Description : Loading and processing of BASH history + ============================================================================ +*/ + #include "include/hstr_history.h" #include "include/hashset.h" #include "include/hashmap.h" @@ -101,86 +110,6 @@ void free_prioritized_history() { // TODO free(prioritizedHistory); } - - -#ifdef GET_HISTORY_FROM_FILE - -static char *historyAsString; - -char *load_history_file() { - char *fileName = get_history_file(); - if(access(fileName, F_OK) != -1) { - char *file_contents; - long input_file_size; - - FILE *input_file = fopen(fileName, "rb"); - fseek(input_file, 0, SEEK_END); - input_file_size = ftell(input_file); - rewind(input_file); - file_contents = malloc((input_file_size + 1) * (sizeof(char))); - if(fread(file_contents, sizeof(char), input_file_size, input_file)==-1) { - exit(EXIT_FAILURE); - } - fclose(input_file); - file_contents[input_file_size] = 0; - - return file_contents; - } else { - fprintf(stderr,"\nHistory file not found: %s\n",fileName); - exit(EXIT_FAILURE); - } -} - -int count_history_lines(char *history) { - int i = 0; - char *p=strchr(history,'\n'); - while (p!=NULL) { - i++; - p=strchr(p+1,'\n'); - } - return i; -} - -char **get_tokenized_history(char *history, int lines) { - char **tokens = malloc(sizeof(char*) * lines); - - int i = 0; - char *pb=history, *pe; - pe=strchr(history, '\n'); - while(pe!=NULL) { - tokens[i]=pb; - *pe=0; - - pb=pe+1; - pe=strchr(pb, '\n'); - i++; - } - - return tokens; -} - -char **get_history_items() { - historyAsString = load_history_file(FILE_HISTORY); - historyItemsCount = count_history_lines(historyAsString); - historyItems = get_tokenized_history(historyAsString, historyItemsCount); - reverse_char_pointer_array(historyItems, historyItemsCount); - return historyItems; -} - -int get_history_items_size() { - return historyItemsCount; -} - -void free_history_items() { - free(historyAsString); - free(historyItems); -} - - - -#else - - void flush_history() { const char *filename = "history"; char *const args[1] = {"-a"}; @@ -235,5 +164,9 @@ void free_history_items() { free(history); } +HistoryItems *get_prioritized_history() { + HistoryItems *fileItems=get_history_items(); + return prioritize_history(fileItems); +} + -#endif diff --git a/src/hstr_utils.c b/src/hstr_utils.c index 650f161..fb1bcb2 100644 --- a/src/hstr_utils.c +++ b/src/hstr_utils.c @@ -1,3 +1,12 @@ +/* + ============================================================================ + Name : hstr_utils.c + Author : martin.dvorak@midforger.com + Copyright : Apache 2.0 + Description : Utilities + ============================================================================ +*/ + #include "include/hstr_utils.h" #define DEFAULT_COMMAND "pwd" diff --git a/src/include/hashmap.h b/src/include/hashmap.h index 28b9eaa..ee522a6 100644 --- a/src/include/hashmap.h +++ b/src/include/hashmap.h @@ -1,3 +1,12 @@ +/* + ============================================================================ + Name : hashmap.h + Author : martin.dvorak@midforger.com + Copyright : Apache 2.0 + Description : Hash map + ============================================================================ +*/ + #ifndef _HASHMAP_H_ #define _HASHMAP_H_ diff --git a/src/include/hashset.h b/src/include/hashset.h index 7df422b..1299a9f 100644 --- a/src/include/hashset.h +++ b/src/include/hashset.h @@ -1,3 +1,12 @@ +/* + ============================================================================ + Name : hashset.h + Author : martin.dvorak@midforger.com + Copyright : Apache 2.0 + Description : Hash set + ============================================================================ +*/ + #ifndef _HASHSET_H #define _HASHSET_H diff --git a/src/include/hstr_history.h b/src/include/hstr_history.h index 617aeb3..198c2c9 100644 --- a/src/include/hstr_history.h +++ b/src/include/hstr_history.h @@ -1,3 +1,12 @@ +/* + ============================================================================ + Name : hstr_history.h + Author : martin.dvorak@midforger.com + Copyright : Apache 2.0 + Description : Loading and processing of BASH history + ============================================================================ +*/ + #ifndef _HSTR_HISTORY_H_ #define _HSTR_HISTORY_H_ @@ -9,9 +18,6 @@ #include #include "hstr_utils.h" -//#define GET_HISTORY_FROM_FILE -#define GET_HISTORY_USING_LIBRARY - #define ENV_VAR_USER "USER" #define ENV_VAR_HOME "HOME" #define ENV_VAR_HISTFILE "HISTFILE" @@ -23,6 +29,8 @@ typedef struct { unsigned count; } HistoryItems; +HistoryItems *get_prioritized_history(); + HistoryItems *get_history_items(); void free_history_items(); diff --git a/src/include/hstr_utils.h b/src/include/hstr_utils.h index 2828c79..451f747 100644 --- a/src/include/hstr_utils.h +++ b/src/include/hstr_utils.h @@ -1,3 +1,12 @@ +/* + ============================================================================ + Name : hstr_utils.h + Author : martin.dvorak@midforger.com + Copyright : Apache 2.0 + Description : Utilities + ============================================================================ +*/ + #ifndef _HSTR_UTILS_H #define _HSTR_UTILS_H diff --git a/src/include/radixsort.h b/src/include/radixsort.h index 25a1088..b290762 100644 --- a/src/include/radixsort.h +++ b/src/include/radixsort.h @@ -1,18 +1,38 @@ +/* + ============================================================================ + Name : radixsort.h + Author : martin.dvorak@midforger.com + Copyright : Apache 2.0 + Description : Radix sort + ============================================================================ +*/ + #ifndef RADIXSORT_H_ #define RADIXSORT_H_ +#define SIX2FOUR_SIZE 1000 + typedef struct radixitem { unsigned key; void *data; struct radixitem *next; } RadixItem; -#define SIX2FOUR_SIZE 1000 +typedef struct radixslot { + unsigned key; + unsigned min; + unsigned max; + struct radixslot *next; +} RadixSlot; typedef struct { unsigned size; - unsigned maxValue; + unsigned maxKey; RadixItem **six2four[SIX2FOUR_SIZE]; + + unsigned _keyLimit; + RadixSlot *_slots; + unsigned _slotsCount; } RadixSorter; void radixsort_init(RadixSorter *rs); diff --git a/src/radixsort.c b/src/radixsort.c index e64d060..5fa94a8 100644 --- a/src/radixsort.c +++ b/src/radixsort.c @@ -1,3 +1,12 @@ +/* + ============================================================================ + Name : radixsort.c + Author : martin.dvorak@midforger.com + Copyright : Apache 2.0 + Description : Radix sort + ============================================================================ +*/ + #include "include/radixsort.h" #include #include @@ -5,16 +14,28 @@ #include #include -RadixItem **radixsort_get_slot() { +RadixItem **radixsort_get_slot(RadixSorter *rs, unsigned key) { RadixItem **slot=malloc(SIX2FOUR_SIZE * sizeof(RadixItem*)); memset(slot, 0, SIX2FOUR_SIZE * sizeof(RadixItem*)); + RadixSlot *slotDescriptor=malloc(sizeof(RadixSlot)); + slotDescriptor->key=key; + slotDescriptor->min=0; + slotDescriptor->max=SIX2FOUR_SIZE; + if(rs->_slots) { + slotDescriptor->next=rs->_slots->next; + } + rs->_slots=slotDescriptor; + rs->_slotsCount++; return slot; } void radixsort_init(RadixSorter *rs) { rs->size=0; memset(rs->six2four, 0, SIX2FOUR_SIZE * sizeof(RadixItem*)); - rs->maxValue=0; + rs->maxKey=0; + rs->_keyLimit=SIX2FOUR_SIZE*SIX2FOUR_SIZE; + rs->_slots=NULL; + rs->_slotsCount=0; } void radixsort_add(RadixSorter *rs, RadixItem *item) { @@ -23,7 +44,7 @@ void radixsort_add(RadixSorter *rs, RadixItem *item) { unsigned three2zero=item->key-six2four*1000; if(rs->six2four[six2four]==NULL) { - rs->six2four[six2four]=radixsort_get_slot(); + rs->six2four[six2four]=radixsort_get_slot(rs, six2four); } RadixItem *chain=rs->six2four[six2four][three2zero]; @@ -35,18 +56,19 @@ void radixsort_add(RadixSorter *rs, RadixItem *item) { } rs->size++; - rs->maxValue=(rs->maxValue>item->key?rs->maxValue:item->key); + rs->maxKey=(rs->maxKey>item->key?rs->maxKey:item->key); } RadixItem **radixsort_dump(RadixSorter *rs) { if(rs->size>0) { RadixItem **result=malloc(rs->size * sizeof(RadixItem *)); - double d = ((double)rs->maxValue)/1000.0; + double d = ((double)rs->maxKey)/1000.0; int six2four = (int)trunc(d); int s, t, i=0; s=six2four; do { + // TODO optimization: iterate only _slots chain t=SIX2FOUR_SIZE-1; do { if(rs->six2four[s]!=NULL) { @@ -69,7 +91,7 @@ RadixItem **radixsort_dump(RadixSorter *rs) { } RadixItem *radix_cut(RadixSorter *rs, unsigned key, void *data) { - if(key<=rs->maxValue) { + if(key<=rs->maxKey) { double d = ((double) key)/1000.0; unsigned six2four = (unsigned)trunc(d); unsigned three2zero=key-six2four*1000;