aboutsummaryrefslogtreecommitdiffstats
path: root/src/libutil/heap.h
diff options
context:
space:
mode:
authorVsevolod Stakhov <vsevolod@highsecure.ru>2016-04-08 14:18:26 +0100
committerVsevolod Stakhov <vsevolod@highsecure.ru>2016-04-08 14:18:26 +0100
commit0bab2739e1f9923bd704d3a70e99c2bda496f87d (patch)
treee9945721600457c72c8e40364f7e27b936406784 /src/libutil/heap.h
parenta4f8e31bff5546c25aee8bbddb5093c25afa762f (diff)
downloadrspamd-0bab2739e1f9923bd704d3a70e99c2bda496f87d.tar.gz
rspamd-0bab2739e1f9923bd704d3a70e99c2bda496f87d.zip
[Feature] Add preliminary implementation of binary heap
Signed-off-by: Vsevolod Stakhov <vsevolod@highsecure.ru>
Diffstat (limited to 'src/libutil/heap.h')
-rw-r--r--src/libutil/heap.h71
1 files changed, 71 insertions, 0 deletions
diff --git a/src/libutil/heap.h b/src/libutil/heap.h
new file mode 100644
index 000000000..58a9a0c88
--- /dev/null
+++ b/src/libutil/heap.h
@@ -0,0 +1,71 @@
+/*-
+ * Copyright 2016 Vsevolod Stakhov
+ *
+ * Licensed 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.
+ */
+#ifndef SRC_LIBUTIL_HEAP_H_
+#define SRC_LIBUTIL_HEAP_H_
+
+#include "config.h"
+
+/**
+ * Binary minimal heap interface based on glib
+ */
+
+struct rspamd_min_heap_elt {
+ gpointer data;
+ guint pri;
+ guint idx;
+};
+
+struct rspamd_min_heap;
+
+/**
+ * Creates min heap with the specified reserved size and compare function
+ * @param reserved_size reserved size in elements
+ * @return opaque minimal heap
+ */
+struct rspamd_min_heap *rspamd_min_heap_create (gsize reserved_size);
+
+/**
+ * Pushes an element to the heap. `pri` should be initialized to use this function,
+ * `idx` is used internally by heap interface
+ * @param heap heap structure
+ * @param elt element to push
+ */
+void rspamd_min_heap_push (struct rspamd_min_heap *heap,
+ struct rspamd_min_heap_elt *elt);
+
+/**
+ * Pops the minimum element from the heap and reorder the queue
+ * @param heap heap structure
+ * @return minimum element
+ */
+struct rspamd_min_heap_elt* rspamd_min_heap_pop (struct rspamd_min_heap *heap);
+
+/**
+ * Updates priority for the element. It must be in queue (so `idx` should be sane)
+ * @param heap heap structure
+ * @param elt element to update
+ * @param npri new priority
+ */
+void rspamd_min_heap_update_elt (struct rspamd_min_heap *heap,
+ struct rspamd_min_heap_elt *elt, guint npri);
+
+/**
+ * Destroys heap (elements are not destroyed themselves)
+ * @param heap
+ */
+void rspamd_min_heap_destroy (struct rspamd_min_heap *heap);
+
+#endif /* SRC_LIBUTIL_HEAP_H_ */