std::vector that can be consumed fast - c++

I'd like to be able to consume parts of a buffer until it's empty. I designed my own below, but I'd like to know if there is such thing in the std library already, so I remove the possibilities of implementing my own wrong.
Note that I use std::vector<T> only for automatic storage. I don't do push_back to insert and pop_back to consume because I want the consumption of data to be as fast as possible.
#include <iostream>
#include <vector>
using namespace std;
template <class T>
class Buffer
{
public:
void write(T* buffer, size_t amountToWrite) {
if (amountToWrite <= this->_buffer.capacity()) {
std::copy(buffer, buffer + amountToWrite, this->_buffer.data());
_size += amountToWrite;
} else {
//In fact I wanted to increase capacity, not size. However we don't use the vector functions, so does it mater?
this->_buffer.resize(amountToWrite);
std::copy(buffer, buffer + amountToWrite, this->_buffer.data());
_size += amountToWrite;
}
}
size_t consume(T* bufferToWrite, size_t amountToConsume) {
if (_current==_size) {
//this means we've already read everything. Maybe we should throw or just return 0?
return 0;
}
if (amountToConsume <= this->_size) {
std::copy(this->_buffer.data() + _current, this->_buffer.data() + _current + amountToConsume, bufferToWrite);
_current += amountToConsume;
return amountToConsume;
} else {
size_t remaining = this->_size - amountToConsume;
std::copy(this->_buffer.data() + _current, this->_buffer.data() + _current + remaining, bufferToWrite);
_current += remaining;
return remaining;
}
}
private:
size_t _size = 0;
std::vector<T> _buffer;
size_t _current = 0;
};
int main()
{
Buffer<uint8_t> b;
uint8_t u[] = {1,2,3,4,5,6};
b.write(u, 6);
uint8_t r[3];
size_t consumedBytes = b.consume(r, 3);
std::cout << "consumed " << consumedBytes << std::endl;
for(int i=0; i<3; i++) {
std::cout << (int) r[i];
}
std::cout << std::endl;
size_t consumedBytes2 = b.consume(r, 3);
std::cout << "consumed " << consumedBytes2 << std::endl;
for(int i=0; i<3; i++) {
std::cout << (int) r[i];
}
std::cout << std::endl;
size_t consumedBytes3 = b.consume(r, 3);
std::cout << "consumed " << consumedBytes3 << std::endl;
return 0;
}
Do you see anything wrong in my implementation?
I'm trying to do the safest possible thing while making it fast.

I think you can take advantage of using dequeue from STL.
It implements a doubly linkedlist in which you can read from one side and write from the other side of it. Check this link for more detail explanations.
Also the other best implementation for a buffer is a circular buffer implementation.
I leave a C++ implementation code for a circular buffer.
class MyCircularQueue {
vector<int> mem;
int front;
int rear;
int len;
int cnt;
public:
MyCircularQueue(int k) {
len = k;
mem = vector<int> (k);
front = 0;
rear = -1;
cnt=0;
}
bool enQueue(int value) {
if(isFull())
return false;
rear = (rear+1) %len;
mem[rear]= value;
cnt++;
return true;
}
bool deQueue() {
if (isEmpty())
return false;
front = (front+1) % len;
cnt--;
return true;
}
int Front() {
if (isEmpty())
return -1;
return mem[front];
}
int Rear() {
if (isEmpty())
return -1;
return mem[rear];
}
bool isEmpty() {
return (cnt ==0);
}
bool isFull() {
return ( cnt == len);
}
};

Related

'<' unable to resolve function overload and Parameters mismatch in Class template, Visual Studio compiler

The error pointed out was at the end just before the };
I don't even see any function overloading or any mismatching parameters.
This is simply a queue data structure that I was trying to implement. But unfortunately, I got these compiler errors. I am sharing the whole code so that easily one can help, since neither I have any function overloaded, not even the constructor nor I have used mismatched parameter. I guess image will help out to see the error.
template<class T>
class Queue {
private:
T* box;
int front;
int rear;
int number_Of_Elements;
int capacity;
public:
Queue(int cap = 0) {
capacity = cap;
front = rear = 0;
number_Of_Elements = 0;
}
bool Empty() {
return size == 0;
}
int next(int i) {
return ((i + 1) % capacity);
}
int previous(int i) {
return ((i + (capacity - 1)) % capacity);
}
int get_Number_Of_Elements() {
return number_Of_Elements;
}
void double_Box() {
T* temp = new T[capacity * 2];
for (int i = 0; i < size; i++) {
temp[i] = box[i];
}
front = 0;
rear = number_Of_Elements;
delete[] box;
box = temp;
}
const T& peek() {
T a = box[front];
front = next(front);
return a;
}
void printQueue() {
cout << "Front is at : " << front << endl;
cout << "Rear is at : " << rear << endl;
for (int i = 0; i < number_Of_Elements; i++) {
cout << "box[" << i << "]" << " : " << box[i] << endl;
}
cout << "------------------------" << endl;
}
void Enqueue(const T& data);
const T& Dequeue();
~Queue() {
delete[] box;
}
// error is exactly here -> };
};
Your problem is with size. You haven't declared it.
I think the compiler tries to use some global function called size instead but it fails.
After declaring the capacity member var, add
size_t size;
Demo
The answer is, there were issues, like some of the variables were not even initialized but they were being used, and the memory calculation errors were there. Like a pointer pointing somewhere in memory that actually does not exist.

