PostGIS  3.0.6dev-r@@SVN_REVISION@@

◆ gserialized_cmp()

int gserialized_cmp ( const GSERIALIZED g1,
const GSERIALIZED g2 
)

Return -1 if g1 is "less than" g2, 1 if g1 is "greater than" g2 and 0 if g1 and g2 are the "same".

Equality is evaluated with a memcmp and size check. So it is possible that two identical objects where one lacks a bounding box could be evaluated as non-equal initially. Greater and less than are evaluated by calculating a sortable key from the center point of the object bounds.

Definition at line 313 of file gserialized.c.

314 {
315  GBOX box1, box2;
316  uint64_t hash1, hash2;
317  size_t sz1 = SIZE_GET(g1->size);
318  size_t sz2 = SIZE_GET(g2->size);
319  size_t hsz1 = gserialized_header_size(g1);
320  size_t hsz2 = gserialized_header_size(g2);
321  uint8_t *b1 = (uint8_t*)g1 + hsz1;
322  uint8_t *b2 = (uint8_t*)g2 + hsz2;
323  size_t bsz1 = sz1 - hsz1;
324  size_t bsz2 = sz2 - hsz2;
325  size_t bsz_min = bsz1 < bsz2 ? bsz1 : bsz2;
326 
327  /* Equality fast path */
328  /* Return equality for perfect equality only */
329  int cmp_srid = gserialized_cmp_srid(g1, g2);
330  int cmp = memcmp(b1, b2, bsz_min);
331  int g1hasz = gserialized_has_z(g1);
332  int g1hasm = gserialized_has_m(g1);
333  int g2hasz = gserialized_has_z(g2);
334  int g2hasm = gserialized_has_m(g2);
335 
336  if (bsz1 == bsz2 && cmp_srid == 0 && cmp == 0 && g1hasz == g2hasz && g1hasm == g2hasm)
337  return 0;
338  else
339  {
340  int g1_is_empty = (gserialized_get_gbox_p(g1, &box1) == LW_FAILURE);
341  int g2_is_empty = (gserialized_get_gbox_p(g2, &box2) == LW_FAILURE);
342  int32_t srid1 = gserialized_get_srid(g1);
343  int32_t srid2 = gserialized_get_srid(g2);
344 
345  /* Empty < Non-empty */
346  if (g1_is_empty && !g2_is_empty)
347  return -1;
348 
349  /* Non-empty > Empty */
350  if (!g1_is_empty && g2_is_empty)
351  return 1;
352 
353  if (!g1_is_empty && !g2_is_empty)
354  {
355  /* Using the boxes, calculate sortable hash key. */
356  hash1 = gbox_get_sortable_hash(&box1, srid1);
357  hash2 = gbox_get_sortable_hash(&box2, srid2);
358 
359  if (hash1 > hash2)
360  return 1;
361  if (hash1 < hash2)
362  return -1;
363  }
364 
365  /* Prefix comes before longer one. */
366  if (bsz1 != bsz2 && cmp == 0)
367  {
368  if (bsz1 < bsz2)
369  return -1;
370  else if (bsz1 > bsz2)
371  return 1;
372  }
373 
374  /* If SRID is not equal, sort on it */
375  if (cmp_srid != 0)
376  return (srid1 > srid2) ? 1 : -1;
377 
378  /* ZM flag sort*/
379  if (g1hasz != g2hasz)
380  return (g1hasz > g2hasz) ? 1 : -1;
381 
382  if (g1hasm != g2hasm)
383  return (g1hasm > g2hasm) ? 1 : -1;
384 
385  assert(cmp != 0);
386  return cmp > 0 ? 1 : -1;
387  }
388 }
uint64_t gbox_get_sortable_hash(const GBOX *g, const int32_t srid)
Return a sortable key based on the center point of the GBOX.
Definition: gbox.c:893
int32_t gserialized_get_srid(const GSERIALIZED *g)
Extract the SRID from the serialized form (it is packed into three bytes so this is a handy function)...
Definition: gserialized.c:126
static size_t gserialized_header_size(const GSERIALIZED *g)
Definition: gserialized.c:278
int gserialized_get_gbox_p(const GSERIALIZED *g, GBOX *gbox)
Read the box from the GSERIALIZED or calculate it if necessary.
Definition: gserialized.c:65
int gserialized_has_m(const GSERIALIZED *g)
Check if a GSERIALIZED has an M ordinate.
Definition: gserialized.c:185
static int gserialized_cmp_srid(const GSERIALIZED *g1, const GSERIALIZED *g2)
Definition: gserialized.c:303
int gserialized_has_z(const GSERIALIZED *g)
Check if a GSERIALIZED has a Z ordinate.
Definition: gserialized.c:174
#define LW_FAILURE
Definition: liblwgeom.h:110
#define SIZE_GET(varsize)
Macro for reading the size from the GSERIALIZED size attribute.
uint32_t size
Definition: liblwgeom.h:430

References gbox_get_sortable_hash(), gserialized_cmp_srid(), gserialized_get_gbox_p(), gserialized_get_srid(), gserialized_has_m(), gserialized_has_z(), gserialized_header_size(), LW_FAILURE, GSERIALIZED::size, and SIZE_GET.

Referenced by geography_cmp(), geography_eq(), geography_ge(), geography_gt(), geography_le(), geography_lt(), lwgeom_cmp(), lwgeom_cmp_full(), lwgeom_eq(), lwgeom_ge(), lwgeom_gt(), lwgeom_le(), lwgeom_lt(), and LWGEOM_same().

Here is the call graph for this function:
Here is the caller graph for this function: