Relz / OOP

Лабораторные работы по Объектно-ориентированному программированию (IDE - Visual Studio 2015)
0 stars 0 forks source link

Замечания по StringList #13

Open alexey-malov opened 7 years ago

alexey-malov commented 7 years ago
    CStringList(CStringList & list);
CStringList DoSomething(const CStringList & lst)
{
    return lst;
}
alexey-malov commented 7 years ago
    void CStringList::Insert(const CListIterator<std::string> & it, std::string data);
    void CStringList::Erase(const CListIterator<std::string> & it);
alexey-malov commented 7 years ago
    std::string & GetLastElement() const;
    std::string & GetFirstElement() const;
    const std::string & GetLastElement() const;
    const std::string & GetFirstElement() const;
alexey-malov commented 7 years ago
    const CListIterator<std::string> cbegin() const;
    const CListIterator<std::string> cend() const;
alexey-malov commented 7 years ago
    CListIterator<std::string> begin();
    CListIterator<std::string> end();
alexey-malov commented 7 years ago
alexey-malov commented 7 years ago
CStringList& CStringList::operator=(CStringList const& rhs)
{
    Copy(rhs);
    return *this;
}
void CStringList::Copy(CStringList const& rhs)
{
    Init();
    for (const std::string & elem : rhs)
    {
        PushBack(elem);
    }
}
CStringList& CStringList::operator=(CStringList && rhs)
{
    Move(std::move(rhs));
    return *this;
}

void CStringList::Move(CStringList && rhs)
{
    m_firstNode = std::move(rhs.m_firstNode);
    m_lastNode = rhs.m_lastNode;
    rhs.m_lastNode = nullptr;
    m_size = rhs.m_size;
    rhs.m_size = 0;
    rhs.Init();
}
alexey-malov commented 7 years ago
void CStringList::PushBack(const std::string & data)
{
    try
    {
        std::unique_ptr<ListNode> newNode = std::make_unique<ListNode>(data, m_lastNode, nullptr);
        ListNode *newLastNode = newNode.get();
        if (m_lastNode)
        {
            m_lastNode->next = std::move(newNode);
        }
        else
        {
            m_firstNode = std::move(newNode);
        }
        m_lastNode = newLastNode;
        m_lastNode->next = nullptr;

        ++m_size;
    }
    catch (...)
    {
        throw;
    }
}
alexey-malov commented 7 years ago
void CStringList::PushFront(const std::string & data)
{
    try
    {
        std::unique_ptr<ListNode> newNode = InsertOnTheEdge(data, nullptr, std::move(m_firstNode));
        if (!newNode->next)
        {
            m_lastNode = newNode.get();
        }
        m_firstNode = std::move(newNode);
        m_firstNode->prev = nullptr;

        m_size++;
    }
    catch (...)
    {
        throw;
    }
}
alexey-malov commented 7 years ago
alexey-malov commented 7 years ago
CListIterator<std::string> CStringList::end()
{
    return (m_lastNode != nullptr) 
        ? CListIterator<std::string>(m_lastNode->next.get(), false) 
        : CListIterator<std::string>(m_lastNode, false);
}
alexey-malov commented 7 years ago
private:
    ListNode* m_node = nullptr;
    bool m_isReverse = false;

    ListNode* operator->() const
    {
        return m_node;
    }
alexey-malov commented 7 years ago
alexey-malov commented 7 years ago

    bool operator==(CListIterator const& rhs) const
    {
        return m_node == rhs.m_node;
    }

    bool operator!=(CListIterator const& rhs) const
    {
        return m_node != rhs.m_node;
    }
alexey-malov commented 7 years ago
void CStringList::Insert(const CListIterator<std::string> & it, std::string data)
{
    if (it == begin())
    {
        PushFront(data);
    }
    else if (it == end())
    {
        PushBack(data);
    }
    else
    {
        try
        {
            auto node = std::make_unique<ListNode>(data, it->prev, std::move(it->prev->next));
            it->prev = std::move(node.get());
            node->prev->next = std::move(node);
        }
        catch (...)
        {
            throw;
        }
    }
}
alexey-malov commented 7 years ago
alexey-malov commented 7 years ago
void CStringList::Init()
{
    m_firstNode = std::make_unique<ListNode>("", nullptr, nullptr);
    m_lastNode = new ListNode("", nullptr, nullptr);
    m_firstNode->next = std::unique_ptr<ListNode>(m_lastNode);
    m_lastNode->prev = m_firstNode.get();
    m_size = 0;
}
alexey-malov commented 7 years ago
void CStringList::PushBack(const std::string & data)
{
    try
    {
        std::unique_ptr<ListNode> newNode = std::make_unique<ListNode>(data, m_lastNode->prev, std::move(m_lastNode->prev->next));
        ListNode *newNodePtr = newNode.get();
        m_lastNode->prev->next = std::move(newNode);
        m_lastNode->prev = newNodePtr;

        ++m_size;
    }
    catch (...)
    {
        throw std::runtime_error("Insertion failed");
    }
}
alexey-malov commented 7 years ago
alexey-malov commented 7 years ago
class CStringList
{
    friend class ListNode;
alexey-malov commented 7 years ago
alexey-malov commented 7 years ago
alexey-malov commented 7 years ago
alexey-malov commented 7 years ago
void CStringList::Clear()
{
    while (m_lastNode)
    {
        m_lastNode->next = nullptr;
        m_lastNode = m_lastNode->prev;
    }
    Init();
}