MobilityDB  1.0
skiplist.h
Go to the documentation of this file.
1 /*****************************************************************************
2  *
3  * This MobilityDB code is provided under The PostgreSQL License.
4  *
5  * Copyright (c) 2016-2021, Université libre de Bruxelles and MobilityDB
6  * contributors
7  *
8  * MobilityDB includes portions of PostGIS version 3 source code released
9  * under the GNU General Public License (GPLv2 or later).
10  * Copyright (c) 2001-2021, PostGIS contributors
11  *
12  * Permission to use, copy, modify, and distribute this software and its
13  * documentation for any purpose, without fee, and without a written
14  * agreement is hereby granted, provided that the above copyright notice and
15  * this paragraph and the following two paragraphs appear in all copies.
16  *
17  * IN NO EVENT SHALL UNIVERSITE LIBRE DE BRUXELLES BE LIABLE TO ANY PARTY FOR
18  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING
19  * LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION,
20  * EVEN IF UNIVERSITE LIBRE DE BRUXELLES HAS BEEN ADVISED OF THE POSSIBILITY
21  * OF SUCH DAMAGE.
22  *
23  * UNIVERSITE LIBRE DE BRUXELLES SPECIFICALLY DISCLAIMS ANY WARRANTIES,
24  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
25  * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON
26  * AN "AS IS" BASIS, AND UNIVERSITE LIBRE DE BRUXELLES HAS NO OBLIGATIONS TO
27  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. 
28  *
29  *****************************************************************************/
30 
36 #ifndef __SKIPLIST_H__
37 #define __SKIPLIST_H__
38 
39 #include <postgres.h>
40 #include <catalog/pg_type.h>
41 
42 #include "temporal.h"
43 
44 /*****************************************************************************/
45 
46 /* Constants defining the behaviour of skip lists which are internal types
47  for computing aggregates */
48 
49 #define SKIPLIST_MAXLEVEL 32
50 #define SKIPLIST_INITIAL_CAPACITY 1024
51 #define SKIPLIST_GROW 1
52 #define SKIPLIST_INITIAL_FREELIST 32
53 
54 /*****************************************************************************/
55 
60 typedef struct
61 {
62  void *value;
63  int height;
64  int next[SKIPLIST_MAXLEVEL];
65 } Elem;
66 
67 typedef enum
68 {
72 } ElemType;
73 
77 typedef struct
78 {
80  int capacity;
81  int next;
82  int length;
83  int *freed;
84  int freecount;
85  int freecap;
86  int tail;
87  void *extra;
88  size_t extrasize;
90 } SkipList;
91 
92 /*****************************************************************************/
93 
94 extern Datum tagg_serialize(PG_FUNCTION_ARGS);
95 extern Datum tagg_deserialize(PG_FUNCTION_ARGS);
96 
97 /*****************************************************************************/
98 
99 extern SkipList *skiplist_make(FunctionCallInfo fcinfo, void **values,
100  int count, ElemType elemtype);
101 extern void *skiplist_headval(SkipList *list);
102 extern void skiplist_splice(FunctionCallInfo fcinfo, SkipList *list,
103  void **values, int count, datum_func2 func, bool crossings);
104 extern void **skiplist_values(SkipList *list);
105 extern void aggstate_set_extra(FunctionCallInfo fcinfo, SkipList *state,
106  void *data, size_t size);
107 
108 /*****************************************************************************/
109 
110 /* Functions for splicing the skiplist */
111 
112 TimestampTz *timestamp_agg(TimestampTz *times1, int count1, TimestampTz *times2,
113  int count2, int *newcount);
114 Period **period_agg(Period **periods1, int count1, Period **periods2,
115  int count2, int *newcount);
116 TInstant **tinstant_tagg(TInstant **instants1, int count1, TInstant **instants2,
117  int count2, Datum (*func)(Datum, Datum), int *newcount);
118 TSequence **tsequence_tagg(TSequence **sequences1, int count1, TSequence **sequences2,
119  int count2, Datum (*func)(Datum, Datum), bool crossings, int *newcount);
120 
121 /*****************************************************************************/
122 
123 #endif
Structure to represent periods.
Definition: timetypes.h:52
int length
Definition: skiplist.h:82
ElemType
Definition: skiplist.h:67
void ** skiplist_values(SkipList *list)
Returns the values contained in the skiplist.
Definition: skiplist.c:556
void skiplist_splice(FunctionCallInfo fcinfo, SkipList *list, void **values, int count, datum_func2 func, bool crossings)
Splice the skiplist with the array of values using the aggregation function.
Definition: skiplist.c:369
void aggstate_set_extra(FunctionCallInfo fcinfo, SkipList *state, void *data, size_t size)
Reads the state value from the buffer.
Definition: skiplist.c:681
Basic functions for temporal types of any subtype.
void * value
Definition: skiplist.h:62
int * freed
Definition: skiplist.h:83
Structure to represent skiplists that keep the current state of an aggregation.
Definition: skiplist.h:77
Datum tagg_serialize(PG_FUNCTION_ARGS)
Serialize the state value.
Definition: skiplist.c:698
int freecount
Definition: skiplist.h:84
int height
Definition: skiplist.h:63
void * extra
Definition: skiplist.h:87
Definition: skiplist.h:70
TSequence ** tsequence_tagg(TSequence **sequences1, int count1, TSequence **sequences2, int count2, Datum(*func)(Datum, Datum), bool crossings, int *newcount)
Generic aggregate function for temporal sequences.
Definition: temporal_aggfuncs.c:362
Period ** period_agg(Period **periods1, int count1, Period **periods2, int count2, int *newcount)
Generic aggregate function for periods.
Definition: time_aggfuncs.c:130
Elem * elems
Definition: skiplist.h:89
TInstant ** tinstant_tagg(TInstant **instants1, int count1, TInstant **instants2, int count2, Datum(*func)(Datum, Datum), int *newcount)
Definition: temporal_aggfuncs.c:173
Definition: skiplist.h:69
Datum(* datum_func2)(Datum, Datum)
Definition: temporal.h:358
TimestampTz * timestamp_agg(TimestampTz *times1, int count1, TimestampTz *times2, int count2, int *newcount)
Definition: time_aggfuncs.c:82
SkipList * skiplist_make(FunctionCallInfo fcinfo, void **values, int count, ElemType elemtype)
Outputs the skiplist in graphviz dot format for visualisation and debugging purposes.
Definition: skiplist.c:270
ElemType elemtype
Definition: skiplist.h:79
Datum tagg_deserialize(PG_FUNCTION_ARGS)
Deserialize the state value.
Definition: skiplist.c:712
void * skiplist_headval(SkipList *list)
Returns the value at the head of the skiplist.
Definition: skiplist.c:337
int capacity
Definition: skiplist.h:80
Definition: skiplist.h:71
size_t extrasize
Definition: skiplist.h:88
#define SKIPLIST_MAXLEVEL
maximum possible is 47 with current RNG
Definition: skiplist.h:49
int tail
Definition: skiplist.h:86
Structure to represent temporal values of sequence subtype.
Definition: temporal.h:279
int freecap
Definition: skiplist.h:85
Structure to represent elements in the skiplists.
Definition: skiplist.h:60
int next
Definition: skiplist.h:81
Structure to represent temporal values of instant subtype.
Definition: temporal.h:253