How to calculate word frequency in a list using C++?

So I have some words in a file. I read them to a List then I'm trying to find the frequency of each word. My problem is that I have to follow a certain implementation for the list which isn't very flexible.
Here's the List class:
const int maxListSize = 50;
template<class T>
class List {
private:
int numberOfElements;
int currentPosition;
T data[maxListSize];
public:
List() {
numberOfElements = 0;
currentPosition = -1;
}
void insert(T element) {
if (numberOfElements >= maxListSize) {
cout << "List is Full" << endl;
return;
}
data[numberOfElements] = element;
numberOfElements++;
}
bool first(T &element) {
if (numberOfElements == 0) {
cout << "List is Empty" << endl;
return false;
}
else {
currentPosition = 0;
element = data[currentPosition];
return true;
}
}
bool next(T &element) {
//Check if the user called the first function
if (currentPosition < 0) {
cout << "Please call the first function before calling the next" << endl;
return false;
}
if (currentPosition >= numberOfElements - 1) {
//cout << "No next item" << endl;
return false;
}
currentPosition++;
element = data[currentPosition];
return true;
}
};
Assume my list is called names. How can I get the frequency of each word?
Here is how I would do it, given the unfortunate interface of List.
Initially I thought I would use List<pair<string, int>> but the first and next functions provide a copy of the element making it impossible to modify in place, so pointers it is!
This leaks memory. If not leaking is important to you then you could use smart pointers or attempt to free the memory. I figured simple might be better.
#include <iostream>
#include <string>
const int maxListSize = 50;
template<class T>
class List
{
private:
int numberOfElements;
int currentPosition;
T data[maxListSize];
public:
List()
{
numberOfElements = 0;
currentPosition = -1;
}
void insert(T element)
{
if (numberOfElements >= maxListSize)
{
return;
}
data[numberOfElements] = element;
numberOfElements++;
}
bool first(T &element)
{
if (numberOfElements == 0)
{
return false;
}
else
{
currentPosition = 0;
element = data[currentPosition];
return true;
}
}
bool next(T &element)
{
if (currentPosition < 0)
{
return false;
}
if (currentPosition >= numberOfElements - 1)
{
return false;
}
currentPosition++;
element = data[currentPosition];
return true;
}
};
using WordPair = std::pair<std::string, int>;
using WordList = List<WordPair*>;
void incrementCount(WordList &wl, const std::string& s)
{
WordPair* item = nullptr;
if (wl.first(item))
{
if (item->first == s)
{
++(item->second);
return;
}
while (wl.next(item))
{
if (item->first == s)
{
++(item->second);
return;
}
}
}
wl.insert(new WordPair { s, 1 });
}
void printList(WordList &wl)
{
WordPair *item = nullptr;
if (wl.first(item))
{
std::cout << item->first << " : " << item->second << "\n";
while (wl.next(item))
{
std::cout << item->first << " : " << item->second << "\n";
}
}
}
int main()
{
std::string words[10] = { "one", "two", "three", "four", "one",
"two", "three", "two", "three", "three" };
WordList wl;
for (int i = 0; i < 10; ++i)
{
incrementCount(wl, words[i]);
}
printList(wl);
}
Example: https://ideone.com/W4Slyq
Are you allowed to hold duplicates? If so you can just loop through the list.
int count(T &element) {
int numberOfDuplicates = 0;
for (int i = 0; i < numberOfElements; i++) {
if (data[i] == element) {
numberOfDuplicates++;
}
}
return numberOfDuplicates;
}

Is there a command to check if objects in my dynamic array are dynamic or static?

