xaizek / hstr (License: Apachev2) (since 2018-12-07)
Bash and Zsh shell history suggest box - easily view, navigate, search and manage your command history.
Commit 692bf121b8f14f445ba2421bf34c76dbd7ff9706

Improving hash function (Kernighan > Bergstain + XOR) and merging hash map & set (as it was set @ objects).
Author: Martin Dvorak
Author date (UTC): 2013-12-23 20:10
Committer name: Martin Dvorak
Committer date (UTC): 2013-12-23 20:10
Parent(s): cd52088c6f64f1c67db092d45c657f1a93c0bf76
Signing key:
Tree: c2ca4fa793e7085607c978d188ebc8efa678be56
File Lines added Lines deleted
src/hashmap.c 0 76
src/hashset.c 29 44
src/hstr_history.c 4 5
src/include/hashmap.h 0 36
src/include/hashset.h 19 13
src/include/radixsort.h 1 1
File src/hashmap.c deleted (index b90016e..0000000)
1 /*
2 ============================================================================
3 Name : hashmap.c
4 Author : martin.dvorak@midforger.com
5 Copyright : Apache 2.0
6 Description : Hash map
7 ============================================================================
8 */
9
10 #include "include/hashmap.h"
11
12 unsigned int hashmap_hash( const char *str ) {
13 int i;
14 unsigned int result = 0;
15
16 for( i = 0; str[ i ] != '\0'; i++ )
17 result = result * 31 + str[ i ];
18
19 return result % HASH_MAP_SIZE;
20 }
21
22 void hashmap_init( HashMap * hs ) {
23 int i;
24 hs->currentSize = 0;
25 for( i = 0; i < HASH_MAP_SIZE; i++ ) {
26 hs->lists[ i ] = NULL;
27 }
28 }
29
30 void *hashmap_get( const HashMap * hs, const char *key ) {
31 int listNum = hashmap_hash( key );
32 struct HashMapNode *ptr = hs->lists[ listNum ];
33
34 while( ptr != NULL && strcmp( ptr->key, key ) != 0 )
35 ptr = ptr->next;
36
37 return (ptr != NULL? ptr->value : NULL);
38 }
39
40 int hashmap_put( HashMap * hs, const char *key, void *value) {
41 struct HashMapNode *newNode;
42 int listNum;
43
44 if( hashmap_get( hs, key ) )
45 return 0;
46
47 listNum = hashmap_hash( key );
48
49
50 newNode = (struct HashMapNode *) malloc( sizeof ( struct HashMapNode ) );
51 if( newNode == NULL ) {
52 fprintf( stderr, "Error allocating node" );
53 return 0;
54 }
55
56 newNode->key = strdup( key );
57 newNode->value = value;
58 newNode->next = hs->lists[ listNum ];
59 hs->lists[ listNum ] = newNode;
60 hs->currentSize++;
61
62 return 1;
63 }
64
65 int hashmap_size(const HashMap * hs) {
66 return hs->currentSize;
67 }
68
69 void hashmap_print( const HashMap * hs ) {
70 int i;
71 struct HashMapNode *ptr;
72
73 for( i = 0; i < HASH_MAP_SIZE; i++ )
74 for( ptr = hs->lists[ i ]; ptr != NULL; ptr = ptr->next )
75 printf( "%s\n", ptr->key );
76 }
File src/hashset.c changed (mode: 100644) (index 1838543..d5c0891)
1 1 /* /*
2 2 ============================================================================ ============================================================================
3 Name : hashset.h
3 Name : hashset.c
4 4 Author : martin.dvorak@midforger.com Author : martin.dvorak@midforger.com
5 5 Copyright : Apache 2.0 Copyright : Apache 2.0
6 6 Description : Hash set Description : Hash set
 
9 9
10 10 #include "include/hashset.h" #include "include/hashset.h"
11 11
12 unsigned int hash( const char *str ) {
12 unsigned int hashmap_hash(const char *str) {
13 13 int i; int i;
14 unsigned int result = 0;
14 unsigned int result = 5381;
15 15
16 16 for( i = 0; str[ i ] != '\0'; i++ ) for( i = 0; str[ i ] != '\0'; i++ )
17 result = result * 31 + str[ i ];
17 result = result * 33 + str[ i ];
18 18
19 return result % TABLE_SIZE;
19 result = result ^ (result >> 16);
20
21 return result % HASH_MAP_SIZE;
20 22 } }
21 23
22 void hashset_init( HashSet * hs ) {
24 void hashset_init(HashSet * hs) {
23 25 int i; int i;
24 26 hs->currentSize = 0; hs->currentSize = 0;
25 for( i = 0; i < TABLE_SIZE; i++ )
27 for( i = 0; i < HASH_MAP_SIZE; i++ ) {
26 28 hs->lists[ i ] = NULL; hs->lists[ i ] = NULL;
29 }
27 30 } }
28 31
29 int hashset_contains( const HashSet * hs, const char *key ) {
30 int listNum = hash( key );
31 struct HashNode *ptr = hs->lists[ listNum ];
32 void *hashset_get(const HashSet * hs, const char *key) {
33 int listNum = hashmap_hash( key );
34 struct HashSetNode *ptr = hs->lists[ listNum ];
32 35
33 36 while( ptr != NULL && strcmp( ptr->key, key ) != 0 ) while( ptr != NULL && strcmp( ptr->key, key ) != 0 )
34 37 ptr = ptr->next; ptr = ptr->next;
35 38
36 return ptr != NULL;
39 return (ptr != NULL? ptr->value : NULL);
40 }
41
42 int hashset_contains(const HashSet * hs, const char *key) {
43 return (hashset_get(hs, key) != NULL);
37 44 } }
38 45
39 int hashset_add( HashSet * hs, const char *key ) {
40 struct HashNode *newNode;
46 int hashset_put(HashSet * hs, const char *key, void *value) {
47 struct HashSetNode *newNode;
41 48 int listNum; int listNum;
42 49
43 if( hashset_contains( hs, key ) )
50 if( hashset_get( hs, key ) )
44 51 return 0; return 0;
45 52
46 listNum = hash( key );
53 listNum = hashmap_hash( key );
47 54
48 55
49 newNode = (struct HashNode *) malloc( sizeof ( struct HashNode ) );
56 newNode = (struct HashSetNode *) malloc( sizeof ( struct HashSetNode ) );
50 57 if( newNode == NULL ) { if( newNode == NULL ) {
51 58 fprintf( stderr, "Error allocating node" ); fprintf( stderr, "Error allocating node" );
52 59 return 0; return 0;
53 60 } }
54 61
55 62 newNode->key = strdup( key ); newNode->key = strdup( key );
63 newNode->value = value;
56 64 newNode->next = hs->lists[ listNum ]; newNode->next = hs->lists[ listNum ];
57 65 hs->lists[ listNum ] = newNode; hs->lists[ listNum ] = newNode;
58 66 hs->currentSize++; hs->currentSize++;
 
... ... int hashset_add( HashSet * hs, const char *key ) {
60 68 return 1; return 1;
61 69 } }
62 70
63 int hashset_remove( HashSet * hs, const char *key ) {
64 struct HashNode *curr;
65 struct HashNode *prev = NULL;
66 int listNum;
67
68 if( !hashset_contains( hs, key ) )
69 return 0;
70
71 listNum = hash( key );
72 curr = hs->lists[ listNum ];
73 while( strcmp( curr->key, key ) != 0 ) {
74 prev = curr;
75 curr = curr->next;
76 }
77
78 if( prev == NULL )
79 hs->lists[ listNum ] = curr->next;
80 else
81 prev->next = curr->next;
82
83 free( curr->key );
84 free( curr );
85
86 hs->currentSize--;
87 return 1;
71 int hashset_add(HashSet * hs, const char *key) {
72 return hashset_put(hs, key, NULL);
88 73 } }
89 74
90 75 int hashset_size(const HashSet * hs) { int hashset_size(const HashSet * hs) {
91 76 return hs->currentSize; return hs->currentSize;
92 77 } }
93 78
94 void hashset_print( const HashSet * hs ) {
79 void hashset_stat( const HashSet * hs ) {
95 80 int i; int i;
96 struct HashNode *ptr;
81 struct HashSetNode *ptr;
97 82
98 for( i = 0; i < TABLE_SIZE; i++ )
83 for( i = 0; i < HASH_MAP_SIZE; i++ )
99 84 for( ptr = hs->lists[ i ]; ptr != NULL; ptr = ptr->next ) for( ptr = hs->lists[ i ]; ptr != NULL; ptr = ptr->next )
100 85 printf( "%s\n", ptr->key ); printf( "%s\n", ptr->key );
101 86 } }
File src/hstr_history.c changed (mode: 100644) (index 1a18fcb..b3b6978)
9 9
10 10 #include "include/hstr_history.h" #include "include/hstr_history.h"
11 11 #include "include/hashset.h" #include "include/hashset.h"
12 #include "include/hashmap.h"
13 12 #include "include/radixsort.h" #include "include/radixsort.h"
14 13
15 14 #include "include/hstr_utils.h" #include "include/hstr_utils.h"
 
... ... HistoryItems *get_prioritized_history() {
68 67 HISTORY_STATE *historyState=history_get_history_state(); HISTORY_STATE *historyState=history_get_history_state();
69 68
70 69 if(historyState->length > 0) { if(historyState->length > 0) {
71 HashMap rankmap;
72 hashmap_init(&rankmap);
70 HashSet rankmap;
71 hashset_init(&rankmap);
73 72
74 73 HashSet blacklist; HashSet blacklist;
75 74 int i; int i;
 
... ... HistoryItems *get_prioritized_history() {
90 89 if(hashset_contains(&blacklist, line)) { if(hashset_contains(&blacklist, line)) {
91 90 continue; continue;
92 91 } }
93 if((r=hashmap_get(&rankmap, line))==NULL) {
92 if((r=hashset_get(&rankmap, line))==NULL) {
94 93 r=malloc(sizeof(RankedHistoryItem)); r=malloc(sizeof(RankedHistoryItem));
95 94 r->rank=HISTORY_RANKING_FUNCTION(0, i, strlen(line)); r->rank=HISTORY_RANKING_FUNCTION(0, i, strlen(line));
96 95 r->item=historyList[i]->line; r->item=historyList[i]->line;
97 96
98 hashmap_put(&rankmap, line, r);
97 hashset_put(&rankmap, line, r);
99 98
100 99 radixItem=malloc(sizeof(RadixItem)); radixItem=malloc(sizeof(RadixItem));
101 100 radixItem->key=r->rank; radixItem->key=r->rank;
File src/include/hashmap.h deleted (index ee522a6..0000000)
1 /*
2 ============================================================================
3 Name : hashmap.h
4 Author : martin.dvorak@midforger.com
5 Copyright : Apache 2.0
6 Description : Hash map
7 ============================================================================
8 */
9
10 #ifndef _HASHMAP_H_
11 #define _HASHMAP_H_
12
13 #include <stdlib.h>
14 #include <stdio.h>
15 #include <string.h>
16
17 #define HASH_MAP_SIZE 10007
18
19 struct HashMapNode {
20 char *key;
21 void *value;
22 struct HashMapNode *next;
23 };
24
25 typedef struct {
26 struct HashMapNode * lists[HASH_MAP_SIZE];
27 int currentSize;
28 } HashMap;
29
30 void hashmap_init( HashMap *hm );
31 void *hashmap_get( const HashMap *hm, const char *key );
32 int hashmap_put( HashMap *hm, const char *key, void *value );
33 int hashmap_size( const HashMap *hm );
34 void hashmap_print( const HashMap *hm );
35
36 #endif /* HASHMAP_H_ */
File src/include/hashset.h changed (mode: 100644) (index 1299a9f..e357a2f)
7 7 ============================================================================ ============================================================================
8 8 */ */
9 9
10 #ifndef _HASHSET_H
11 #define _HASHSET_H
10 #ifndef _HASHSET_H_
11 #define _HASHSET_H_
12 12
13 13 #include <stdlib.h> #include <stdlib.h>
14 14 #include <stdio.h> #include <stdio.h>
15 15 #include <string.h> #include <string.h>
16 16
17 #define TABLE_SIZE 10007
17 #define HASH_MAP_SIZE 10007
18 18
19 struct HashNode {
19 struct HashSetNode {
20 20 char *key; char *key;
21 struct HashNode *next;
21 void *value;
22 struct HashSetNode *next;
22 23 }; };
23 24
24 25 typedef struct { typedef struct {
25 struct HashNode * lists[TABLE_SIZE];
26 struct HashSetNode * lists[HASH_MAP_SIZE];
26 27 int currentSize; int currentSize;
27 28 } HashSet; } HashSet;
28 29
29 void hashset_init( HashSet *hs );
30 int hashset_contains( const HashSet *hs, const char *key );
31 int hashset_add( HashSet *hs, const char *key );
32 int hashset_remove( HashSet *hs, const char *key );
33 int hashset_size( const HashSet *hs );
34 void hashset_print( const HashSet *hs );
30 void hashset_init(HashSet *hs);
35 31
36 #endif
32 int hashset_contains(const HashSet *hs, const char *key);
33 int hashset_add(HashSet *hs, const char *key);
34 int hashset_size(const HashSet *hs);
35
36 void *hashset_get(const HashSet *hm, const char *key);
37 int hashset_put(HashSet *hm, const char *key, void *value);
38 int hashset_remove(HashSet *hm, const char *key);
39
40 void hashset_stat(const HashSet *hm);
41
42 #endif /* HASHSET_H_ */
File src/include/radixsort.h changed (mode: 100644) (index a5f0958..a9196df)
... ... void radixsort_init(RadixSorter *rs, unsigned keyLimit);
38 38 void radixsort_add(RadixSorter *rs, RadixItem *item); void radixsort_add(RadixSorter *rs, RadixItem *item);
39 39 RadixItem *radix_cut(RadixSorter *rs, unsigned key, void *data); RadixItem *radix_cut(RadixSorter *rs, unsigned key, void *data);
40 40 RadixItem **radixsort_dump(RadixSorter *rs); RadixItem **radixsort_dump(RadixSorter *rs);
41 void radixsort_stat(RadixSorter *rs);
42 41 void radixsort_destroy(RadixSorter *rs); void radixsort_destroy(RadixSorter *rs);
42 void radixsort_stat(RadixSorter *rs);
43 43
44 44 #endif /* RADIXSORT_H_ */ #endif /* RADIXSORT_H_ */
Hints

Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"

Clone this repository using HTTP(S):
git clone https://code.reversed.top/user/xaizek/hstr

Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@code.reversed.top/user/xaizek/hstr

You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a pull request:
... clone the repository ...
... make some changes and some commits ...
git push origin master