66 lines
1.2 KiB
C++
66 lines
1.2 KiB
C++
#include <algorithm>
|
|
|
|
#include "sm_array.h"
|
|
|
|
garray::garray(): size(0) {};
|
|
|
|
bool garray::add(string name) {
|
|
if(name == "") {
|
|
throw string("cannot add empty string");
|
|
}
|
|
if((not contains(name)) and size < MAX_DATA_SIZE) {
|
|
data[size] = name;
|
|
size++;
|
|
return true;
|
|
}
|
|
else {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
bool garray::contains(string name) {
|
|
for(int i = 0; i < size; i++) {
|
|
if(name == data[i])
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
bool garray::remove(string name) {
|
|
bool reduce = false;
|
|
if(contains(name)) {
|
|
reduce = true;
|
|
}
|
|
else {
|
|
return reduce;
|
|
}
|
|
garray tmp;
|
|
for(int i = 0; i < size; i++) {
|
|
if(data[i] != name) {
|
|
tmp.add(data[i]);
|
|
}
|
|
}
|
|
for(int i = 0; i < MAX_DATA_SIZE; i++) {
|
|
data[i] = tmp.data[i];
|
|
}
|
|
if(reduce)
|
|
size--;
|
|
return reduce;
|
|
}
|
|
|
|
void garray::randomize() {
|
|
random_shuffle(&data[0], &data[size]);
|
|
}
|
|
|
|
ostream & operator<<(ostream & os, garray & g) {
|
|
os << "[";
|
|
for(int i = 0; i < g.size; i++) {
|
|
os << i << "-" << g.data[i];
|
|
if (i != g.size - 1) {
|
|
os << ", ";
|
|
}
|
|
}
|
|
os << "]";
|
|
return os;
|
|
}
|