libhash_ex

This commit is contained in:
Kolan Sh 2011-05-19 18:55:40 +04:00
parent e74c9a80ae
commit 5162d972fb
6 changed files with 233 additions and 0 deletions

7
.hgignore Normal file
View File

@ -0,0 +1,7 @@
syntax: glob
*.o
*.swp
*.gch
*.gcno
*.out
callgrind.out.*

55
c/gprof_ex/collatz.c Normal file
View File

@ -0,0 +1,55 @@
#include <stdio.h>
/* Computes the length of Collatz sequences */
unsigned int
step (unsigned int x)
{
if (x % 2 == 0)
{
return (x / 2);
}
else
{
return (3 * x + 1);
}
}
unsigned int
nseq (unsigned int x0)
{
unsigned int i = 1, x;
if (x0 == 1 || x0 == 0)
return i;
x = step (x0);
while (x != 1 && x != 0)
{
x = step (x);
i++;
}
return i;
}
int
main (void)
{
unsigned int i, m = 0, im = 0;
for (i = 1; i < 500000; i++)
{
unsigned int k = nseq (i);
if (k > m)
{
m = k;
im = i;
printf ("sequence length = %u for %u\n", m, im);
}
}
return 0;
}

83
c/libhash_ex/Makefile Normal file
View File

@ -0,0 +1,83 @@
CC=cc
CXX=c++
INCLUDE=-I$(HOME)/projects/include
KOLAN_PROJECTS=$(HOME)/projects
KOLAN_INCLUDE=$(KOLAN_PROJECTS)/include
# Compiler flags
# if mode variable is empty, setting debug build mode
ifeq ($(mode),)
mode = debug
endif
ifeq ($(mode),debug)
CFLAGS = -O0 -g -std=c99 -pedantic -Wextra -Wconversion
LDFLAGS =
endif
ifeq ($(mode),profile)
CFLAGS = -O0 -g -p -ftest-coverage -Wcoverage-mismatch
LDFLAGS = -g -p
endif
ifeq ($(mode),develop)
CFLAGS += -O2 -g
LDFLAGS += -O1
endif
ifeq ($(mode),release)
CFLAGS += -O2
LDFLAGS += -O1
endif
CFLAGS += -Wall
# $(shell pkg-config --cflags libhash)
LDFLAGS += -Wall -lhash
# $(shell pkg-config --libs libhash)
all: libhash_ex
ifneq ($(mode),debug)
ifneq ($(mode),profile)
ifneq ($(mode),develop)
ifneq ($(mode),release)
@echo "Invalid build mode."
@echo "Please use 'make mode=release' or 'make mode=develop' or 'make mode=debug'"
@exit 1
endif
endif
endif
endif
@echo ".........................."
@echo "Building on "$(mode)" mode "
@echo "CFLAGS=$(CFLAGS)"
@echo "LDLAGS=$(LDFLAGS)"
@echo ".........................."
%.o :
$(CC) -c $(CFLAGS) $(LDFLAGS) $(INCLUDE) -o $@ $<
libhash_ex: libhash_ex.o
$(CC) $(CFLAGS) $(LDFLAGS) $(INCLUDE) -o $@ libhash_ex.o
libhash_ex.o: libhash_ex.[c,h] mpool.o
mpool.o: $(KOLAN_INCLUDE)/mpool.[c,h] xerror.o
netfuncs.o: $(KOLAN_INCLUDE)/netfuncs.[c,h]
listen.o: listen.[c,h] params.h xerror.o
cfg_files.o: cfg_files.[c,h] ssdparser.o
request.o: request.[c,h] ssdparser.o
ssdparser.o: $(KOLAN_PROJECTS)/multiexpat/inc/ssdparser.[c,h] $(KOLAN_INCLUDE)/c_const.h \
$(KOLAN_INCLUDE)/map1251.h $(KOLAN_INCLUDE)/insys_parser/tags.h $(KOLAN_INCLUDE)/insys_parser/const.h xmalloc.o
xmalloc.o: $(KOLAN_INCLUDE)/xmalloc.[c,h] xerror.o
signals.o: signals.[c,h] xerror.o
xerror.o: $(KOLAN_INCLUDE)/xerror.[c,h]
clean:
$(RM) *.o *.out callgrind.out.* *.gcno dataserver echo-client
.PHONY: all clean

BIN
c/libhash_ex/libhash_ex Executable file

Binary file not shown.

67
c/libhash_ex/libhash_ex.c Normal file
View File

@ -0,0 +1,67 @@
#include <hash.h>
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#define MAX_CHANNUM 1024
u_int32_t hash_function(void *key, u_int32_t number_of_buckets)
{
// return hash_hash_int(key, number_of_buckets);
return hash_hash_string(key, number_of_buckets); // вызывается по-умолчанию, если hash_function=NULL в hash_initialise()
}
int compare_keys(void *key1, void *key2)
{
// return hash_compare_int(key1, key2);
return hash_compare_string(key1, key2); // вызывается по-умолчанию, если compare_keys=NULL в hash_initialise()
}
int duplicate_key(void **destination, void *source)
{
// hash_copy_int(destination, source);
hash_copy_string(destination, source); // вызывается по умолчанию, если duplicate_key=NULL в hash_initialise()
return 0;
}
void free_key(void *key)
{
void *warnings_stub = key;
warnings_stub = warnings_stub;
}
void free_value(void *value)
{
void *warnings_stub = value;
warnings_stub = warnings_stub;
}
int main()
{
u_int32_t nch = 5;
struct hash h;
//~ hash_initialise(&h, 2 * nch, NULL, NULL, NULL, NULL, NULL); // пример работы с ключом-строкой
hash_initialise(&h, 2 * nch, hash_function, compare_keys, duplicate_key, free_key, free_value);
hash_insert(&h, "Shashkin", "Kolan");
hash_insert(&h, "Iljin", "Egor");
hash_insert(&h, "Korsakov", "Kostya");
hash_insert(&h, "Sinelnikov", "Alexandr");
hash_insert(&h, "Kochetkov", "Sergey");
hash_insert(&h, "Bovykin", "Sergey");
hash_insert(&h, "Novikov", "Alexey");
char *val_ptr;
hash_retrieve(&h, "Kochetkov", (void **)&val_ptr);
printf("Kochetkov = %s\n", val_ptr);
hash_deinitialise(&h);
return 0;
}

View File

@ -0,0 +1,21 @@
[indentation]
indent_width=8
indent_type=1
indent_hard_tab_width=8
detect_indent=false
indent_mode=2
[project]
name=libhash_ex
base_path=/home/kolan/dev/c/libhash_ex/
[long line marker]
long_line_behaviour=1
long_line_column=72
[files]
current_page=2
FILE_NAME_0=532;C;0;16;1;1;1;/home/kolan/dev/c/libhash_ex/libhash_ex.c;0
FILE_NAME_1=204;C;0;16;1;1;1;/usr/include/hash.h;0
FILE_NAME_2=0;Make;0;16;1;1;1;/home/kolan/dev/c/libhash_ex/Makefile;0