From c1f39437d0ad38d1f8d76f9ebf904faa9a7aaaf6 Mon Sep 17 00:00:00 2001 From: Teodor Sigaev Date: Wed, 10 May 2006 11:39:12 +0000 Subject: [PATCH] Some optimizations by Volkan YAZICI --- contrib/intarray/_int_op.c | 5 +++-- contrib/intarray/_int_tool.c | 31 ++++++++++++++----------------- 2 files changed, 17 insertions(+), 19 deletions(-) diff --git a/contrib/intarray/_int_op.c b/contrib/intarray/_int_op.c index 7a2065bc21..036c357fb0 100644 --- a/contrib/intarray/_int_op.c +++ b/contrib/intarray/_int_op.c @@ -83,8 +83,6 @@ _int_same(PG_FUNCTION_ARGS) if (avoid || bvoid) return (avoid && bvoid) ? TRUE : FALSE; - SORT(a); - SORT(b); na = ARRNELEMS(a); nb = ARRNELEMS(b); da = ARRPTR(a); @@ -94,7 +92,10 @@ _int_same(PG_FUNCTION_ARGS) if (na == nb) { + SORT(a); + SORT(b); result = TRUE; + for (n = 0; n < na; n++) if (da[n] != db[n]) { diff --git a/contrib/intarray/_int_tool.c b/contrib/intarray/_int_tool.c index 480e16ed9f..82ce4b7ac5 100644 --- a/contrib/intarray/_int_tool.c +++ b/contrib/intarray/_int_tool.c @@ -34,7 +34,7 @@ inner_int_contains(ArrayType *a, ArrayType *b) j++; } else - j++; + break; return (n == nb) ? TRUE : FALSE; } @@ -76,13 +76,6 @@ ArrayType * inner_int_union(ArrayType *a, ArrayType *b) { ArrayType *r = NULL; - int na, - nb; - int *da, - *db, - *dr; - int i, - j; CHECKARRVALID(a); CHECKARRVALID(b); @@ -94,31 +87,35 @@ inner_int_union(ArrayType *a, ArrayType *b) if (ARRISVOID(b)) r = copy_intArrayType(a); - if (r) - dr = ARRPTR(r); - else + if (!r) { - na = ARRNELEMS(a); - nb = ARRNELEMS(b); - da = ARRPTR(a); - db = ARRPTR(b); + int na = ARRNELEMS(a), + nb = ARRNELEMS(b); + int *da = ARRPTR(a), + *db = ARRPTR(b); + int i,j, *dr; r = new_intArrayType(na + nb); dr = ARRPTR(r); /* union */ i = j = 0; - while (i < na && j < nb) - if (da[i] < db[j]) + while (i < na && j < nb) { + if (da[i] == db[j]) { + *dr++ = da[i++]; + j++; + } else if (da[i] < db[j]) *dr++ = da[i++]; else *dr++ = db[j++]; + } while (i < na) *dr++ = da[i++]; while (j < nb) *dr++ = db[j++]; + r = resize_intArrayType(r, dr-ARRPTR(r)); } if (ARRNELEMS(r) > 1) -- 2.40.0