📄 tcutil.h
字号:
/************************************************************************************************* * hash map *************************************************************************************************/typedef struct _TCMAPREC { /* type of structure for an element of a map */ uint32_t ksiz; /* size of the region of the key */ uint32_t vsiz; /* size of the region of the value */ struct _TCMAPREC *left; /* pointer to the left child */ struct _TCMAPREC *right; /* pointer to the right child */ struct _TCMAPREC *prev; /* pointer to the previous element */ struct _TCMAPREC *next; /* pointer to the next element */} TCMAPREC;typedef struct { /* type of structure for a map */ TCMAPREC **buckets; /* bucket array */ TCMAPREC *first; /* pointer to the first element */ TCMAPREC *last; /* pointer to the last element */ TCMAPREC *cur; /* pointer to the current element */ uint32_t bnum; /* number of buckets */ uint64_t rnum; /* number of records */ uint64_t msiz; /* total size of records */} TCMAP;/* Create a map object. The return value is the new map object. */TCMAP *tcmapnew(void);/* Create a map object with specifying the number of the buckets. `bnum' specifies the number of the buckets. The return value is the new map object. */TCMAP *tcmapnew2(uint32_t bnum);/* Create a map object with initial string elements. `str' specifies the string of the first element. The other arguments are other elements. They should be trailed by a `NULL' argument. The return value is the new map object. The key and the value of each record are situated one after the other. */TCMAP *tcmapnew3(const char *str, ...);/* Copy a map object. `map' specifies the map object. The return value is the new map object equivalent to the specified object. */TCMAP *tcmapdup(const TCMAP *map);/* Delete a map object. `map' specifies the map object. Note that the deleted object and its derivatives can not be used anymore. */void tcmapdel(TCMAP *map);/* Store a record into a map object. `map' specifies the map object. `kbuf' specifies the pointer to the region of the key. `ksiz' specifies the size of the region of the key. `vbuf' specifies the pointer to the region of the value. `vsiz' specifies the size of the region of the value. If a record with the same key exists in the map, it is overwritten. */void tcmapput(TCMAP *map, const void *kbuf, int ksiz, const void *vbuf, int vsiz);/* Store a string record into a map object. `map' specifies the map object. `kstr' specifies the string of the key. `vstr' specifies the string of the value. If a record with the same key exists in the map, it is overwritten. */void tcmapput2(TCMAP *map, const char *kstr, const char *vstr);/* Store a new record into a map object. `map' specifies the map object. `kbuf' specifies the pointer to the region of the key. `ksiz' specifies the size of the region of the key. `vbuf' specifies the pointer to the region of the value. `vsiz' specifies the size of the region of the value. If successful, the return value is true, else, it is false. If a record with the same key exists in the map, this function has no effect. */bool tcmapputkeep(TCMAP *map, const void *kbuf, int ksiz, const void *vbuf, int vsiz);/* Store a new string record into a map object. `map' specifies the map object. `kstr' specifies the string of the key. `vstr' specifies the string of the value. If successful, the return value is true, else, it is false. If a record with the same key exists in the map, this function has no effect. */bool tcmapputkeep2(TCMAP *map, const char *kstr, const char *vstr);/* Concatenate a value at the end of the value of the existing record in a map object. `map' specifies the map object. `kbuf' specifies the pointer to the region of the key. `ksiz' specifies the size of the region of the key. `vbuf' specifies the pointer to the region of the value. `vsiz' specifies the size of the region of the value. If there is no corresponding record, a new record is created. */void tcmapputcat(TCMAP *map, const void *kbuf, int ksiz, const void *vbuf, int vsiz);/* Concatenate a string value at the end of the value of the existing record in a map object. `map' specifies the map object. `kstr' specifies the string of the key. `vstr' specifies the string of the value. If there is no corresponding record, a new record is created. */void tcmapputcat2(TCMAP *map, const char *kstr, const char *vstr);/* Remove a record of a map object. `map' specifies the map object. `kbuf' specifies the pointer to the region of the key. `ksiz' specifies the size of the region of the key. If successful, the return value is true. False is returned when no record corresponds to the specified key. */bool tcmapout(TCMAP *map, const void *kbuf, int ksiz);/* Remove a string record of a map object. `map' specifies the map object. `kstr' specifies the string of the key. If successful, the return value is true. False is returned when no record corresponds to the specified key. */bool tcmapout2(TCMAP *map, const char *kstr);/* Retrieve a record in a map object. `map' specifies the map object. `kbuf' specifies the pointer to the region of the key. `ksiz' specifies the size of the region of the key. `sp' specifies the pointer to the variable into which the size of the region of the return value is assigned. If successful, the return value is the pointer to the region of the value of the corresponding record. `NULL' is returned when no record corresponds. 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 void *tcmapget(const TCMAP *map, const void *kbuf, int ksiz, int *sp);/* Retrieve a string record in a map object. `map' specifies the map object. `kstr' specifies the string of the key. If successful, the return value is the string of the value of the corresponding record. `NULL' is returned when no record corresponds. */const char *tcmapget2(const TCMAP *map, const char *kstr);/* Move a record to the edge of a map object. `map' specifies the map object. `kbuf' specifies the pointer to the region of a key. `ksiz' specifies the size of the region of the key. `head' specifies the destination which is the head if it is true or the tail if else. If successful, the return value is true. False is returned when no record corresponds to the specified key. */bool tcmapmove(TCMAP *map, const void *kbuf, int ksiz, bool head);/* Move a string record to the edge of a map object. `map' specifies the map object. `kstr' specifies the string of a key. `head' specifies the destination which is the head if it is true or the tail if else. If successful, the return value is true. False is returned when no record corresponds to the specified key. */bool tcmapmove2(TCMAP *map, const char *kstr, bool head);/* Initialize the iterator of a map object. `map' specifies the map object. The iterator is used in order to access the key of every record stored in the map object. */void tcmapiterinit(TCMAP *map);/* Get the next key of the iterator of a map object. `map' specifies the map object. `sp' specifies the pointer to the variable into which the size of the region of the return value is assigned. If successful, the return value is the pointer to the region of the next key, else, it is `NULL'. `NULL' is returned when no record can be fetched from the iterator. 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. The order of iteration is assured to be the same as the stored order. */const void *tcmapiternext(TCMAP *map, int *sp);/* Get the next key string of the iterator of a map object. `map' specifies the map object. If successful, the return value is the pointer to the region of the next key, else, it is `NULL'. `NULL' is returned when no record can be fetched from the iterator. The order of iteration is assured to be the same as the stored order. */const char *tcmapiternext2(TCMAP *map);/* Get the number of records stored in a map object. `map' specifies the map object. The return value is the number of the records stored in the map object. */uint64_t tcmaprnum(const TCMAP *map);/* Get the total size of memory used in a map object. `map' specifies the map object. The return value is the total size of memory used in a map object. */uint64_t tcmapmsiz(const TCMAP *map);/* Create a list object containing all keys in a map object. `map' specifies the map object. The return value is the new list object containing all keys in the map object. Because the object of the return value is created with the function `tclistnew', it should be deleted with the function `tclistdel' when it is no longer in use. */TCLIST *tcmapkeys(const TCMAP *map);/* Create a list object containing all values in a map object. `map' specifies the map object. The return value is the new list object containing all values in the map object. Because the object of the return value is created with the function `tclistnew', it should be deleted with the function `tclistdel' when it is no longer in use. */TCLIST *tcmapvals(const TCMAP *map);/* Add an integer to a record in a map object. `map' specifies the map object. `kbuf' specifies the pointer to the region of the key. `ksiz' specifies the size of the region of the key. `num' specifies the additional value. The return value is the summation value. If the corresponding record exists, the value is treated as an integer and is added to. If no record corresponds, a new record of the additional value is stored. */int tcmapaddint(TCMAP *map, const void *kbuf, int ksiz, int num);/* Add a real number to a record in a map object. `map' specifies the map object. `kbuf' specifies the pointer to the region of the key. `ksiz' specifies the size of the region of the key. `num' specifies the additional value. The return value is the summation value. If the corresponding record exists, the value is treated as a real number and is added to. If no record corresponds, a new record of the additional value is stored. */double tcmapadddouble(TCMAP *map, const void *kbuf, int ksiz, double num);/* Clear a map object. `map' specifies the map object. All records are removed. */void tcmapclear(TCMAP *map);/* Remove front records of a map object. `map' specifies the map object. `num' specifies the number of records to be removed. */void tcmapcutfront(TCMAP *map, int num);/* Serialize a map object into a byte array. `map' specifies the map object. `sp' specifies the pointer to the variable into which the size of the region of the return value is assigned. The return value is the pointer to the region of the result serial region. Because the region of the return value is allocated with the `malloc' call, it should be released with the `free' call when it is no longer in use. */void *tcmapdump(const TCMAP *map, int *sp);/* Create a map object from a serialized byte array. `ptr' specifies the pointer to the region of serialized byte array. `size' specifies the size of the region. The return value is a new map object. Because the object of the return value is created with the function `tcmapnew', it should be deleted with the function `tcmapdel' when it is no longer in use. */TCMAP *tcmapload(const void *ptr, int size);/************************************************************************************************* * hash map (for experts) *************************************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -