File tree 4 files changed +142
-0
lines changed
4 files changed +142
-0
lines changed Original file line number Diff line number Diff line change
1
+ cmake_minimum_required (VERSION 3.12)
2
+ project (c_hashmap C)
3
+
4
+ set (CMAKE_C_STANDARD 99)
5
+
6
+ add_executable (c_hashmap main.c eprintf.c eprintf.h)
Original file line number Diff line number Diff line change
1
+ #include "eprintf.h"
2
+
3
+ /* eprintf: выводит сообщение об ошибке и выходит */
4
+ void eprintf (char * fmt , ...) {
5
+ va_list args ;
6
+
7
+ fflush (stdout );
8
+ if (getprogname () != NULL ) {
9
+ fprintf (stderr , "%s: " , getprogname ());
10
+ }
11
+
12
+ va_start (args , fmt );
13
+ vfprintf (stderr , fmt , args );
14
+ va_end (args );
15
+
16
+ if (fmt [0 ] != '\0' && fmt [strlen (fmt ) - 1 ] == ':' ) {
17
+ fprintf (stderr , " %s" , strerror (errno ));
18
+ }
19
+
20
+ fprintf (stderr , "\n" );
21
+ exit (2 ); /* условный код аварийного выхода */
22
+ }
23
+
24
+ /* emalloc: вызывает malloc, сообщает об ошибке */
25
+ void * emalloc (size_t n ) {
26
+ void * p ;
27
+
28
+ p = malloc (n );
29
+ if (p == NULL ) {
30
+ eprintf ("malloc of %u bytes failed:" , n );
31
+ }
32
+
33
+ return p ;
34
+ }
35
+
36
+ /* weprintf: выводит сообщение об ошибке */
37
+ void weprintf (char * fmt , ...) {
38
+ va_list args ;
39
+
40
+ fflush (stdout );
41
+ if (getprogname () != NULL ) {
42
+ fprintf (stderr , "%s: " , getprogname ());
43
+ }
44
+
45
+ va_start (args , fmt );
46
+ vfprintf (stderr , fmt , args );
47
+ va_end (args );
48
+
49
+ if (fmt [0 ] != '\0' && fmt [strlen (fmt ) - 1 ] == ':' ) {
50
+ fprintf (stderr , " %s" , strerror (errno ));
51
+ }
52
+
53
+ fprintf (stderr , "\n" );
54
+ }
Original file line number Diff line number Diff line change
1
+ //
2
+ // Created by Dmitriy Smotrov on 09/08/2018.
3
+ //
4
+
5
+ #ifndef C_BTREE_EPRINTF_H
6
+ #define C_BTREE_EPRINTF_H
7
+
8
+ #include <zconf.h>
9
+ #include <stdlib.h>
10
+ #include <stdio.h>
11
+ #include <memory.h>
12
+ #include <errno.h>
13
+
14
+ void eprintf (char * fmt , ...);
15
+ void * emalloc (size_t n );
16
+ void weprintf (char * fmt , ...);
17
+
18
+ #endif //C_BTREE_EPRINTF_H
Original file line number Diff line number Diff line change
1
+ #include <stdio.h>
2
+ #include <memory.h>
3
+ #include "eprintf.h"
4
+
5
+ typedef struct Nameval Nameval ;
6
+ struct Nameval {
7
+ char * name ;
8
+ int value ;
9
+ Nameval * next ; /* следующий в цепочке */
10
+ };
11
+
12
+ const int NHASH = 11 ;
13
+
14
+ Nameval * symtab [NHASH ]; /* таблица символов */
15
+
16
+ enum {
17
+ MULTIPLIER = 31
18
+ };
19
+
20
+ unsigned int hash (char * str ) {
21
+ unsigned int h ;
22
+ unsigned char * p ;
23
+
24
+ h = 0 ;
25
+ for (p = (unsigned char * ) str ; * p != '\0' ; p ++ ) {
26
+ h = MULTIPLIER * h + * p ;
27
+ }
28
+
29
+ return h % NHASH ;
30
+ }
31
+
32
+ Nameval * lookup (char * name , int create , int value ) {
33
+ int h ;
34
+ Nameval * sym ;
35
+
36
+ h = hash (name );
37
+ for (sym = symtab [h ]; sym != NULL ; sym = sym -> next ) {
38
+ if (strcmp (name , sym -> name ) == 0 ) {
39
+ return sym ;
40
+ }
41
+ }
42
+ if (create ) {
43
+ sym = (Nameval * ) emalloc (sizeof (Nameval ));
44
+ sym -> name = name ; /* размещается в другом месте */
45
+ sym -> value = value ;
46
+ sym -> next = symtab [h ];
47
+ symtab [h ] = sym ;
48
+ }
49
+
50
+ return sym ;
51
+ }
52
+
53
+ int main () {
54
+ lookup ("AElig" , 1 , 0x00c );
55
+ lookup ("zeta" , 1 , 0x03b6 );
56
+ lookup ("Acicrc" , 1 , 0x00c2 );
57
+ lookup ("AAcute" , 1 , 0x00c1 );
58
+
59
+ Nameval * zeta = lookup ("zeta" , 0 , 0 );
60
+ printf ("\nlookup: %s %x" , zeta -> name , zeta -> value );
61
+
62
+ return 0 ;
63
+ }
64
+
You can’t perform that action at this time.
0 commit comments