triple.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #ifndef _LSUP_TRIPLE_H
  2. #define _LSUP_TRIPLE_H
  3. #include "term.h"
  4. typedef struct LSUP_Triple {
  5. LSUP_Term *s;
  6. LSUP_Term *p;
  7. LSUP_Term *o;
  8. } LSUP_Triple;
  9. typedef enum {
  10. TRP_POS_S = 0,
  11. TRP_POS_P = 1,
  12. TRP_POS_O = 2,
  13. } LSUP_TriplePos;
  14. LSUP_Triple *
  15. LSUP_triple_new(LSUP_Term *s, LSUP_Term *p, LSUP_Term *o);
  16. #define TRP_DUMMY LSUP_triple_new (NULL, NULL, NULL)
  17. LSUP_Triple *
  18. LSUP_triple_new_from_btriple (const LSUP_BufferTriple *sspo);
  19. LSUP_BufferTriple *
  20. LSUP_btriple_new_from_triple (const LSUP_Triple *spo);
  21. /** @brief Initialize internal term pointers in a heap-allocated triple.
  22. *
  23. * NOTE: the term structures are not copied. If the triple is freed with
  24. * #LSUP_triple_free(), the originally provided terms are freed too.
  25. *
  26. * @param spo[in] Triple pointer to initialize.
  27. */
  28. LSUP_rc
  29. LSUP_triple_init (LSUP_Triple *spo, LSUP_Term *s, LSUP_Term *p, LSUP_Term *o);
  30. /** @brief Free the internal pointers of a triple.
  31. *
  32. * @param spo[in] Triple to be freed.
  33. */
  34. void
  35. LSUP_triple_done (LSUP_Triple *spo);
  36. /** @brief Free a triple and all its internal pointers.
  37. *
  38. * NOTE: If the term pointers are not to be freed (e.g. they are owned by a
  39. * back end), use a simple free(spo) instead of this.
  40. *
  41. * @param spo[in] Triple to be freed.
  42. */
  43. void
  44. LSUP_triple_free (LSUP_Triple *spo);
  45. #define _FN_BODY \
  46. if (n == TRP_POS_S) return trp->s; \
  47. if (n == TRP_POS_P) return trp->p; \
  48. if (n == TRP_POS_O) return trp->o; \
  49. return NULL;
  50. /** @brief Get triple by term position.
  51. *
  52. * Useful for looping over all terms.
  53. *
  54. * @param trp[in] Triple pointer.
  55. *
  56. * @param n[in] A number between 0÷2.
  57. *
  58. * @return Corresponding triple term or NULL if n is out of range.
  59. */
  60. inline LSUP_Term *
  61. LSUP_triple_pos (const LSUP_Triple *trp, LSUP_TriplePos n)
  62. { _FN_BODY }
  63. /** @brief Get serialized triple by term position.
  64. *
  65. * Useful for looping over all terms.
  66. *
  67. * @param trp[in] Serialized triple pointer.
  68. *
  69. * @param n[in] A number between 0÷2.
  70. *
  71. * @return Corresponding serialized term or NULL if n is out of range.
  72. */
  73. inline LSUP_Buffer *
  74. LSUP_btriple_pos (const LSUP_BufferTriple *trp, LSUP_TriplePos n)
  75. { _FN_BODY }
  76. #undef _FN_BODY
  77. /** @brief Hash a triple.
  78. *
  79. * TODO This doesn't handle blank nodes correctly.
  80. */
  81. inline LSUP_Key
  82. LSUP_triple_hash (const LSUP_Triple *trp)
  83. {
  84. LSUP_BufferTriple *strp = LSUP_btriple_new_from_triple (trp);
  85. LSUP_Key hash = LSUP_btriple_hash (strp);
  86. LSUP_btriple_free (strp);
  87. return hash;
  88. }
  89. #endif