I made a dynamic array with template. The problem is that when I don't keep there pointers (for example: Tab<string> da;) my destructor doesn't have to clear it and throws error caused by delete arr[i];. My question is if I can put some if condition(in which I would put clear() method) which would tell me if my array keeps pointers. In the simplest way I can use clear() in main when I keeps there pointers, but my teacher wants me to make it like I wrote above.
I tried using is_pointer, but it doesn't work or I use it wrong.
Any suggestions?
#ifndef TABLICA_H
#define TABLICA_H
#include <iostream>
#include <type_traits>
using namespace std;
template<class T>
class Tab
{
public:
int size = 0;
int max_size = 1;
T* arr;
bool isDynamic = false;
Tab()
{
arr = new T[max_size];
}
~Tab()
{
clear();
delete[] arr;
}
void check_size()
{
if (size == max_size)
{
max_size = max_size * 2;
T* arr2 = new T[max_size];
for (int i = 0; i < size; i++)
{
arr2[i] = arr[i];
}
delete[] arr;
arr = arr2;
}
}
void push_back(const T& value)
{
check_size();
arr[size] = value;
size++;
}
T return_by_index(int index)
{
if (index<0 || index > size)
{
return NULL;
}
return arr[index];
}
bool replace(int index, const T& value)
{
if (index<0 || index > size)
{
return false;
}
arr[index] = value;
return true;
}
void print(int number)
{
cout << "Rozmiar obecny: " << size << endl;
cout << "Rozmiar maksymalny: " << max_size << endl;
cout << "Adres tablicy: " << arr << endl;
cout << "Kilka poczatkowych elementow tablicy " << "(" << number << ")" << endl;
for (int i = 0; i < number; i++)
{
cout << *arr[i] << endl;
}
}
void clear()
{
for (int i = 0; i < size; i++)
{
delete arr[i];
}
}
};
#endif
//Source:
#include <iostream>
struct object
{
int field1;
char field2;
object()
{
field1 = rand() % 10001;
field2 = rand() % 26 + 'A';
}
};
ostream& operator<<(ostream& out, const object& o)
{
return out << o.field1 << " " << o.field2;
}
int main()
{
Tab < object* >* da = new Tab < object* >();
delete da;
system("PAUSE");
return 0;

How to test the given ADT implementation with templates such as <int, int> and <string, int>?

I am working on a problem that requires the implementation of two ADT's. After Implementing, I need to test my bag implementations with the following template combinations:
<int, string>-- all functions
<string, int> -- insert and find functions only
My testing so far has been entering integers to test the different functions. I do not understand what it means to test the implementations with the templates.
Here is my bagADT implementation:
#include <stdlib.h>
#include "bagADT.h"
template <typename E>
class ABag : public Bag<E> {
private:
int maxSize;
int listSize;
E* listArray;
public:
ABag(int size = defaultSize) { // Constructor
maxSize = size;
listSize = 0;
listArray = new E[maxSize];
}
~ABag() { delete[] listArray; } // Destructor
bool addItem(const E& item) {
if (listSize >= maxSize) {
return false;
}
listArray[listSize] = item;
std::cout << "Add Item: Added " << item << " in spot " << listSize << std::endl;
listSize++;
return true;
}
bool remove(E& item) {
for (int i = 0; i < listSize; i++) {
if (listArray[i] == item) {
std::cout << "Remove: Removed " << item << " from position ";
item = i;
std::cout<< item << " and adjusted the location of all other elements." << std::endl;
for (i= item; i < listSize; i++) {
listArray[i] = listArray[i + 1];
}
listSize--;
return true;
}
}
return false;
}
bool removeTop(E& returnValue) {
if (listSize == 0) {
return false;
}
else {
returnValue = listArray[listSize - 1];
std::cout << "Remove Top: Removed " << returnValue << " from the top of the stack." << std::endl;
for (int i = listSize; i < maxSize; i++) {
listArray[i] = listArray[i + 1];
}
listSize--;
return true;
}
}
bool find(E& returnValue) const {
for (int i = 0; i < (listSize - 1); i++) {
if (listArray[i] == returnValue) {
returnValue = i;
return true;
}
}
return false;
}
bool inspectTop(E& item) const {
if (listSize == 0) {
return false;
}
else {
item = listArray[listSize - 1];
std::cout << "Inspect Top: The value on top is currently " << item << "." << std::endl;
return true;
}
}
void emptyBag() {
delete[] listArray;
listSize = 0;
listArray = new E[maxSize];
std::cout << "Empty Bag: Emptied the bag." << std::endl;
}
bool operator+=(const E& addend) {
if (listSize < maxSize) {
return true;
}
return false;
}
int size() const {
std::cout << "Size: Number of elements in listArray: " << listSize << std::endl;
return (listSize - 1);
}
int bagCapacity() const {
std::cout << "Bag Capacity: The capacity of this bag is " << maxSize << std::endl;
return maxSize;
}
};
Here is another file provided by my professor called kvpairs:
#ifndef KVPAIR_H
#define KVPAIR_H
// Container for a key-value pair
// Key object must be an object for which the == operator is defined.
// For example, int and string will work since they both have == defined,
// but Int will not work since it does not have == defined.
template <typename Key, typename E>
class KVpair {
private:
Key k;
E e;
public:
// Constructors
KVpair() {}
KVpair(Key kval, E eval)
{
k = kval; e = eval;
}
KVpair(const KVpair& o) // Copy constructor
{
k = o.k; e = o.e;
}
void operator =(const KVpair& o) // Assignment operator
{
k = o.k; e = o.e;
}
bool operator==(const KVpair& o) const {
if (o.k == k) {
return true;
}
return false;
}
//The following overload is provided by Adam Morrone, Spring 2016 class.
//Thanks Adam :)
friend ostream& operator<<(ostream& os, const KVpair& o) // output print operator
{
os << "Key: " << o.k << " Value: " << o.e;
return os;
}
// Data member access functions
Key key() { return k; }
void setKey(Key ink) { k = ink; }
E value() { return e; }
};
#endif
I am expected to show the test outputs using the above templates, but I have no idea how to do this. Also, ignore the += overload. It is incorrect and I know. I am supposed to overload it to directly add a new int to the array.
I think I understand now. I could be wrong, but this is my guess.
Your bag is singly templated, but it will be holding KVpair. They said they will use KVpair with <int, string> and <string, int>.
When they talk about testing it, that means they will be instantiating it as follows:
int main() {
ABag<KVPair<int, string>> bag;
bag.addItem(KVpair(1, "hi"));
//...
}
This is what I am pretty sure they mean by "testing it with templates".
As a minor edit, I don't know what C++ version you are using but if it's very archaic, you might need to write template instantiation like ABag<KVPair<int, string> > instead of putting them together. I remember vaguely this being an issue a long time ago.

C++ int PriorityQueue using stack

Im just starting to learn C++ programming and for exercise i found this task. I have to write a PriorityQueue using dynamic, array based, integer stack. This is what i have got so far.
#include <iostream>
using namespace std;
class PrioQueue
{
private:
int *bottom_;
int *top_;
int size_;
public:
PrioQueue(int n = 20){
bottom_ = new int[n];
top_ = bottom_;
size_ = n;
}
int getSize(){ return size_; }
void push(int c){
if (!full()){
*top_ = c;
top_++;
}
else{
resize(size_ * 2);
*top_ = c;
top_++;
}
SortPrioQueue();
}
void resize(int newSize){
//Allocate new array and copy in data
int *newArray = new int[newSize];
memcpy(newArray, bottom_, size_ * sizeof(int));
// Set the top to the new array
top_ = newArray + (top_ - bottom_);
// Delete old array
delete[] bottom_;
// Update pointers and size
bottom_ = newArray;
size_ = newSize;
cout << "array has been resized" << endl;
}
void SortPrioQueue(){
int swap = 0; //holding variable
for (int i = 0; i < (size_ - 1); i++)
{
for (int j = (i + 1); j < size_; j++)
{
if (bottom_[i] > bottom_[j])
{
swap = bottom_[i];
bottom_[i] = bottom_[j];
bottom_[j] = swap;
}
}
}
}
int num_items() {
return (top_ - bottom_);
}
int pop(){
top_--;
return *top_;
}
int full() {
return (num_items() >= size_);
}
int empty() {
return (num_items() <= 0);
}
void print(){
cout << "Stack currently holds " << num_items() << " items: ";
for (int *element = bottom_; element<top_; element++) {
cout << " " << *element;
}
cout << "\n";
}
~PrioQueue(){ // stacks when exiting functions
delete[] bottom_;
}
};
int main(){
PrioQueue s(5);
s.print(); cout << "\n";
s.push(10); s.push(24); s.push(53); s.push(74); s.push(5);
s.print(); cout << "\n";
//s.SortPrioQueue();//if i call it here
s.print(); cout << "\n";
while (!s.empty()) s.pop();
if (s.num_items() != 0) {
cout << "Error: Stack is corrupt!\n";
}
s.print(); cout << "\n";
// destructor for s automatically called
system("pause"); // execute M$-DOS' pause command
return 0;
}
There seems to be a problem with SortPrioQueue() method. If i call it from main() method it works fine.
But if i call it from push() method then i get this.
Thanks in advance for your help.
SortPrioQueue() should use top_ - _bottom (or num_items()) as limit and not size.