NB broken. Working on the list implementation.

This commit is contained in:
Kyle Isom 2017-12-19 23:07:51 +00:00
parent 9bba1bcbd1
commit f1220734a4
7 changed files with 129 additions and 22 deletions

View File

@ -4,6 +4,7 @@
#include <cstddef> #include <cstddef>
namespace ods {
// Lists are sequences of values. // Lists are sequences of values.
template <typename T> template <typename T>
@ -12,9 +13,10 @@ public:
virtual std::size_t size(void); virtual std::size_t size(void);
virtual T get(std::size_t); virtual T get(std::size_t);
virtual T set(std::size_t, T); virtual T set(std::size_t, T);
virtual void add(std:size_t, T); virtual void add(std::size_t, T);
virtual T remove(std::size_t); virtual T remove(std::size_t);
}; };
} // end namespace ods
#endif #endif

24
include/simplist.h Normal file
View File

@ -0,0 +1,24 @@
#ifndef __ODS_SIMPLIST__
#define __ODS_SIMPLIST__
#include "list.h"
namespace ods {
template <typename T>
class SimpList : public List {
public:
SimpList();
std::size_t size(void);
T get(std::size_t);
T set(std::size_t, T);
void add(std::size_t, T);
T remove(std::size_t);
private:
T arr[];
std::size_t cap;
std::size_t len;
};
} // end namespace ods
#endif

View File

@ -142,6 +142,8 @@ Run times come in three flavours:
is equal to x. In addition, a Bag supports the findAll(x) operation that is equal to x. In addition, a Bag supports the findAll(x) operation that
returns a list of all elements in the Bag that are equal to x. returns a list of all elements in the Bag that are equal to x.
In progress: src/ch01ex05.cc.
6. From scratch, write and test implementations of the List, USet and SSet 6. From scratch, write and test implementations of the List, USet and SSet
interfaces. These do not have to be efficient. They can be used later to interfaces. These do not have to be efficient. They can be used later to
test the correctness and performance of more efficient implementations. test the correctness and performance of more efficient implementations.

View File

@ -1,9 +1,11 @@
AM_CPPFLAGS = -Wall -Wextra -pedantic -Wshadow -Wpointer-arith -Wcast-align AM_CPPFLAGS = -Wall -Wextra -pedantic -Wshadow -Wpointer-arith -Wcast-align
AM_CPPFLAGS += -Wwrite-strings -Wmissing-declarations -Wno-long-long -Werror AM_CPPFLAGS += -Wwrite-strings -Wmissing-declarations -Wno-long-long -Werror
AM_CPPFLAGS += -Wunused-variable -std=c++14 -D_XOPEN_SOURCE -O0 -g -I. AM_CPPFLAGS += -Wunused-variable -std=c++14 -D_XOPEN_SOURCE -O0 -g -I. -I../include
AM_CPPFLAGS += -fno-elide-constructors -Weffc++ AM_CPPFLAGS += -fno-elide-constructors -Weffc++
bin_PROGRAMS := ch01ex01 ch01ex03 ch01ex04 bin_PROGRAMS := ch01ex01 ch01ex03 ch01ex04 ch01ex05 ch01ex06
ch01ex01_SOURCES := ch01ex01.cc ch01ex01_SOURCES := ch01ex01.cc
ch01ex03_SOURCES := ch01ex03.cc ch01ex03_SOURCES := ch01ex03.cc
ch01ex04_SOURCES := ch01ex04.cc ch01ex04_SOURCES := ch01ex04.cc
ch01ex05_SOURCES := ch01ex05.cc
ch01ex06_SOURCES := simplist.cc ch01ex06.cc

View File

@ -1,15 +1,22 @@
#include <iostream> #include <iostream>
#include <vector> #include <vector>
// Using a USet, implement a Bag. A Bag is like a USet supports the add(x), // Using a USet, implement a Bag. A Bag is like a USet — it supports the add(x),
// remove(x), and find (x) methods — but it allows duplicate // remove(x), and find (x) methods — but it allows duplicate elements to be
// elements to be stored. The find(x) operation in a Bag returns some element (if any) that is equal to x. In addition, // stored. The find(x) operation in a Bag returns some element (if any) that is
// a Bag supports the findAll(x) operation that returns a list of all elements in the Bag that are equal to x. // equal to x. In addition, a Bag supports the findAll(x) operation that returns
// a list of all elements in the Bag that are equal to x.
template<typename T> // template<typename T>
class Bag { // class Bag {
public: // public:
//
// private:
// vector<T> v;
// };
private: int
vector<T> v; main(void)
}; {
return 0;
}

22
src/ch01ex06.cc Normal file
View File

@ -0,0 +1,22 @@
#include <cassert>
#include <simplist.h>
using namespace std;
static void
check_simplist(void)
{
ods::SimpList<int> sl;
sl.add(0, 1);
sl.add(1, 2);
sl.add(2, 3);
assert(sl.size() == 3);
}
int
main(void)
{
check_simplist();
}

48
src/simplist.cc Normal file
View File

@ -0,0 +1,48 @@
#include "simplist.h"
#include <cassert>
#include <cstdlib>
#include <stdexcept>
constexpr std::size_t DEFAULT_SIZE = 8;
namespace ods {
SimpList<T>::SimpList<T>()
{
this->arr = new T[DEFAULT_SIZE];
this->cap = 0;
this->len = 0;
}
std::size_t
SimpList<T> size(void)
{
std::assert(len <= cap);
return this->len();
}
T
SimpList<T>::get(std::size_t i)
{
throw std::invalid_argument("invalid argument");
}
T
SimpList<T>::set(std::size_t, T)
{
throw std::invalid_argument("invalid argument");
}
void
SimpList<T>::add(std::size_t, T)
{
return;
}
T
SimpList<T>::remove(std::size_t)
{
throw std::invalid_argument("invalid argument");
}
};