priority_queue.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * Copyright 2022 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #ifndef OSSL_INTERNAL_PRIORITY_QUEUE_H
  10. #define OSSL_INTERNAL_PRIORITY_QUEUE_H
  11. #pragma once
  12. #include <stdlib.h>
  13. #include <openssl/e_os2.h>
  14. #define PRIORITY_QUEUE_OF(type) OSSL_PRIORITY_QUEUE_##type
  15. #define DEFINE_PRIORITY_QUEUE_OF_INTERNAL(type, ctype) \
  16. typedef struct ossl_priority_queue_st_##type PRIORITY_QUEUE_OF(type); \
  17. static ossl_unused ossl_inline PRIORITY_QUEUE_OF(type) * ossl_pqueue_##type##_new(int (*compare)(const ctype *, const ctype *)) \
  18. { \
  19. return (PRIORITY_QUEUE_OF(type) *)ossl_pqueue_new( \
  20. (int (*)(const void *, const void *))compare); \
  21. } \
  22. static ossl_unused ossl_inline void \
  23. ossl_pqueue_##type##_free(PRIORITY_QUEUE_OF(type) * pq) \
  24. { \
  25. ossl_pqueue_free((OSSL_PQUEUE *)pq); \
  26. } \
  27. static ossl_unused ossl_inline void \
  28. ossl_pqueue_##type##_pop_free(PRIORITY_QUEUE_OF(type) * pq, \
  29. void (*freefunc)(ctype *)) \
  30. { \
  31. ossl_pqueue_pop_free((OSSL_PQUEUE *)pq, (void (*)(void *))freefunc); \
  32. } \
  33. static ossl_unused ossl_inline int \
  34. ossl_pqueue_##type##_reserve(PRIORITY_QUEUE_OF(type) * pq, size_t n) \
  35. { \
  36. return ossl_pqueue_reserve((OSSL_PQUEUE *)pq, n); \
  37. } \
  38. static ossl_unused ossl_inline size_t \
  39. ossl_pqueue_##type##_num(const PRIORITY_QUEUE_OF(type) * pq) \
  40. { \
  41. return ossl_pqueue_num((OSSL_PQUEUE *)pq); \
  42. } \
  43. static ossl_unused ossl_inline int \
  44. ossl_pqueue_##type##_push(PRIORITY_QUEUE_OF(type) * pq, \
  45. ctype * data, size_t *elem) \
  46. { \
  47. return ossl_pqueue_push((OSSL_PQUEUE *)pq, (void *)data, elem); \
  48. } \
  49. static ossl_unused ossl_inline ctype * \
  50. ossl_pqueue_##type##_peek(const PRIORITY_QUEUE_OF(type) * pq) \
  51. { \
  52. return (type *)ossl_pqueue_peek((OSSL_PQUEUE *)pq); \
  53. } \
  54. static ossl_unused ossl_inline ctype * \
  55. ossl_pqueue_##type##_pop(PRIORITY_QUEUE_OF(type) * pq) \
  56. { \
  57. return (type *)ossl_pqueue_pop((OSSL_PQUEUE *)pq); \
  58. } \
  59. static ossl_unused ossl_inline ctype * \
  60. ossl_pqueue_##type##_remove(PRIORITY_QUEUE_OF(type) * pq, \
  61. size_t elem) \
  62. { \
  63. return (type *)ossl_pqueue_remove((OSSL_PQUEUE *)pq, elem); \
  64. } \
  65. struct ossl_priority_queue_st_##type
  66. #define DEFINE_PRIORITY_QUEUE_OF(type) \
  67. DEFINE_PRIORITY_QUEUE_OF_INTERNAL(type, type)
  68. typedef struct ossl_pqueue_st OSSL_PQUEUE;
  69. OSSL_PQUEUE *ossl_pqueue_new(int (*compare)(const void *, const void *));
  70. void ossl_pqueue_free(OSSL_PQUEUE *pq);
  71. void ossl_pqueue_pop_free(OSSL_PQUEUE *pq, void (*freefunc)(void *));
  72. int ossl_pqueue_reserve(OSSL_PQUEUE *pq, size_t n);
  73. size_t ossl_pqueue_num(const OSSL_PQUEUE *pq);
  74. int ossl_pqueue_push(OSSL_PQUEUE *pq, void *data, size_t *elem);
  75. void *ossl_pqueue_peek(const OSSL_PQUEUE *pq);
  76. void *ossl_pqueue_pop(OSSL_PQUEUE *pq);
  77. void *ossl_pqueue_remove(OSSL_PQUEUE *pq, size_t elem);
  78. #endif