qfrjava
2025-04-14 b94d4dd2e6bb5be898cda29c7e8a5356de1e60d7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
 
#include "postgres.h"
 
#include "utils/age_graphid_ds.h"
 
/* defines */
/*
 * A simple linked list node for graphid lists (int64). PG's implementation
 * has too much overhead for this type of list as it only directly supports
 * regular ints, not int64s, of which a graphid currently is.
 */
typedef struct GraphIdNode
{
    graphid id;
    struct GraphIdNode *next;
} GraphIdNode;
 
/* a container for a linked list of GraphIdNodes */
typedef struct ListGraphId
{
    GraphIdNode *head;
    GraphIdNode *tail;
    int64 size;
} ListGraphId;
 
/* declarations */
 
/* definitions */
/* return the next GraphIdNode */
GraphIdNode *next_GraphIdNode(GraphIdNode *node)
{
    return node->next;
}
 
/* return the graphid */
graphid get_graphid(GraphIdNode *node)
{
    return node->id;
}
 
/* get the size of the passed stack */
int64 get_stack_size(ListGraphId *stack)
{
    return stack->size;
}
 
/* return a reference to the head entry in the stack, or NULL if empty */
GraphIdNode *peek_stack_head(ListGraphId *stack)
{
    if (stack == NULL)
    {
        return NULL;
    }
 
    return stack->head;
}
 
/* return a reference to the tail entry in the stack */
GraphIdNode *peek_stack_tail(ListGraphId *stack)
{
    return stack->tail;
}
 
/* return a reference to the head entry of a list */
GraphIdNode *get_list_head(ListGraphId *list)
{
    return list->head;
}
 
/* get the size of the passed list */
int64 get_list_size(ListGraphId *list)
{
    return list->size;
}
 
/*
 * Helper function to add a graphid to the end of a ListGraphId container.
 * If the container is NULL, it creates the container with the entry.
 */
ListGraphId *append_graphid(ListGraphId *container, graphid id)
{
    GraphIdNode *new_node = NULL;
 
    /* create the new link */
    new_node = palloc0(sizeof(GraphIdNode));
    new_node->id = id;
    new_node->next = NULL;
 
    /*
     * If the container is NULL then this is a new list. So, create the
     * container and add in the new link.
     */
    if (container == NULL)
    {
        container = palloc0(sizeof(ListGraphId));
        container->head = new_node;
        container->tail = new_node;
        container->size = 1;
    }
    /* otherwise, this is an existing list, append id */
    else
    {
        container->tail->next = new_node;
        container->tail = new_node;
        container->size++;
    }
 
    return container;
}
 
/* free (delete) a ListGraphId list */
void free_ListGraphId(ListGraphId *container)
{
    GraphIdNode *curr_node = NULL;
    GraphIdNode *next_node = NULL;
 
    /* if the container is NULL we don't need to delete anything */
    if (container == NULL)
    {
        return;
    }
 
    /* otherwise, start from the head, free, and delete the links */
    curr_node = container->head;
    while (curr_node != NULL)
    {
        next_node = curr_node->next;
        /* we can do this because this is just a list of ints */
        pfree(curr_node);
        curr_node = next_node;
    }
 
    /* free the container */
    pfree(container);
}
 
/* helper function to create a new, empty, graphid stack */
ListGraphId *new_graphid_stack(void)
{
    ListGraphId *stack = NULL;
 
    /* allocate the container for the stack */
    stack = palloc0(sizeof(ListGraphId));
 
    /* set it to its initial values */
    stack->head = NULL;
    stack->tail = NULL;
    stack->size = 0;
 
    /* return the new stack */
    return stack;
}
 
/* helper function to free a graphid stack's contents but, not the container */
void free_graphid_stack(ListGraphId *stack)
{
    Assert(stack != NULL);
 
    if (stack == NULL)
    {
        elog(ERROR, "free_graphid_stack: NULL stack");
    }
 
    /* while there are entries */
    while (stack->head != NULL)
    {
        /* get the next element in the stack */
        GraphIdNode *next = stack->head->next;
 
        /* free the head element */
        pfree(stack->head);
        /* move the head to the next */
        stack->head = next;
    }
 
    /* reset the tail and size */
    stack->tail = NULL;
    stack->size = 0;
}
 
/*
 * Helper function for a generic push graphid (int64) to a stack. If the stack
 * is NULL, it will error out.
 */
void push_graphid_stack(ListGraphId *stack, graphid id)
{
    GraphIdNode *new_node = NULL;
 
    Assert(stack != NULL);
 
    if (stack == NULL)
    {
        elog(ERROR, "push_graphid_stack: NULL stack");
    }
 
    /* create the new element */
    new_node = palloc0(sizeof(GraphIdNode));
    new_node->id = id;
 
    /* insert (push) the new element on the top */
    new_node->next = stack->head;
    stack->head = new_node;
    stack->size++;
}
 
/*
 * Helper function for a generic pop graphid (int64) from a stack. If the stack
 * is empty, it will error out. You should verify that the stack isn't empty
 * prior to calling.
 */
graphid pop_graphid_stack(ListGraphId *stack)
{
    GraphIdNode *node = NULL;
    graphid id;
 
    Assert(stack != NULL);
    Assert(stack->size != 0);
 
    if (stack == NULL)
    {
        elog(ERROR, "pop_graphid_stack: NULL stack");
    }
 
    if (stack->size <= 0)
    {
        elog(ERROR, "pop_graphid_stack: empty stack");
    }
 
 
    /* remove the element from the top of the stack */
    node = stack->head;
    id = node->id;
    stack->head = stack->head->next;
    stack->size--;
    /* free the element */
    pfree(node);
 
    /* return the id */
    return id;
}