Commit 21759509 authored by Florian Klemme's avatar Florian Klemme
Browse files

Map, Pair und Vector implementiert.

parent 0e434fae
Pipeline #61 failed with stages
in 1 minute and 59 seconds
#pragma once
#include <iostream>
#include "Map.hpp"
#include "Vector.hpp"
using Zahlen = Vector<int>;
using String = Vector<char>;
class CppKlausur {
public:
// Definiere Input/Output fuer das Programm
......@@ -12,7 +16,7 @@ class CppKlausur {
// Programm ausfuehren, entsprechend der Aufgabenbeschreibung
void run();
private:
private:
void einfuegemodus();
void suchmodus();
void loeschmodus();
......@@ -21,7 +25,7 @@ class CppKlausur {
std::istream& m_in;
std::ostream& m_out;
protected:
Map m_forward;
Map m_reverse;
protected:
Map<Zahlen, String> m_forward;
Map<String, Vector<Zahlen>> m_reverse;
};
\ No newline at end of file
#pragma once
class Map {};
\ No newline at end of file
#include "Pair.hpp"
#include "Vector.hpp"
template <typename Key, typename Value>
class Map {
public:
int size() const noexcept { return m_data.size(); }
Value& operator[](const Key& key) {
for (int i = 0; i < m_data.size(); ++i) {
if (m_data[i].first == key) return m_data[i].second;
}
// Wie in std::map: default-constructe
// value, wenn key noch nicht vorhanden
m_data.push_back(Pair(key, Value()));
return m_data[m_data.size() - 1].second;
}
int count(const Key& key) const noexcept {
int count = 0;
for (int i = 0; i < m_data.size(); ++i) {
if (m_data[i].first == key) ++count;
}
return count;
}
protected:
Vector<Pair<Key, Value>> m_data;
};
\ No newline at end of file
#pragma once
template <typename First, typename Second>
struct Pair {
Pair() = default;
Pair(First first, Second second) : first(first), second(second) {}
First first;
Second second;
};
\ No newline at end of file
......@@ -5,46 +5,48 @@
template <typename T>
class Vector {
public:
// Rule of five mit "copy-and-swap" Idom
explicit Vector(int initial_capacitance = 0)
: m_data(initial_capacitance ? new T[initial_capacitance] : nullptr),
m_capacitance(initial_capacitance),
Vector() : m_data(nullptr), m_capacitance(0), m_size(0) {}
explicit Vector(int initial_capacitance)
: m_data(initial_capacitance > 0 ? new T[initial_capacitance] : nullptr),
m_capacitance(initial_capacitance > 0 ? initial_capacitance : 0),
m_size(0) {}
Vector(const Vector& that) : Vector() {
Vector temp = that;
swap(temp);
// Rule of five mit "copy-and-swap" Idom...
~Vector() {
if (m_data) delete[] m_data;
}
Vector(Vector&& that) : Vector() { swap(that); }
virtual ~Vector() {
if (m_data) delete[] m_data;
Vector(const Vector& that) : Vector(that.m_size) {
for (int i = 0; i < that.m_size; ++i) m_data[m_size++] = that.m_data[i];
}
Vector& operator=(const Vector& that) {
Vector temp = that;
swap(temp);
Vector(Vector&& that) noexcept : Vector() { swap(*this, that); }
// Call-by-value: Kopie fuer Swap
Vector& operator=(Vector that) {
swap(*this, that);
return *this;
}
Vector& operator=(Vector&& that) {
swap(that);
Vector& operator=(Vector&& that) noexcept {
swap(*this, that);
return *this;
}
private:
void swap(Vector& that) {
::swap(m_data, that.m_data);
::swap(m_capacitance, that.m_capacitance);
::swap(m_size, that.m_size);
friend void swap(Vector& a, Vector& b) noexcept {
swap(a.m_data, b.m_data);
swap(a.m_capacitance, b.m_capacitance);
swap(a.m_size, b.m_size);
}
public:
int capacitance() const { return m_capacitance; }
int size() const { return m_size; }
int capacitance() const noexcept { return m_capacitance; }
int size() const noexcept { return m_size; }
T& operator[](int index) noexcept { return m_data[index]; }
T operator[](int index) const noexcept { return m_data[index]; }
public:
void push_back(const T& value) {
if (!(m_size < m_capacitance)) resize(2 * m_capacitance + 1);
m_data[m_size++] = value;
......@@ -67,4 +69,13 @@ class Vector {
T* m_data;
int m_capacitance; // Allokierter Speicher fuer m_data
int m_size; // Anzahl der Elemente in m_data
};
\ No newline at end of file
};
template <typename T>
bool operator==(const Vector<T>& a, const Vector<T>& b) {
if (a.size() != b.size()) return false;
for (int i = 0; i < a.size(); ++i) {
if (a[i] != b[i]) return false;
}
return true;
}
\ No newline at end of file
#include "Map.hpp"
#include <gtest/gtest.h>
TEST(Vector, access) {
Map<char, int> m1;
ASSERT_EQ(0, m1.size());
m1['a'] = 5;
ASSERT_EQ(1, m1.size());
ASSERT_EQ(5, m1['a']);
ASSERT_EQ(1, m1.count('a'));
ASSERT_EQ(0, m1.count('b'));
m1['b'] = 3;
ASSERT_EQ(2, m1.size());
ASSERT_EQ(3, m1['b']);
ASSERT_EQ(1, m1.count('a'));
ASSERT_EQ(1, m1.count('b'));
}
\ No newline at end of file
......@@ -13,17 +13,33 @@ TEST(Vector, Create) {
}
TEST(Vector, Copy) {
Vector<int> v1;
v1.push_back(4);
v1.push_back(2);
v1.push_back(0);
ASSERT_EQ(3, v1.size());
ASSERT_GE(3, v1.capacitance());
auto v2 = v1;
ASSERT_EQ(v1.size(), v2.size());
ASSERT_GE(v2.size(), v2.capacitance());
for (int i = 0; i < v1.size(); ++i) ASSERT_EQ(v1[i], v2[i]);
}
TEST(Vector, push_back) {
Vector<int> v1;
ASSERT_EQ(0, v1.size());
v1.push_back(4);
ASSERT_EQ(1, v1.size());
ASSERT_EQ(4, v1[0]);
v1.push_back(2);
ASSERT_EQ(2, v1.size());
ASSERT_EQ(2, v1[1]);
v1.push_back(0);
ASSERT_EQ(3, v1.size());
ASSERT_EQ(0, v1[2]);
ASSERT_GE(3, v1.capacitance());
auto v2 = v1;
ASSERT_GE(3, v2.capacitance());
ASSERT_EQ(3, v2.size());
}
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment