circular-buffer

C /C++ Lock-free (or nonblocking) Ring Buffer that OVERWRITES oldest data?

断了今生、忘了曾经 提交于 2019-12-05 02:08:47
I'm trying to find a way to make a Lock Free OR Non-blocking way to make a Ring Buffer for single consumer / single consumer that will over-write the oldest data int the buffer. I've read a lot of lock-free algorithms that work when you "return false" if the buffer is full--ie, don't add; but I can't find even pseudo-code that talks about how to do it when you need to overwrite the oldest data. I am using GCC 4.1.2 (restriction at work, i can't upgrade the version...) and I have the Boost libraries, and in the past I made my own Atomic< T > variable type that follows pretty closely to the

Efficient circular list

可紊 提交于 2019-12-04 20:06:20
问题 I want a simple yet efficient circular buffer/queue. If I use std::vector , I have to do this: if ( v.size() >= limit ) { std::vector<int> it = v.begin(); v.insert( it, data ); v.erase( it+1 ); } Is there any simpler solution? 回答1: You want to maintain the size of the buffer, overwriting older items. Just overwrite the old ones as time goes on. If you want to deal with the case where nItems < limit, then you would need to deal with that, this is just a simple example of using modulo to insert

a text file circular buffer in python

好久不见. 提交于 2019-12-04 06:31:17
I need a python script implementing a circular buffer for rows in a text file limited to N rows like this: row 1 -> pop row 2 row 3 | | push -> row N What's the best solution? EDIT: This script should create and maintain the text file which only contains the latest N lines. Then it should pop the first line pushed in. Like a fifo buffer. Try my recipe and sorry for italian usage: #!/usr/bin/env python # -*- coding: utf-8 -*- # # fifo(.py) # # Copyright 2011 Fabio Di Bernardini <fdb@altraqua.com> # # This program is free software; you can redistribute it and/or modify # it under the terms of

ring buffer with numpy/ctypes

南楼画角 提交于 2019-12-04 06:05:12
I'm developing a client which will receive the [EEG] data over tcp and write it to the ring buffer. I thought it can be very convenient to have the buffer as a ctypes or numpy array because it's possible to create a numpy 'view' to any location of such buffer and read/write/process the data without any copying operations. Or is it a bad idea in general? However, I don't see how to implement a circular buffer of a fixed size this way. Suppose I have created a buffer object which is contiguous in memory. What is the best way to write the data when the end of the buffer is reached? One possible

How to find the number of the lexicographically minimal string rotation?

眉间皱痕 提交于 2019-12-04 04:57:35
How to find the number of lexicographically minimal string rotation ? For example: S = abab, N = 2 S = abca, N = 1 S = aaaa, N = 4 I tried Duval's algorithm, it works very long. The string length of 100000000 characters. Easy -- just determine the minimum period of the string. A string which is periodic in minimal period K will produce identical (and hence lexicographically equal) strings for exactly N/K different rotations, so whatever the lexicographic minimum is, it'll be the result of N/K different rotations. 来源: https://stackoverflow.com/questions/21026091/how-to-find-the-number-of-the

How do you iterate backward over circular buffer without a conditional?

时光总嘲笑我的痴心妄想 提交于 2019-12-04 00:02:44
Iterating forward through a circular buffer without using a conditional is easy with the remainder operator... iterator = (iterator + 1) % buffer_size; I can't for the life of me figure out the reverse operation, iterating backward. Does iterator = (iterator + buffer_size - 1) % buffer_size work for you? Go one less than all the way around. Borealid's answer works. (note: iterator is set to 0 initially). Another solution is iterator = buffer_size - 1 - (buffer_size - iterator) % buffer_size with iterator set to buffer_size initially. 来源: https://stackoverflow.com/questions/3437477/how-do-you

Circular Buffer in Flash

末鹿安然 提交于 2019-12-03 20:05:14
问题 I need to store items of varying length in a circular queue in a flash chip. Each item will have its encapsulation so I can figure out how big it is and where the next item begins. When there are enough items in the buffer, it will wrap to the beginning. What is a good way to store a circular queue in a flash chip? There is a possibility of tens of thousands of items I would like to store. So starting at the beginning and reading to the end of the buffer is not ideal because it will take time

scala collections circular buffer

徘徊边缘 提交于 2019-12-03 14:33:37
Just messing about here, with circular buffers. Is this a sensible implementation or is there a faster/more reliable way to skin this cat? class CircularBuffer[T](size: Int)(implicit mf: Manifest[T]) { private val arr = new scala.collection.mutable.ArrayBuffer[T]() private var cursor = 0 val monitor = new ReentrantReadWriteLock() def push(value: T) { monitor.writeLock().lock() try { arr(cursor) = value cursor += 1 cursor %= size } finally { monitor.writeLock().unlock() } } def getAll: Array[T] = { monitor.readLock().lock() try { val copy = new Array[T](size) arr.copyToArray(copy) copy }

Efficient circular list

懵懂的女人 提交于 2019-12-03 12:45:13
I want a simple yet efficient circular buffer/queue. If I use std::vector , I have to do this: if ( v.size() >= limit ) { std::vector<int> it = v.begin(); v.insert( it, data ); v.erase( it+1 ); } Is there any simpler solution? You want to maintain the size of the buffer, overwriting older items. Just overwrite the old ones as time goes on. If you want to deal with the case where nItems < limit, then you would need to deal with that, this is just a simple example of using modulo to insert into a fixed size buffer. std::vector<int> data(10); for (int i = 0 ; i < 100 ; ++i) { data[i%10] = i; }

How do I code a simple integer circular buffer in C/C++?

瘦欲@ 提交于 2019-12-03 11:16:49
问题 I see a lot of templates and complicated data structures for implementing a circular buffer. How do I code a simple integer circular buffer for 5 numbers? I'm thinking in C is the most straightforward? Thanks. 回答1: Have an array, buffer , of 5 integers. Have an index ind to the next element. When you add, do buffer[ind] = value; ind = (ind + 1) % 5; 回答2: Take an array, arr , an index idx , and a counter, num . To insert foo , say arr[idx++] = foo; idx %= buffer_len; num++; . To read out an