languages

A collection of programs made with different programming languages.
git clone git://evanalba.com/languages
Log | Files | Refs

todo_list.cpp (2692B)


      1 /*
      2  * Name        : todo_list.cpp
      3  * Author      : Evan Alba
      4  * Description : CPP File for class TodoList.
      5  */
      6 #include "todo_list.h"
      7 
      8 TodoList::TodoList() {
      9   size_ = 0;
     10   cap_ = 25;
     11   list_ = new TodoItem*[cap_];
     12   for (unsigned int i = 0; i < cap_; i++) {
     13     list_[i] = nullptr;
     14   }
     15 }
     16 
     17 TodoList::~TodoList() {
     18   for (unsigned int i = 0; i < size_; i++) {
     19     delete list_[i];
     20   }
     21   delete[] list_;
     22 }
     23 
     24 void TodoList::AddItem(TodoItem* add) {
     25   if (size_ == cap_) {
     26     IncreaseCap();
     27   }
     28   list_[size_] = add;
     29   size_ += 1;
     30 }
     31 
     32 void TodoList::DeleteItem(unsigned int location) {
     33   if ((location > 0) && (location <= size_)) {
     34     delete list_[location - 1];
     35     list_[location - 1] = nullptr;
     36     size_ -= 1;
     37     TightenArray(location - 1);
     38   }
     39 }
     40 
     41 TodoItem* TodoList::GetItem(int location) {
     42   if ((size_ > 0) && (size_ <= cap_) && (list_[location-1] != nullptr)) {
     43     return list_[location - 1];
     44   }
     45   return nullptr;
     46 }
     47 
     48 unsigned int TodoList::GetSize() const {
     49   return size_;
     50 }
     51 
     52 unsigned int TodoList::GetCapacity() const {
     53   return cap_;
     54 }
     55 
     56 void TodoList::Sort() {
     57   for (unsigned int i = 0; i <= (size_ - 1); i++) {
     58     int j = i;
     59     while ((j > 0) && (list_[j]->priority() < list_[j - 1]->priority())) {
     60       std::swap(list_[j], list_[j - 1]);
     61       j -= 1;
     62     }
     63   }
     64 }
     65 
     66 /*
     67   Returns a string containing all TodoItems in the list.
     68   Uses the TodoItems ToFile function to create. Each item should be
     69   on its own line.
     70 */
     71 std::string TodoList::ToFile() {
     72   if ((size_ > 0) && (size_ <= cap_)) {
     73     std::stringstream all;
     74     for (unsigned int i = 0; i < size_; i++) {
     75         all << list_[i]->ToFile() << std::endl;
     76     }
     77     return all.str();
     78   }
     79   return "";
     80 }
     81 
     82 /* Outputs a numbered list of all TodoItem present in the list. */
     83 std::ostream &operator << (std::ostream &out, const TodoList &obj) {
     84   for (unsigned int i = 0; i < obj.size_; i++) {
     85     out << obj.list_[i]->description() << obj.list_[i]->priority()
     86     << obj.list_[i]->completed() << std::endl;
     87   }
     88   return out;
     89 }
     90 
     91 /* PRIVATE */
     92 /*
     93   Increases the capacity of the array by 10. Should be called by
     94   AddItem at the appropriate time.
     95 */
     96 void TodoList::IncreaseCap() {
     97   cap_ += 10;
     98   TodoItem** extend = new TodoItem*[cap_];
     99   for (unsigned int i = 0; i < size_; i++) {
    100     extend[i] = list_[i];
    101   }
    102   for (unsigned int i = size_; i < cap_; i++) {
    103     extend[i] = nullptr;
    104   }
    105   delete[] list_;
    106   list_ = extend;
    107 }
    108 
    109 /* 
    110   Compacts the array to get rid of an empty spot in the array. 
    111   Should be called by DeleteItem at the appropriate time. 
    112 */
    113 void TodoList::TightenArray(int start) {
    114   for (unsigned int i = start; i < size_; i++) {
    115     if (list_[i + 1] != nullptr) {
    116       list_[i] = list_[i + 1];
    117     }
    118   }
    119 }