123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196 |
- #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 struct LSUP_SerTriple {
- LSUP_Buffer *s;
- LSUP_Buffer *p;
- LSUP_Buffer *o;
- } LSUP_SerTriple;
- 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 (TERM_DUMMY, TERM_DUMMY, TERM_DUMMY)
- LSUP_SerTriple *
- LSUP_striple_new(LSUP_Buffer *s, LSUP_Buffer *p, LSUP_Buffer *o);
- #define STRP_DUMMY LSUP_striple_new (BUF_DUMMY, BUF_DUMMY, BUF_DUMMY)
- LSUP_Triple *
- LSUP_triple_new_from_striple (const LSUP_SerTriple *sspo);
- LSUP_SerTriple *
- LSUP_striple_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 Initialize internal term pointers in a heap-allocated buffer triple.
- *
- * The triple must be freed with #LSUP_striple_free().
- *
- * @param sspo[in] Serialized triple pointer to initialize.
- */
- LSUP_rc
- LSUP_striple_init (
- LSUP_SerTriple *sspo, LSUP_Buffer *s, LSUP_Buffer *p, LSUP_Buffer *o);
- /** @brief Serialize a RDF triple into a buffer triple.
- *
- * The internal structure must be freed with #LSUP_striple_done after use.
- *
- * @param spo[in] Triple to be serialized.
- * @param sspo[out] Buffer triple handle. It must point to an already allocated
- * structure.
- *
- * @return LSUP_OK or an error code resulting from #LSUP_term_serialize.
- */
- LSUP_rc
- LSUP_triple_serialize (const LSUP_Triple *spo, LSUP_SerTriple *sspo);
- /** @brief Deserialize a buffer triple into a RDF triple.
- *
- * The internal structure must be freed with #LSUP_triple_done after use.
- *
- * @param sspo[in] Buffer triple to be serialized.
- * @param spo[out] RDF triple handle. It must point to an already allocated
- * structure.
- *
- * @return LSUP_OK or an error code resulting from #LSUP_term_deserialize.
- */
- LSUP_rc
- LSUP_triple_deserialize (const LSUP_SerTriple *sspo, LSUP_Triple *spo);
- /** @brief Free the internal pointers of a triple.
- *
- * @param spo[in] Triple to be freed.
- */
- void
- LSUP_triple_done (LSUP_Triple *spo);
- /** @brief Free the internal pointers of a buffer triple.
- *
- * @param sspo[in] Buffer triple to be freed.
- */
- void
- LSUP_striple_done (LSUP_SerTriple *sspo);
- /** @brief Free a triple and all its internal pointers.
- *
- * @param spo[in] Triple to be freed.
- */
- void
- LSUP_triple_free (LSUP_Triple *spo);
- /** @brief Free a buffer triple and all its internal pointers.
- *
- * @param sspo[in] Buffer triple to be freed.
- */
- void
- LSUP_striple_free (LSUP_SerTriple *sspo);
- #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_striple_pos (const LSUP_SerTriple *trp, LSUP_TriplePos n)
- { _FN_BODY }
- #undef _FN_BODY
- /** @brief Hash a buffer triple.
- *
- * TODO This doesn't handle blank nodes correctly.
- */
- inline LSUP_Key
- LSUP_striple_hash (const LSUP_SerTriple *strp)
- {
- return XXH64 (
- strp->s->addr, strp->s->size,
- XXH64 (
- strp->p->addr, strp->p->size,
- XXH64 (strp->o->addr, strp->o->size, HASH_SEED)
- )
- );
- }
- /** @brief Hash a triple.
- *
- * TODO This doesn't handle blank nodes correctly.
- */
- inline LSUP_Key
- LSUP_triple_hash (const LSUP_Triple *trp)
- {
- LSUP_SerTriple *strp = LSUP_striple_new_from_triple (trp);
- LSUP_Key hash = LSUP_striple_hash (strp);
- LSUP_striple_free (strp);
- return hash;
- }
- #endif
|