summaryrefslogtreecommitdiffstats
path: root/src/bitvec.c
diff options
context:
space:
mode:
authorPravin Kumarvel <pmanohar@radisys.com>2016-12-02 15:13:03 +0530
committerHarald Welte <laforge@gnumonks.org>2017-01-06 10:37:42 +0000
commit848de8f1df561f4253f754ec3f2415e541071420 (patch)
treee0d2286ddcbb19791fedc1dd28cbc2609c7b85b4 /src/bitvec.c
parent592fcc97c004b7e3e84ed6960ea9989157c94f75 (diff)
Add function to get uninterrupted bit run
Function bitvec_rl_curbit added to get number of uninterrupted bits run in vector starting from the current bit till max number of bits. Test case is added to check bitvec_rl_curbit. Change-Id: Iae153d3639ea6b891c1fc10d7801a435c9492e26
Diffstat (limited to 'src/bitvec.c')
-rw-r--r--src/bitvec.c45
1 files changed, 45 insertions, 0 deletions
diff --git a/src/bitvec.c b/src/bitvec.c
index 38148acb..c895cffa 100644
--- a/src/bitvec.c
+++ b/src/bitvec.c
@@ -575,6 +575,51 @@ unsigned bitvec_rl(const struct bitvec *bv, bool b)
return bv->cur_bit;
}
+/*! \brief Return number (bits) of uninterrupted bit run in vector
+ * starting from the current bit
+ * \param[in] bv The boolean vector to work on
+ * \param[in] b The boolean, sequence of 1's or 0's to be checked
+ * \param[in] max_bits Total Number of Uncmopresed bits
+ * \returns Number of consecutive bits of \p b in \p bv and cur_bit will
+ * \go to cur_bit + number of consecutive bit
+ */
+unsigned bitvec_rl_curbit(struct bitvec *bv, bool b, int max_bits)
+{
+ unsigned i = 0;
+ unsigned j = 8;
+ int temp_res = 0;
+ int count = 0;
+ unsigned readIndex = bv->cur_bit;
+ unsigned remaining_bits = max_bits % 8;
+ unsigned remaining_bytes = max_bits / 8;
+ unsigned byte_mask = 0xFF;
+
+ if (readIndex % 8) {
+ for (j -= (readIndex % 8) ; j > 0 ; j--) {
+ if (readIndex < max_bits && bitvec_read_field(bv, &readIndex, 1) == b)
+ temp_res++;
+ else {
+ bv->cur_bit--;
+ return temp_res;
+ }
+ }
+ }
+ for (i = (readIndex / 8);
+ i < (remaining_bits ? remaining_bytes + 1 : remaining_bytes);
+ i++, count++) {
+ if ((b ? byte_mask : 0) != bv->data[i]) {
+ bv->cur_bit = (count * 8 +
+ leading_bits(bv->data[i], b) + readIndex);
+ return count * 8 +
+ leading_bits(bv->data[i], b) + temp_res;
+ }
+ }
+ bv->cur_bit = (temp_res + (count * 8)) + readIndex;
+ if (bv->cur_bit > max_bits)
+ bv->cur_bit = max_bits;
+ return (bv->cur_bit - readIndex + temp_res);
+}
+
/*! \brief Shifts bitvec to the left, n MSB bits lost */
void bitvec_shiftl(struct bitvec *bv, unsigned n)
{