priority_queue.h 5.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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) * \
  18. ossl_pqueue_##type##_new(int (*compare)(const ctype *, const ctype *)) \
  19. { \
  20. return (PRIORITY_QUEUE_OF(type) *)ossl_pqueue_new( \
  21. (int (*)(const void *, const void *))compare); \
  22. } \
  23. static ossl_unused ossl_inline void \
  24. ossl_pqueue_##type##_free(PRIORITY_QUEUE_OF(type) *pq) \
  25. { \
  26. ossl_pqueue_free((OSSL_PQUEUE *)pq); \
  27. } \
  28. static ossl_unused ossl_inline void \
  29. ossl_pqueue_##type##_pop_free(PRIORITY_QUEUE_OF(type) *pq, \
  30. void (*freefunc)(ctype *)) \
  31. { \
  32. ossl_pqueue_pop_free((OSSL_PQUEUE *)pq, (void (*)(void *))freefunc);\
  33. } \
  34. static ossl_unused ossl_inline int \
  35. ossl_pqueue_##type##_reserve(PRIORITY_QUEUE_OF(type) *pq, size_t n) \
  36. { \
  37. return ossl_pqueue_reserve((OSSL_PQUEUE *)pq, n); \
  38. } \
  39. static ossl_unused ossl_inline size_t \
  40. ossl_pqueue_##type##_num(const PRIORITY_QUEUE_OF(type) *pq) \
  41. { \
  42. return ossl_pqueue_num((OSSL_PQUEUE *)pq); \
  43. } \
  44. static ossl_unused ossl_inline int \
  45. ossl_pqueue_##type##_push(PRIORITY_QUEUE_OF(type) *pq, \
  46. ctype *data, size_t *elem) \
  47. { \
  48. return ossl_pqueue_push((OSSL_PQUEUE *)pq, (void *)data, elem); \
  49. } \
  50. static ossl_unused ossl_inline ctype * \
  51. ossl_pqueue_##type##_peek(const PRIORITY_QUEUE_OF(type) *pq) \
  52. { \
  53. return (type *)ossl_pqueue_peek((OSSL_PQUEUE *)pq); \
  54. } \
  55. static ossl_unused ossl_inline ctype * \
  56. ossl_pqueue_##type##_pop(PRIORITY_QUEUE_OF(type) *pq) \
  57. { \
  58. return (type *)ossl_pqueue_pop((OSSL_PQUEUE *)pq); \
  59. } \
  60. static ossl_unused ossl_inline ctype * \
  61. ossl_pqueue_##type##_remove(PRIORITY_QUEUE_OF(type) *pq, \
  62. size_t elem) \
  63. { \
  64. return (type *)ossl_pqueue_remove((OSSL_PQUEUE *)pq, elem); \
  65. } \
  66. struct ossl_priority_queue_st_ ## type
  67. # define DEFINE_PRIORITY_QUEUE_OF(type) \
  68. DEFINE_PRIORITY_QUEUE_OF_INTERNAL(type, type)
  69. typedef struct ossl_pqueue_st OSSL_PQUEUE;
  70. OSSL_PQUEUE *ossl_pqueue_new(int (*compare)(const void *, const void *));
  71. void ossl_pqueue_free(OSSL_PQUEUE *pq);
  72. void ossl_pqueue_pop_free(OSSL_PQUEUE *pq, void (*freefunc)(void *));
  73. int ossl_pqueue_reserve(OSSL_PQUEUE *pq, size_t n);
  74. size_t ossl_pqueue_num(const OSSL_PQUEUE *pq);
  75. int ossl_pqueue_push(OSSL_PQUEUE *pq, void *data, size_t *elem);
  76. void *ossl_pqueue_peek(const OSSL_PQUEUE *pq);
  77. void *ossl_pqueue_pop(OSSL_PQUEUE *pq);
  78. void *ossl_pqueue_remove(OSSL_PQUEUE *pq, size_t elem);
  79. #endif