From 427a35fba56da56dbba48b439f5b5c5a52a150d0 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Sat, 18 Jul 2020 23:19:57 +0200 Subject: Fixed geek7.c with heap. Renamed vecque.h to vecheap.h --- examples/geek7.c | 16 ++++----- stc/cvecheap.h | 98 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ stc/cvecque.h | 98 -------------------------------------------------------- 3 files changed, 106 insertions(+), 106 deletions(-) create mode 100644 stc/cvecheap.h delete mode 100644 stc/cvecque.h diff --git a/examples/geek7.c b/examples/geek7.c index 4bf57174..60ffdd07 100644 --- a/examples/geek7.c +++ b/examples/geek7.c @@ -1,4 +1,6 @@ /* +https://www.geeksforgeeks.org/find-the-k-smallest-numbers-after-deleting-given-elements/ + Find the k smallest numbers after deleting given elements Given an array of integers, find the k smallest numbers after deleting given elements. In case of repeating elements delete only one instance in the given array for every instance of element present in the array containing the elements to be deleted. Assume that there are at least k elements left in the array after making n deletions. @@ -22,11 +24,11 @@ After inserting all the elements excluding the ones which are to be deleted, Pop #include #include #include -#include +#include declare_CMap(ii, int, int); declare_CVec(i, int); -declare_CVec_heap(i); +declare_CVec_heap(i, >); // Find k minimum element from arr[0..m-1] after deleting // elements from del[0..n-1] @@ -41,7 +43,7 @@ void findElementsAfterDel(int arr[], int m, int del[], cmap_ii_at(&mp, del[i], 0)->value++; } - CVec_i heap = CVec_init; + CVec_i heap = cvec_init; for (int i = 0; i < m; ++i) { @@ -60,17 +62,15 @@ void findElementsAfterDel(int arr[], int m, int del[], // Else push it in the min heap else - cvecheap_i_push(&heap, arr[i]); - - CVecHeap_i Text + cvec_i_heapPush(&heap, arr[i]); } // Print top k elements in the min heap for (int i = 0; i < k; ++i) { - printf("%d ", cvecque_i_top(&heap)); + printf("%d ", cvec_i_heapTop(&heap)); // Pop the top element - cvecque_i_pop(&heap); + cvec_i_heapPop(&heap); } } diff --git a/stc/cvecheap.h b/stc/cvecheap.h new file mode 100644 index 00000000..7867557f --- /dev/null +++ b/stc/cvecheap.h @@ -0,0 +1,98 @@ +/* MIT License + * + * Copyright (c) 2020 Tyge Løvset, NORCE, www.norceresearch.no + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +/* Priority queue using CVec as heap. */ + +#ifndef CVECHEAP__H__ +#define CVECHEAP__H__ + +#include "cvec.h" + +/* Requires declare_CVec(tag, ...) to be declared. cmpOpr is '<' (max-heap) or '>' (min-heap) */ +#define declare_CVec_heap(tag, cmpOpr) \ + \ +STC_API void \ +cvec_##tag##_heapify(CVec_##tag* self); \ +STC_API CVecValue_##tag \ +cvec_##tag##_heapErase(CVec_##tag* self, size_t i); \ +STC_INLINE CVecValue_##tag \ +cvec_##tag##_heapTop(CVec_##tag* self) {return self->data[0];} \ +STC_INLINE CVecValue_##tag \ +cvec_##tag##_heapPop(CVec_##tag* self) {return cvec_##tag##_heapErase(self, 0);} \ +STC_API void \ +cvec_##tag##_heapPush(CVec_##tag* self, CVecValue_##tag value); \ + \ +implement_CVec_heap(tag, cmpOpr) \ +typedef CVec_##tag CVecHeap_##tag + +/* -------------------------- IMPLEMENTATION ------------------------- */ + +#if !defined(STC_HEADER) || defined(STC_IMPLEMENTATION) +#define implement_CVec_heap(tag, cmpOpr) \ + \ +STC_INLINE void \ +_cvec_##tag##_siftDown(CVecValue_##tag* arr, size_t i, size_t n) { \ + size_t r = i, j = i << 1; \ + while (j <= n) { \ + if (j < n && cvec_##tag##_sortCompare(&arr[j], &arr[j + 1]) cmpOpr 0) \ + ++j; \ + if (cvec_##tag##_sortCompare(&arr[r], &arr[j]) cmpOpr 0) { \ + CVecValue_##tag t = arr[r]; arr[r] = arr[j]; arr[r = j] = t; \ + } else \ + return; \ + j <<= 1; \ + } \ +} \ + \ +STC_API CVecValue_##tag \ +cvec_##tag##_heapErase(CVec_##tag* self, size_t i) { \ + CVecValue_##tag ret = self->data[i]; \ + self->data[i] = cvec_##tag##_back(*self); \ + cvec_##tag##_popBack(self); \ + _cvec_##tag##_siftDown(self->data - 1, i + 1, cvec_size(*self)); \ + return ret; \ +} \ + \ +STC_API void \ +cvec_##tag##_heapPush(CVec_##tag* self, CVecValue_##tag value) { \ + cvec_##tag##_pushBack(self, value); \ + size_t n = cvec_size(*self), j = n; \ + CVecValue_##tag *arr = self->data - 1; \ + for (; j > 1 && cvec_##tag##_sortCompare(&arr[j >> 1], &value) cmpOpr 0; j >>= 1) \ + arr[j] = arr[j >> 1]; \ + arr[j] = value; \ +} \ + \ +STC_API void \ +cvec_##tag##_heapify(CVec_##tag* self) { \ + size_t n = cvec_size(*self); \ + CVecValue_##tag *arr = self->data - 1; \ + for (size_t m = n >> 1; m; --m) \ + _cvec_##tag##_siftDown(arr, m, n); \ +} + +#else +#define implement_CVec_heap(tag, cmpOpr) +#endif + +#endif diff --git a/stc/cvecque.h b/stc/cvecque.h deleted file mode 100644 index 26174a5c..00000000 --- a/stc/cvecque.h +++ /dev/null @@ -1,98 +0,0 @@ -/* MIT License - * - * Copyright (c) 2020 Tyge Løvset, NORCE, www.norceresearch.no - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -/* Priority queue using CVec as heap. */ - -#ifndef CVECHEAP__H__ -#define CVECHEAP__H__ - -#include "cvec.h" - -/* Requires declare_CVec(tag, ...) to be declared. cmpOpr is '<' (max-heap) or '>' (min-heap) */ -#define declare_CVec_heap(tag, cmpOpr) \ - \ -STC_API void \ -cvec_##tag##_heapify(CVec_##tag* self); \ -STC_API CVecValue_##tag \ -cvec_##tag##_heapErase(CVec_##tag* self, size_t i); \ -STC_INLINE CVecValue_##tag \ -cvec_##tag##_heapTop(CVec_##tag* self) {return self->data[0];} \ -STC_INLINE CVecValue_##tag \ -cvec_##tag##_heapPop(CVec_##tag* self) {return cvec_##tag##_heapErase(self, 0);} \ -STC_API void \ -cvec_##tag##_heapPush(CVec_##tag* self, CVecValue_##tag value); \ - \ -implement_CVec_heap(tag, cmpOpr) \ -typedef CVec_##tag CVecHeap_##tag - -/* -------------------------- IMPLEMENTATION ------------------------- */ - -#if !defined(STC_HEADER) || defined(STC_IMPLEMENTATION) -#define implement_CVec_heap(tag, cmpOpr) \ - \ -STC_INLINE void \ -_cvec_##tag##_siftDown(CVecValue_##tag* arr, size_t i, size_t n) { \ - size_t r = i, c = i << 1; \ - while (c <= n) { \ - if (c < n && cvec_##tag##_sortCompare(&arr[c], &arr[c + 1]) cmpOpr 0) \ - ++c; \ - if (cvec_##tag##_sortCompare(&arr[r], &arr[c]) cmpOpr 0) { \ - CVecValue_##tag t = arr[r]; arr[r] = arr[c]; arr[r = c] = t; \ - } else \ - return; \ - c <<= 1; \ - } \ -} \ - \ -STC_API CVecValue_##tag \ -cvec_##tag##_heapErase(CVec_##tag* self, size_t i) { \ - CVecValue_##tag ret = self->data[i]; \ - self->data[i] = cvec_##tag##_back(*self); \ - cvec_##tag##_popBack(self); \ - _cvec_##tag##_siftDown(self->data - 1, i + 1, cvec_size(*self)); \ - return ret; \ -} \ - \ -STC_API void \ -cvec_##tag##_heapPush(CVec_##tag* self, CVecValue_##tag value) { \ - cvec_##tag##_pushBack(self, value); \ - size_t n = cvec_size(*self), i = n; \ - CVecValue_##tag *arr = self->data - 1; \ - for (; i > 1 && cvec_##tag##_sortCompare(&arr[i >> 1], &value) cmpOpr 0; i >>= 1) \ - arr[i] = arr[i >> 1]; \ - arr[i] = value; \ -} \ - \ -STC_API void \ -cvec_##tag##_heapify(CVec_##tag* self) { \ - size_t n = cvec_size(*self); \ - CVecValue_##tag *arr = self->data - 1; \ - for (size_t i = n >> 1; i; --i) \ - _cvec_##tag##_siftDown(arr, i, n); \ -} - -#else -#define implement_CVec_heap(tag, cmpOpr) -#endif - -#endif -- cgit v1.2.3