Welcome to mirror list, hosted at ThFree Co, Russian Federation.

round_buffer.cpp « utils - github.com/thirdpin/libopencm3_cpp_extensions.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e554d9c255b592fd5100b0a415fd2a1ed2ec9742 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
 * This file is part of the libopencm3_cpp_extensions project.
 * hosted at http://github.com/thirdpin/libopencm3_cpp_extensions
 *
 * Copyright (C) 2016  Third Pin LLC
 * Written by Pavel Larionov <p.larionov@thirdpin.ru>
 * Written by Anastasiia Lazareva <a.lazareva@thirdpin.ru>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/*
ROUND BUFFER implementation, public interface
*/

#include "round_buffer.h"

namespace cm3cpp {

namespace utils {

RoundBuffer::RoundBuffer()
{
    init(ROUND_BUFFER_DEFAULT_SIZE);
}

RoundBuffer::RoundBuffer(uint32_t size)
{
    init(size);
}

uint32_t RoundBuffer::get(void* buffer, uint32_t index, uint32_t count)
{
    uint32_t cnt = get_count();
    uint32_t cnt_out = ((count + index) <= cnt) ? (count + index) : cnt;
    for (uint32_t i = index; i < cnt_out; i++)
        ((uint8_t*)buffer)[i - index] = (*this)[i];
    return (cnt_out);
}

bool RoundBuffer::push(void* buffer, uint32_t count)
{
    bool result = true;

    for (uint32_t i = 0; i < count; i++)
        result = push(((uint8_t*)buffer)[i]);
    return result;
}

bool RoundBuffer::push(RoundBuffer* buffer, uint32_t count, bool withPop)
{
    bool result = true;
    uint32_t max_size = buffer->get_count();

    max_size = ((max_size < count) ? max_size : count);

    if (max_size > 0) {
        if (withPop) {
            for (uint32_t i = 0; i < max_size; i++)
                result = push(buffer->pop());
        }

        else {
            for (uint32_t i = 0; i < max_size; i++)
                result = push(buffer->operator[](i));
        }
    }

    return result;
}

uint32_t RoundBuffer::pop(void* buffer, uint32_t count)
{
    uint32_t cnt = get_count();
    uint32_t cnt_out = (count <= cnt) ? count : cnt;

    for (uint32_t i = 0; i < cnt_out; i++)
        ((uint8_t*)buffer)[i] = pop();

    return (cnt_out);
}

uint32_t RoundBuffer::pop(uint32_t count)
{
    uint32_t cnt = get_count();
    uint32_t cnt_out = (count <= cnt) ? count : cnt;

    for (uint32_t i = 0; i < cnt_out; i++)
        pop();

    return (cnt_out);
}

int RoundBuffer::memcmp(void* buffer, uint32_t sizebuf)
{
    uint8_t* buf = (uint8_t*)buffer;
    unsigned int cnt = get_count();

    if (!cnt || !sizebuf || (sizebuf > cnt))
        return (-1);

    uint32_t i = 0;
    while (i < sizebuf && buf[i] == (*this)[i])
        i++;
    if (i == sizebuf)
        return (0);  //???????
    return (i + 1);
}

int RoundBuffer::mem_search(void* buffer, uint32_t sizebuf)
{
    uint32_t cnt = get_count();
    if (cnt < sizebuf)
        return (-1);
    uint8_t* pbuf = (uint8_t*)buffer;
    for (uint32_t i_begin = 0; i_begin <= (cnt - sizebuf); i_begin++) {
        uint32_t i_word;
        uint8_t byte;
        for (i_word = 0; i_word < sizebuf; i_word++) {
            byte = (*this)[i_begin + i_word];
            if (byte != pbuf[i_word]) {
                if (i_word != 0)
                    i_word = 0;
                break;
            }
        }
        if (i_word == sizebuf)
            return (i_begin);
    }
    return (-2);
}

}  // namespace utils

}  // namespace cm3cpp