Polishing, Apache license headers, history from file loading removal, radix sort optimization impl started.

This commit is contained in:
Martin Dvorak 2013-12-15 11:21:37 +01:00
parent 98d93e846e
commit 35513e47d0
12 changed files with 131 additions and 101 deletions

View file

@ -41,8 +41,3 @@ BUGS
----
https://github.com/dvorka/hstr/issues
AUTHOR
------
martin.dvorak@mindforger.com

View file

@ -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 ) {

View file

@ -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 ) {

View file

@ -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();

View file

@ -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

View file

@ -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"

View file

@ -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_

View file

@ -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

View file

@ -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 <unistd.h>
#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();

View file

@ -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

View file

@ -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);

View file

@ -1,3 +1,12 @@
/*
============================================================================
Name : radixsort.c
Author : martin.dvorak@midforger.com
Copyright : Apache 2.0
Description : Radix sort
============================================================================
*/
#include "include/radixsort.h"
#include <stdlib.h>
#include <stdio.h>
@ -5,16 +14,28 @@
#include <string.h>
#include <stddef.h>
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;