Knyffen
03ec008d0d
Move some helper files to other folders. Actually remove the .sage files from the main directory, as I wrote I would in the readme.
79 lines
1.9 KiB
C++
79 lines
1.9 KiB
C++
#include "hashed_string.hpp"
|
|
|
|
#include <stdlib.h>
|
|
#include <stdint.h>
|
|
#include <string>
|
|
#include <string.h>
|
|
#include <iostream>
|
|
|
|
My_string::My_string(char * chars, size_t size) {
|
|
this->size = size;
|
|
this->chars = new char[size];
|
|
if (size != 0) {
|
|
memcpy(this->chars, chars, size*sizeof(this->chars[0]));
|
|
}
|
|
}
|
|
|
|
My_string::~My_string() {
|
|
delete[] chars;
|
|
}
|
|
|
|
bool My_string::operator==(const My_string& other)
|
|
{
|
|
if (size != other.size)
|
|
return false;
|
|
return strncmp(chars, other.chars, size) == 0;
|
|
}
|
|
|
|
Hashed_string::Hashed_string() : string(My_string(NULL, 0)) {
|
|
initializer = true;
|
|
}
|
|
|
|
Hashed_string::Hashed_string(My_string * s) : string(My_string(s->chars, s->size)) {};
|
|
|
|
Hashed_string::Hashed_string(Hashed_string * hs) : string(My_string(hs->string.chars, hs->string.size)), appearances(hs->appearances) {};
|
|
|
|
Hashed_string::~Hashed_string() {
|
|
// if you delete the initializer, delete the whole chain
|
|
if (initializer) {
|
|
while (next != NULL)
|
|
next->remove(next);
|
|
}
|
|
}
|
|
|
|
int Hashed_string::append(Hashed_string * new_string) {
|
|
if (!initializer) {
|
|
if (string == new_string->string) {
|
|
appearances += new_string->appearances;
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
if (last_element) {
|
|
new_string->previous = this;
|
|
next = new_string;
|
|
last_element = false;
|
|
return 1;
|
|
} else {
|
|
return next->append(new_string);
|
|
}
|
|
}
|
|
|
|
void Hashed_string::remove(Hashed_string * removed_string) {
|
|
if (initializer && last_element)
|
|
return;
|
|
|
|
if (string == removed_string->string) {
|
|
if (last_element) {
|
|
previous->last_element = true;
|
|
previous->next = NULL;
|
|
} else {
|
|
previous->next = next;
|
|
next->previous = previous;
|
|
}
|
|
|
|
delete this;
|
|
} else if (!last_element)
|
|
next->remove(removed_string);
|
|
}
|