123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- #ifndef _LSUP_TRIPLE_H
- #define _LSUP_TRIPLE_H
- #include "term.h"
- typedef struct LSUP_Triple {
- LSUP_Term *s;
- LSUP_Term *p;
- LSUP_Term *o;
- } LSUP_Triple;
- typedef enum {
- TRP_POS_S = 0,
- TRP_POS_P = 1,
- TRP_POS_O = 2,
- } LSUP_TriplePos;
- LSUP_Triple *
- LSUP_triple_new(LSUP_Term *s, LSUP_Term *p, LSUP_Term *o);
- #define TRP_DUMMY LSUP_triple_new (NULL, NULL, NULL)
- LSUP_Triple *
- LSUP_triple_new_from_btriple (const LSUP_BufferTriple *sspo);
- LSUP_BufferTriple *
- LSUP_btriple_new_from_triple (const LSUP_Triple *spo);
- /** @brief Initialize internal term pointers in a heap-allocated triple.
- *
- * NOTE: the term structures are not copied. If the triple is freed with
- * #LSUP_triple_free(), the originally provided terms are freed too.
- *
- * @param spo[in] Triple pointer to initialize.
- */
- LSUP_rc
- LSUP_triple_init (LSUP_Triple *spo, LSUP_Term *s, LSUP_Term *p, LSUP_Term *o);
- /** @brief Free the internal pointers of a triple.
- *
- * @param spo[in] Triple to be freed.
- */
- void
- LSUP_triple_done (LSUP_Triple *spo);
- /** @brief Free a triple and all its internal pointers.
- *
- * NOTE: If the term pointers are not to be freed (e.g. they are owned by a
- * back end), use a simple free(spo) instead of this.
- *
- * @param spo[in] Triple to be freed.
- */
- void
- LSUP_triple_free (LSUP_Triple *spo);
- #define _FN_BODY \
- if (n == TRP_POS_S) return trp->s; \
- if (n == TRP_POS_P) return trp->p; \
- if (n == TRP_POS_O) return trp->o; \
- return NULL;
- /** @brief Get triple by term position.
- *
- * Useful for looping over all terms.
- *
- * @param trp[in] Triple pointer.
- *
- * @param n[in] A number between 0÷2.
- *
- * @return Corresponding triple term or NULL if n is out of range.
- */
- inline LSUP_Term *
- LSUP_triple_pos (const LSUP_Triple *trp, LSUP_TriplePos n)
- { _FN_BODY }
- /** @brief Get serialized triple by term position.
- *
- * Useful for looping over all terms.
- *
- * @param trp[in] Serialized triple pointer.
- *
- * @param n[in] A number between 0÷2.
- *
- * @return Corresponding serialized term or NULL if n is out of range.
- */
- inline LSUP_Buffer *
- LSUP_btriple_pos (const LSUP_BufferTriple *trp, LSUP_TriplePos n)
- { _FN_BODY }
- #undef _FN_BODY
- /** @brief Hash a triple.
- *
- * TODO This doesn't handle blank nodes correctly.
- */
- inline LSUP_Key
- LSUP_triple_hash (const LSUP_Triple *trp)
- {
- LSUP_BufferTriple *strp = LSUP_btriple_new_from_triple (trp);
- LSUP_Key hash = LSUP_btriple_hash (strp);
- LSUP_btriple_free (strp);
- return hash;
- }
- #endif
|