Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added ListTable #6

Merged
merged 7 commits into from
Mar 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion polinom/include/HeadRing.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ class THeadRing : public TDatList<T> {
int GoNext(void) override;
void InsCurrent(T* pVal) override;
void InsLast(T* pVal) override;
void DelList(void) override;
void DelList(void) override;
int IsListEnded(void) const override;

THeadRing& operator=(const THeadRing& q);
};

Expand Down
7 changes: 4 additions & 3 deletions polinom/src/DatList.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,10 @@ int TDatList<T>::Reset(void)
}

template<class T>
int TDatList<T>::IsListEnded(void) const
{
return pCurrLink->pNext == pStop;
int TDatList<T>::IsListEnded(void) const
{
if (pCurrLink == NULL) return NULL;
return pCurrLink == pStop;
}

template<class T>
Expand Down
16 changes: 14 additions & 2 deletions polinom/src/HeadRing.hpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "HeadRing.h"
template<class T>
THeadRing<T>::THeadRing()
{
Expand Down Expand Up @@ -40,8 +41,12 @@ int THeadRing<T>::GoNext(void)
{
if (this->pCurrLink->GetNextDatLink() == this->pHead) {
this->pCurrLink = this->pHead;
}
return TDatList<T>::GoNext();
}

this->pPrevLink = this->pCurrLink;
this->pCurrLink = this->pCurrLink->GetNextDatLink();
this->CurrPos = ++this->CurrPos % this->ListLen;
return this->CurrPos;
}

template<class T>
Expand Down Expand Up @@ -76,6 +81,13 @@ void THeadRing<T>::DelList(void)
}
}

template<class T>
int THeadRing<T>::IsListEnded(void) const
{
if (this->pCurrLink == NULL) return NULL;
return this->pPrevLink == pHead;
}

template<class T>
THeadRing<T>& THeadRing<T>::operator=(const THeadRing<T>& q)
{
Expand Down
17 changes: 12 additions & 5 deletions table/ListTable/include/ListTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
#include <list>

#include "Table.h"
#include "DatList.h"
#include "HeadRing.h"

/// <summary>
/// Линейная таблица на списке.
/// </summary>
template <class Key, class Value>
class ListTable : public Table<Key,Value> {
protected:
TDatList<typename Table<Key, Value>::template STableRec<Key, Value>> data;
THeadRing<typename Table<Key, Value>::template STableRec<Key, Value>> data;
public:
ListTable(size_t maxSize = 10000) : Table<Key, Value>(maxSize) { }
~ListTable() { }
Expand All @@ -22,19 +22,19 @@ class ListTable : public Table<Key,Value> {
/// <summary>
/// Возращает количество записей (длину таблицы).
/// </summary>
size_t GetDataCount() const noexcept override { return data.size(); }
size_t GetDataCount() const noexcept override { return data.GetLength(); }

/// <summary>
/// Проверка таблицы на пустоту.
/// Если длина таблицы равна нулю, возвращает true.
/// </summary>
bool IsEmpty() const noexcept override { return data.size() == 0; }
bool IsEmpty() const noexcept override { return data.GetLength() == 0; }

/// <summary>
/// Проверка таблицы на заполненность.
/// Если длина таблицы равна maxSize, возвращает true.
/// </summary>
bool IsFull() const noexcept override { return data.size() == this->maxSize; };
bool IsFull() const noexcept override { return data.GetLength() == this->maxSize; };
#pragma endregion

#pragma region Main Methods
Expand All @@ -59,6 +59,13 @@ class ListTable : public Table<Key,Value> {
#pragma endregion

#pragma region Navigate

/// <summary>
/// Устанавливает текущую позицию на первую запись
/// Возвращает 0
/// </summary>
virtual size_t Reset(void) noexcept override;

/// <summary>
/// Проверка окончания таблицы.
/// Если таблица кончилась, то возращает true.
Expand Down
50 changes: 45 additions & 5 deletions table/ListTable/src/ListTable.hpp
Original file line number Diff line number Diff line change
@@ -1,40 +1,80 @@
#include "ListTable.h"
template<class Key, class Value>
Value* ListTable<Key, Value>::Find(Key key)
{
return new Value();
data.Reset();

while (!data.IsListEnded()) {
auto* record = data.GetDatValue();
if (record == nullptr)
break;

if (record->key == key)
return &record->value;

data.GoNext();
}

throw std::runtime_error("Key not found in the table.");
}

template<class Key, class Value>
void ListTable<Key, Value>::Insert(Key key, Value value)
{
try {
Value* recValue = Find(key);
*recValue = value;
}
catch (std::runtime_error) {
if (!IsFull())
data.InsLast(new Table<Key, Value>::STableRec<Key, Value>(key, value));
else
throw std::length_error("Table is overflow");
}
}

template<class Key, class Value>
void ListTable<Key, Value>::Delete(Key key)
{
Value* recValue = Find(key);
data.DelCurrent();
}

template<class Key, class Value>
size_t ListTable<Key, Value>::Reset(void) noexcept
{
data.Reset();
return Table<Key, Value>::Reset();
}

template<class Key, class Value>
bool ListTable<Key, Value>::IsTabEnded(void) const noexcept
{
return false;
return data.IsListEnded();
}

template<class Key, class Value>
size_t ListTable<Key, Value>::GoNext(void) noexcept
{
return size_t();
this->position = data.GoNext();
return this->position;
}

template<class Key, class Value>
Key ListTable<Key, Value>::GetKey(void) const
{
return Key();
auto* rec = data.GetDatValue();
if (rec == nullptr)
throw std::runtime_error("Getting key from empty table");
return rec->key;
}

template<class Key, class Value>
Value ListTable<Key, Value>::GetValuePtr(void) const
{
return Value();
auto* rec = data.GetDatValue();
if (rec == nullptr)
throw std::runtime_error("Getting value from empty table");
return rec->value;
}

99 changes: 99 additions & 0 deletions table/ListTable/test/test_listTable.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#include <gtest.h>
#include "ListTable.h"
using namespace std;

TEST(ListTable, Get_size) {
ListTable<string, int> table;
ASSERT_EQ(0, table.GetDataCount());
ASSERT_NO_THROW(table.Insert("r1", 1234));
ASSERT_EQ(1, table.GetDataCount());
}

TEST(ListTable, Is_empty) {
ListTable<string, int> table;
ASSERT_EQ(true, table.IsEmpty());
ASSERT_NO_THROW(table.Insert("r1", 1234));
ASSERT_EQ(false, table.IsEmpty());
}

TEST(ListTable, Is_full) {
ListTable<string, int> table(1);
ASSERT_EQ(false, table.IsFull());
ASSERT_NO_THROW(table.Insert("r1", 1234));
ASSERT_EQ(true, table.IsFull());
}

TEST(ListTable, Insert) {
ListTable<string, int> table;
ASSERT_NO_THROW(table.Insert("r1", 1234));
ASSERT_EQ(1, table.GetDataCount());
}

TEST(ListTable, Delete_exist_record) {
ListTable<string, int> table;
ASSERT_NO_THROW(table.Insert("r1", 1234));
ASSERT_NO_THROW(table.Delete("r1"));
ASSERT_EQ(0, table.GetDataCount());
}

TEST(ListTable, Delete_not_exist_record) {
ListTable<string, int> table;
ASSERT_THROW(table.Delete("r1"), std::runtime_error);
}

TEST(ListTable, Find_exist_record) {
ListTable<string, int> table;
ASSERT_NO_THROW(table.Insert("r1", 1234));

int* val;
ASSERT_NO_THROW(val = table.Find("r1"));
ASSERT_EQ(1234, *val);
}

TEST(ListTable, Find_not_exist_record) {
ListTable<string, int> table;
ASSERT_THROW(table.Find("r1"), std::runtime_error);
}

TEST(ListTable, Reset) {
ListTable<string, int> table;
ASSERT_EQ(0, table.Reset());
}

TEST(ListTable, Is_table_ended) {
ListTable<string, int> table;
ASSERT_NO_THROW(table.Insert("r1", 1234));
ASSERT_EQ(false, table.IsTabEnded());
ASSERT_NO_THROW(table.GoNext());
ASSERT_EQ(true, table.IsTabEnded());
}

TEST(ListTable, Go_next) {
ListTable<string, int> table;
ASSERT_NO_THROW(table.Insert("r1", 1234));
ASSERT_NO_THROW(table.Insert("r2", 1234));
ASSERT_EQ(1, table.GoNext());
ASSERT_EQ(0, table.GoNext());
}

TEST(ListTable, Get_key) {
ListTable<string, int> table;
ASSERT_NO_THROW(table.Insert("r1", 1234));
ASSERT_EQ("r1", table.GetKey());
}

TEST(ListTable, Get_key_from_empty_table) {
ListTable<string, int> table;
ASSERT_THROW(table.GetKey(), std::runtime_error);
}

TEST(ListTable, Get_value) {
ListTable<string, int> table;
ASSERT_NO_THROW(table.Insert("r1", 1234));
ASSERT_EQ(1234, table.GetValuePtr());
}

TEST(ListTable, Get_value_from_empty_table) {
ListTable<string, int> table;
ASSERT_THROW(table.GetValuePtr(), std::runtime_error);
}
3 changes: 0 additions & 3 deletions table/ListTable/test/test_vectorTable.cpp

This file was deleted.

4 changes: 4 additions & 0 deletions table/Table/include/Table.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ class Table {
struct STableRec {
Key key;
Value value;
STableRec(Key key, Value value) {
this->key = key;
this->value = value;
}
};

/// <summary>
Expand Down
Loading