Re: implemented missing bitSetBit() and bitGetBit()

Lists: pgsql-hackers
From: David Helgason <david(at)uti(dot)is>
To: pgsql-hackers(at)postgresql(dot)org
Subject: implemented missing bitSetBit() and bitGetBit()
Date: 2004-01-23 00:59:03
Message-ID: 56656C0E-4D3F-11D8-9552-000A9566DA8A@uti.is
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

I needed these, so I went and implemented them myself. I have to admit
I'm not so wise on PostgreSQL backend stuff, and so I abstained from
editing the fmgrtab.c and postgres.bki.

Sorry for that, but it just seemed too complicated. Perhaps there
should really be a script to do this? I could imagine I'm not the only
one slightly daunted by these files...

There's seems to be no reason for changing the docs since the functions
are documented as existing :)
I didn't add the other functions that one might expect to exist
(get-/set_byte and others), since I don't really need them.

I tested this code under 7.4 since that's what I've got here, but
imagine nothing much changed in this end of the world for 7.4.1...

This is to be appended to src/backend/utils/adt/varbit.c:

/*-------------------------------------------------------------
* bitSetBit
*
* Given an instance of type 'bit' creates a new one with
* the Nth bit set to the given value.
*
*-------------------------------------------------------------
*/
PG_FUNCTION_INFO_V1(bitSetBit);
Datum bitSetBit(PG_FUNCTION_ARGS)
{
VarBit *arg1 = PG_GETARG_VARBIT_P(0);
int32 n = PG_GETARG_INT32(1);
int32 newBit = PG_GETARG_INT32(2);
VarBit *result;
int bitlen,
bytelen,
byteNo,
bitNo;
unsigned char oldByte,
newByte;

bitlen = VARBITLEN(arg1);

/*
* sanity checks!
*/
if (newBit != 0 && newBit != 1)
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("new bit must be 0 or 1")));

if (n < 0 || n >= bitlen)
ereport(ERROR,
(errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
errmsg("bit %d outside of valid range, 0..%d",
n, bitlen - 1)));

/* Copy input bitstring */
bytelen = VARSIZE(arg1);
result = (VarBit *) palloc(bytelen);
memcpy(VARBITS(result), VARBITS(arg1), VARBITBYTES(arg1));
VARATT_SIZEP(result) = bytelen;
VARBITLEN(result) = bitlen;

/*
* Update the bit.
*/
byteNo = n / 8;
bitNo = 7 - (n % 8);

oldByte = ((unsigned char *) VARBITS(result))[byteNo];

if (newBit == 0)
newByte = oldByte & (~(1 << bitNo));
else
newByte = oldByte | (1 << bitNo);

((unsigned char *) VARBITS(result))[byteNo] = newByte;
PG_RETURN_VARBIT_P(result);
}

/*-------------------------------------------------------------
* bitGetBit
*
* Given an instance of type 'bit' returns the Nth bit.
*
*-------------------------------------------------------------
*/
PG_FUNCTION_INFO_V1(bitGetBit);
Datum bitGetBit(PG_FUNCTION_ARGS)
{
VarBit *arg1 = PG_GETARG_VARBIT_P(0);
int32 n = PG_GETARG_INT32(1);
int bitlen,
bytelen,
byteNo,
bitNo;
unsigned char theByte;

bitlen = VARBITLEN(arg1);

/*
* sanity check!
*/
if (n < 0 || n >= bitlen)
ereport(ERROR,
(errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
errmsg("bit %d outside of valid range, 0..%d",
n, bitlen - 1)));

/*
* Find the target bit
*/
byteNo = n / BITS_PER_BYTE;
bitNo = BITS_PER_BYTE - 1 - (n % BITS_PER_BYTE);

theByte = ((unsigned char *) VARBITS(arg1))[byteNo];

/*
* Shift a set bit to target position, & with the target byte, shift
back
* to get integer 0 or 1
*/
PG_RETURN_INT32((int)(theByte & (1 << bitNo)) >> bitNo);
}


From: Neil Conway <neilc(at)samurai(dot)com>
To: David Helgason <david(at)uti(dot)is>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: implemented missing bitSetBit() and bitGetBit()
Date: 2004-02-04 19:22:05
Message-ID: 87isim1wgy.fsf@mailbox.samurai.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

David Helgason <david(at)uti(dot)is> writes:
> I needed these, so I went and implemented them myself.

I didn't see any followup to this: do we want to include this in the
main tree, contrib/, or not at all?

-Neil (who has no opinion on the matter, but just wants to make sure
this doesn't fall through the cracks)


From: Peter Eisentraut <peter_e(at)gmx(dot)net>
To: Neil Conway <neilc(at)samurai(dot)com>, David Helgason <david(at)uti(dot)is>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: implemented missing bitSetBit() and bitGetBit()
Date: 2004-02-04 19:51:33
Message-ID: 200402042051.33509.peter_e@gmx.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Neil Conway wrote:
> David Helgason <david(at)uti(dot)is> writes:
> > I needed these, so I went and implemented them myself.
>
> I didn't see any followup to this: do we want to include this in the
> main tree, contrib/, or not at all?

getbit sounds a lot like what substring() does. So perhaps setbit could
actually be handled by replace()? That would be a more general
solution (since it would handle more than one bit at a time).


From: David Helgason <david(at)uti(dot)is>
To: Peter Eisentraut <peter_e(at)gmx(dot)net>
Cc: Neil Conway <neilc(at)samurai(dot)com>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: implemented missing bitSetBit() and bitGetBit()
Date: 2004-02-04 21:02:02
Message-ID: 612783B2-5755-11D8-9EFF-000A9566DA8A@uti.is
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On 4. feb 2004, at 20:51, Peter Eisentraut wrote:
> Neil Conway wrote:
>> David Helgason <david(at)uti(dot)is> writes:
>>> I needed these, so I went and implemented them myself.
>>
>> I didn't see any followup to this: do we want to include this in the
>> main tree, contrib/, or not at all?
> getbit sounds a lot like what substring() does. So perhaps setbit
> could
> actually be handled by replace()? That would be a more general
> solution (since it would handle more than one bit at a time).

I sort of agree, but it's currently documented like I implemented it
(afaics), so it's a simple thing to include.

I feel a bit bad for not having done a full patch with test-cases and
.bki modifications etc., but it seemed a pretty daunting task (for my
schedule at least).

Hope someone can use it though.

David Helgason,
Over the Edge Entertainments


From: Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)us>
To: Peter Eisentraut <peter_e(at)gmx(dot)net>
Cc: Neil Conway <neilc(at)samurai(dot)com>, David Helgason <david(at)uti(dot)is>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: implemented missing bitSetBit() and bitGetBit()
Date: 2004-02-12 16:05:18
Message-ID: 200402121605.i1CG5IU25221@candle.pha.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Peter Eisentraut wrote:
> Neil Conway wrote:
> > David Helgason <david(at)uti(dot)is> writes:
> > > I needed these, so I went and implemented them myself.
> >
> > I didn't see any followup to this: do we want to include this in the
> > main tree, contrib/, or not at all?
>
> getbit sounds a lot like what substring() does. So perhaps setbit could
> actually be handled by replace()? That would be a more general
> solution (since it would handle more than one bit at a time).

Added to TODO:

* Allow substring/replace() to get/set bit values

--
Bruce Momjian | http://candle.pha.pa.us
pgman(at)candle(dot)pha(dot)pa(dot)us | (610) 359-1001
+ If your life is a hard drive, | 13 Roberts Road
+ Christ can be your backup. | Newtown Square, Pennsylvania 19073