summaryrefslogtreecommitdiff
path: root/runtime/skiplist.c
blob: f81d520b31ea9c6ee11ba98e4980371c3711ecd7 (plain)
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
/**************************************************************************/
/*                                                                        */
/*                                 OCaml                                  */
/*                                                                        */
/*             Xavier Leroy, projet Cambium, INRIA Paris                  */
/*                                                                        */
/*   Copyright 2020 Institut National de Recherche en Informatique et     */
/*     en Automatique.                                                    */
/*                                                                        */
/*   All rights reserved.  This file is distributed under the terms of    */
/*   the GNU Lesser General Public License version 2.1, with the          */
/*   special exception on linking described in the file LICENSE.          */
/*                                                                        */
/**************************************************************************/

#define CAML_INTERNALS

/* A dictionary data structure implemented as skip lists
   (see William Pugh, "Skip lists: a probabilistic alternative to
   balanced binary trees", Comm. ACM 33(6), 1990). */

#include <stddef.h>
#include "caml/config.h"
#include "caml/memory.h"
#include "caml/misc.h"
#include "caml/skiplist.h"

/* Size of struct skipcell, in bytes, without the forward array */
#if (__STDC_VERSION__ >= 199901L)
#define SIZEOF_SKIPCELL sizeof(struct skipcell)
#else
#define SIZEOF_SKIPCELL (sizeof(struct skipcell) - sizeof(struct skipcell *))
#endif

/* Generate a random level for a new node: 0 with probability 3/4,
   1 with probability 3/16, 2 with probability 3/64, etc.
   We use a simple linear congruential PRNG (see Knuth vol 2) instead
   of random(), because we need exactly 32 bits of pseudo-random data
   (i.e. 2 * (NUM_LEVELS - 1)).  Moreover, the congruential PRNG
   is faster and guaranteed to be deterministic (to reproduce bugs). */

static uint32_t random_seed = 0;

static int random_level(void)
{
  uint32_t r;
  int level = 0;

  /* Linear congruence with modulus = 2^32, multiplier = 69069
     (Knuth vol 2 p. 106, line 15 of table 1), additive = 25173. */
  r = random_seed = random_seed * 69069 + 25173;
  /* Knuth (vol 2 p. 13) shows that the least significant bits are
     "less random" than the most significant bits with a modulus of 2^m,
     so consume most significant bits first */
  while ((r & 0xC0000000U) == 0xC0000000U) { level++; r = r << 2; }
  CAMLassert(level < NUM_LEVELS);
  return level;
}

/* Initialize a skip list */

void caml_skiplist_init(struct skiplist * sk)
{
  int i;
  for (i = 0; i < NUM_LEVELS; i++) sk->forward[i] = NULL;
  sk->level = 0;
}

/* Search a skip list */

int caml_skiplist_find(struct skiplist * sk, uintnat key, uintnat * data)
{
  int i;
  struct skipcell ** e, * f;

  e = sk->forward;
  for (i = sk->level; i >= 0; i--) {
    while (1) {
      f = e[i];
      if (f == NULL || f->key > key) break;
      if (f->key == key) {
        *data = f->data;
        return 1;
      }
      e = f->forward;
    }
  }
  return 0;
}

int caml_skiplist_find_below(struct skiplist * sk, uintnat k,
                             uintnat * key, uintnat * data)
{
  int i;
  struct skipcell ** e, * f, * last = NULL;

  e = sk->forward;
  for (i = sk->level; i >= 0; i--) {
    while (1) {
      f = e[i];
      if (f == NULL || f->key > k) break;
      last = f;
      e = f->forward;
    }
  }
  if (!last) {
    return 0;
  } else {
    *key = last-> key; *data = last->data; return 1;
  }
}

/* Insertion in a skip list */

int caml_skiplist_insert(struct skiplist * sk,
                         uintnat key, uintnat data)
{
  struct skipcell ** update[NUM_LEVELS];
  struct skipcell ** e, * f;
  int i, new_level;

  /* Init "cursor" to list head */
  e = sk->forward;
  /* Find place to insert new node */
  for (i = sk->level; i >= 0; i--) {
    while (1) {
      f = e[i];
      if (f == NULL || f->key >= key) break;
      e = f->forward;
    }
    update[i] = &e[i];
  }
  f = e[0];
  /* If already present, update data */
  if (f != NULL && f->key == key) {
    f->data = data;
    return 1;
  }
  /* Insert additional element, updating list level if necessary */
  new_level = random_level();
  if (new_level > sk->level) {
    for (i = sk->level + 1; i <= new_level; i++)
      update[i] = &sk->forward[i];
    sk->level = new_level;
  }
  f = caml_stat_alloc(SIZEOF_SKIPCELL +
                      (new_level + 1) * sizeof(struct skipcell *));
  f->key = key;
  f->data = data;
  for (i = 0; i <= new_level; i++) {
    f->forward[i] = *update[i];
    *update[i] = f;
  }
  return 0;
}

/* Deletion in a skip list */

int caml_skiplist_remove(struct skiplist * sk, uintnat key)
{
  struct skipcell ** update[NUM_LEVELS];
  struct skipcell ** e, * f;
  int i;

  /* Init "cursor" to list head */
  e = sk->forward;
  /* Find element in list */
  for (i = sk->level; i >= 0; i--) {
    while (1) {
      f = e[i];
      if (f == NULL || f->key >= key) break;
      e = f->forward;
    }
    update[i] = &e[i];
  }
  f = e[0];
  /* If not found, nothing to do */
  if (f == NULL || f->key != key) return 0;
  /* Rebuild list without node */
  for (i = 0; i <= sk->level; i++) {
    if (*update[i] == f)
      *update[i] = f->forward[i];
  }
  /* Reclaim list element */
  caml_stat_free(f);
  /* Down-correct list level */
  while (sk->level > 0 &&
         sk->forward[sk->level] == NULL)
    sk->level--;
  return 1;
}

/* Empty a skip list */

void caml_skiplist_empty(struct skiplist * sk)
{
  struct skipcell * e, * next;
  int i;

  for (e = sk->forward[0]; e != NULL; e = next) {
    next = e->forward[0];
    caml_stat_free(e);
  }
  for (i = 0; i <= sk->level; i++) sk->forward[i] = NULL;
  sk->level = 0;
}