📄 cabin.h
字号:
/************************************************************************************************* * The utitlity API of QDBM * Copyright (C) 2000-2003 Mikio Hirabayashi * This file is part of QDBM, Quick Database Manager. * QDBM is free software; you can redistribute it and/or modify it under the terms of the GNU * Lesser General Public License as published by the Free Software Foundation; either version * 2.1 of the License or any later version. QDBM is distributed in the hope that it will be * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more * details. * You should have received a copy of the GNU Lesser General Public License along with QDBM; if * not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA * 02111-1307 USA. *************************************************************************************************/#ifndef _CABIN_H /* duplication check */#define _CABIN_H#include <stdlib.h>/************************************************************************************************* * API *************************************************************************************************/typedef struct { /* type of structure for a basic datum */ char *dptr; /* pointer to the region */ int dsize; /* size of the region */ int asize; /* size of the allocated region */} CBDATUM;typedef struct { /* type of structure for an element of a list */ char *dptr; /* pointer to the region */ int dsize; /* size of the effective region */} CBLISTDATUM;typedef struct { /* type of structure for a list */ CBLISTDATUM *array; /* array of data */ int anum; /* number of the elements of the array */ int start; /* start index of using elements */ int num; /* number of using elements */} CBLIST;typedef struct { /* type of structure for an element of a map */ char *kbuf; /* pointer to the region of the key */ int ksiz; /* size of the region of the key */ char *vbuf; /* pointer to the region of the value */ int vsiz; /* size of the region of the value */ int hash; /* second hash value */ char *left; /* pointer to the left child */ char *right; /* pointer to the right child */ char *prev; /* pointer to the previous element */ char *next; /* pointer to the next element */} CBMAPDATUM;typedef struct { /* type of structure for a map */ CBMAPDATUM **buckets; /* bucket array */ CBMAPDATUM *first; /* pointer to the first element */ CBMAPDATUM *last; /* pointer to the last element */ CBMAPDATUM *cur; /* pointer to the current element */ int rnum; /* number of records */} CBMAP;/* Call back function for handling a fatal error. The argument specifies the error message. The initial value of this variable is `NULL'. If the value is `NULL', the default function is called when a fatal error occurs. A fatal error occures when memory allocation is failed. */extern void (*cbfatalfunc)(const char *);/* Allocate a region on memory. `size' specifies the size of the region. The return value is the pointer to the allocated region. Because the region of the return value is allocated with the `malloc' call, it should be released with the `free' call if it is no longer in use. */void *cbmalloc(size_t size);/* Re-allocate a region on memory. `ptr' specifies the pointer to a region. `size' specifies the size of the region. The return value is the pointer to the re-allocated region. Because the region of the return value is allocated with the `realloc' call, it should be released with the `free' call if it is no longer in use. */void *cbrealloc(void *ptr, size_t size);/* Duplicate a region on memory. `ptr' specifies the pointer to a region. `size' specifies the size of the region. If it is negative, the size is assigned with `strlen(ptr)'. The return value is the pointer to the allocated region of the duplicate. Because an additional zero code is appended at the end of the region of the return value, the return value can be treated as a character string. Because the region of the return value is allocated with the `malloc' call, it should be released with the `free' call if it is no longer in use. */char *cbmemdup(const char *ptr, int size);/* Register the pointer or handle of an object to the global garbage collector. `ptr' specifies the pointer or handle of an object. `func' specifies the pointer to a function to release resouces of the object. Its argument is the pointer or handle of the object to release. This function assures that resources of an object are released when the process exits normally by returning from the `main' function or calling the `exit' function. */void cbglobalgc(void *ptr, void (*func)(void *));/* Sort an array using insert sort. `base' spacifies the pointer to an array. `nmemb' specifies the number of elements of the array. `size' specifies the size of each element. `compar' specifies the pointer to comparing function. The two arguments specify the pointers of elements. The comparing function should returns positive if the former is big, negative if the latter is big, 0 if both are equal. Insert sort is useful only if most elements have been sorted already. */void cbisort(void *base, int nmemb, int size, int(*compar)(const void *, const void *));/* Sort an array using shell sort. `base' spacifies the pointer to an array. `nmemb' specifies the number of elements of the array. `size' specifies the size of each element. `compar' specifies the pointer to comparing function. The two arguments specify the pointers of elements. The comparing function should returns positive if the former is big, negative if the latter is big, 0 if both are equal. If most elements have been sorted, shell sort may be faster than heap sort or quick sort. */void cbssort(void *base, int nmemb, int size, int(*compar)(const void *, const void *));/* Sort an array using heap sort. `base' spacifies the pointer to an array. `nmemb' specifies the number of elements of the array. `size' specifies the size of each element. `compar' specifies the pointer to comparing function. The two arguments specify the pointers of elements. The comparing function should returns positive if the former is big, negative if the latter is big, 0 if both are equal. Although heap sort is robust against bias of input, quick sort is faster in most cases. */void cbhsort(void *base, int nmemb, int size, int(*compar)(const void *, const void *));/* Sort an array using quick sort. `base' spacifies the pointer to an array. `nmemb' specifies the number of elements of the array. `size' specifies the size of each element. `compar' specifies the pointer to comparing function. The two arguments specify the pointers of elements. The comparing function should returns positive if the former is big, negative if the latter is big, 0 if both are equal. Being sensitive to bias of input, quick sort is the fastest sorting algorithm. */void cbqsort(void *base, int nmemb, int size, int(*compar)(const void *, const void *));/* Get a datum handle. `ptr' specifies the pointer to the region of the initial content. If it is `NULL', an empty datum is created. `size' specifies the size of the region. If it is negative, the size is assigned with `strlen(ptr)'. The return value is a datum handle. */CBDATUM *cbdatumopen(const char *ptr, int size);/* Copy a datum. `datum' specifies a datum handle. The return value is a new datum handle. */CBDATUM *cbdatumdup(const CBDATUM *datum);/* Free a datum handle. `datum' specifies a datum handle. Because the region of a closed handle is released, it becomes impossible to use the handle. */void cbdatumclose(CBDATUM *datum);/* Concatenate a datum and a region. `datum' specifies a datum handle. `ptr' specifies the pointer to the region to be appended. `size' specifies the size of the region. If it is negative, the size is assigned with `strlen(ptr)'. */void cbdatumcat(CBDATUM *datum, const char *ptr, int size);/* Get the pointer of the region of a datum. `datum' specifies a datum handle. The return value is the pointer of the region of a datum. Because an additional zero code is appended at the end of the region of the return value, the return value can be treated as a character string. */const char *cbdatumptr(const CBDATUM *datum);/* Get the size of the region of a datum. `datum' specifies a datum handle. The return value is the size of the region of a datum. */int cbdatumsize(const CBDATUM *datum);/* Change the size of the region of a datum. `datum' specifies a datum handle. `size' specifies the new size of the region. If the new size is bigger than the one of old, the surplus region is filled with zero codes. */void cbdatumsetsize(CBDATUM *datum, int size);/* Get a list handle. The return value is a list handle. */CBLIST *cblistopen(void);/* Copy a list. `list' specifies a list handle. The return value is a new list handle. */CBLIST *cblistdup(const CBLIST *list);/* Close a list handle. `list' specifies a list handle. Because the region of a closed handle is released, it becomes impossible to use the handle. */void cblistclose(CBLIST *list);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -