Untitled

mail@pastecode.io avatar
unknown
c_cpp
3 years ago
1.5 kB
2
Indexable
Never
#include "ContenitoreStatico.h"

int StaticContainer::count(int value) {
	int countResult = 0;
	for (int i = 0; i < usedValues; i++) {
		if (elements[i] == value)
			countResult++;
	}
	return countResult;
}
int StaticContainer::remove(int value) {
	int removed = 0;
	int i = 0;
	//Non sappiamo quante volte verrà eseguito il ciclo causa uguaglianze
	while (i < usedValues) {
		if (elements[i] == value) {
			usedValues--;
			elements[i] = elements[usedValues];
			removed++;
		}
		else
			i++;
	}
	return removed;
}
bool StaticContainer::removeOne(int value) {
	for (int i = 0; i < usedValues; i++) {
		if (elements[i] == value) {
			usedValues--;
			elements[i] = elements[usedValues];
			return true;
		}
	}
	return false;
}
void StaticContainer::add(int value) {
	if (usedValues == CAPACITY)
		return; //Scrivere così vuol dire che usciamo
	elements[usedValues] = value;
	usedValues++;
}
int StaticContainer::checkElement(int value) {
	if (value < 0 || value >= usedValues)
	return 0;
	return elements[value];
}
void StaticContainer::operator+=(StaticContainer istance) {
	if (usedValues + istance.usedValues > CAPACITY)
		return;
	int start = usedValues;
	int stop = usedValues + istance.usedValues;
	for (int i = start; i < stop; i++) {
		int index = i - start;
		elements[i] = istance.checkElement(index);
	}
	usedValues += istance.usedValues;
}
int StaticContainer::getElements(int i) {
	return elements[i];
}