vendredi 23 février 2018

Remove in Circular buffers

SO I have this remove function that is supposed to delete an element within the active data of the circular buffer and shift to left the elements. so if my arrays is [[0]45,[1]46,[3]47,[4]48,[5]49] and I remove 47 it should look like [[0]45,[1]46,[3]48,[4]49], but no matter what I do I seem to not make it work.

int SSLColumn::remove(int data)
{
int index = -1;
// traverse array from the start
for (int i = m_start; i < (m_start + m_size); i++)
{
    if (m_data[i % m_capacity] == data)
    {
        index = i % m_capacity;
        cout << "index = " << index << endl;
    }
}
if (index == -1)// if not found
{
    return -1;
}
if (index == m_start) // if its at the start
{
    m_start = (m_start + 1) % m_capacity;
    m_size--;
    return index;
}
if (index == (m_end - 1 + m_capacity) % m_capacity) // if its at the end
{
    m_end = (m_end - 1 ) % m_capacity;
    m_size--;
    return index;
}
if (index > m_start && index < (m_end - 1 + m_capacity) % m_capacity) // if its in the middle it shifts to the left
{
    for (int e = 0; m_start < m_end; e++)
    {

    m_data[e % m_capacity] = m_data[(e + 1 + m_capacity) % m_capacity];
        m_size--;
    }
    return index;
}
}

where m_size is the number of items stored in the circular buffer and m_apacity is how much space was allocated.

How can I get this shift to the left to make it work?

Aucun commentaire:

Enregistrer un commentaire