~vcs-imports/mammoth-replicator/trunk

« back to all changes in this revision

Viewing changes to contrib/tsearch2/snmap.c

  • Committer: alvherre
  • Date: 2005-12-16 21:24:52 UTC
  • Revision ID: svn-v4:db760fc0-0f08-0410-9d63-cc6633f64896:trunk:1
Initial import of the REL8_0_3 sources from the Pgsql CVS repository.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * simple but fast map from str to Oid
 
3
 * Teodor Sigaev <teodor@sigaev.ru>
 
4
 */
 
5
#include <errno.h>
 
6
#include <stdlib.h>
 
7
#include <string.h>
 
8
 
 
9
#include "postgres.h"
 
10
#include "snmap.h"
 
11
#include "common.h"
 
12
 
 
13
static int
 
14
compareSNMapEntry(const void *a, const void *b)
 
15
{
 
16
        if (((SNMapEntry *) a)->nsp < ((SNMapEntry *) b)->nsp)
 
17
                return -1;
 
18
        else if (((SNMapEntry *) a)->nsp > ((SNMapEntry *) b)->nsp)
 
19
                return 1;
 
20
        else
 
21
                return strcmp(((SNMapEntry *) a)->key, ((SNMapEntry *) b)->key);
 
22
}
 
23
 
 
24
void
 
25
addSNMap(SNMap * map, char *key, Oid value)
 
26
{
 
27
        if (map->len >= map->reallen)
 
28
        {
 
29
                SNMapEntry *tmp;
 
30
                int                     len = (map->reallen) ? 2 * map->reallen : 16;
 
31
 
 
32
                tmp = (SNMapEntry *) realloc(map->list, sizeof(SNMapEntry) * len);
 
33
                if (!tmp)
 
34
                        ereport(ERROR,
 
35
                                        (errcode(ERRCODE_OUT_OF_MEMORY),
 
36
                                         errmsg("out of memory")));
 
37
                map->reallen = len;
 
38
                map->list = tmp;
 
39
        }
 
40
        map->list[map->len].key = strdup(key);
 
41
        if (!map->list[map->len].key)
 
42
                ereport(ERROR,
 
43
                                (errcode(ERRCODE_OUT_OF_MEMORY),
 
44
                                 errmsg("out of memory")));
 
45
        map->list[map->len].nsp = get_oidnamespace(TSNSP_FunctionOid);
 
46
        map->list[map->len].value = value;
 
47
        map->len++;
 
48
        if (map->len > 1)
 
49
                qsort(map->list, map->len, sizeof(SNMapEntry), compareSNMapEntry);
 
50
}
 
51
 
 
52
void
 
53
addSNMap_t(SNMap * map, text *key, Oid value)
 
54
{
 
55
        char       *k = text2char(key);
 
56
 
 
57
        addSNMap(map, k, value);
 
58
        pfree(k);
 
59
}
 
60
 
 
61
Oid
 
62
findSNMap(SNMap * map, char *key)
 
63
{
 
64
        SNMapEntry *ptr;
 
65
        SNMapEntry      ks;
 
66
 
 
67
        ks.key = key;
 
68
        ks.nsp = get_oidnamespace(TSNSP_FunctionOid);
 
69
        ks.value = 0;
 
70
 
 
71
        if (map->len == 0 || !map->list)
 
72
                return 0;
 
73
        ptr = (SNMapEntry *) bsearch(&ks, map->list, map->len, sizeof(SNMapEntry), compareSNMapEntry);
 
74
        return (ptr) ? ptr->value : 0;
 
75
}
 
76
 
 
77
Oid
 
78
findSNMap_t(SNMap * map, text *key)
 
79
{
 
80
        char       *k = text2char(key);
 
81
        int                     res;
 
82
 
 
83
        res = findSNMap(map, k);
 
84
        pfree(k);
 
85
        return res;
 
86
}
 
87
 
 
88
void
 
89
freeSNMap(SNMap * map)
 
90
{
 
91
        SNMapEntry *entry = map->list;
 
92
 
 
93
        if (map->list)
 
94
        {
 
95
                while (map->len)
 
96
                {
 
97
                        if (entry->key)
 
98
                                free(entry->key);
 
99
                        entry++;
 
100
                        map->len--;
 
101
                }
 
102
                free(map->list);
 
103
        }
 
104
        memset(map, 0, sizeof(SNMap));
 
105
}