NB broken. Working on the list implementation.
This commit is contained in:
parent
9bba1bcbd1
commit
f1220734a4
|
@ -4,17 +4,19 @@
|
|||
|
||||
#include <cstddef>
|
||||
|
||||
namespace ods {
|
||||
|
||||
// Lists are sequences of values.
|
||||
template <typename T>
|
||||
class List {
|
||||
public:
|
||||
virtual std::size_t size(void);
|
||||
virtual T get(std::size_t);
|
||||
virtual T set(std::size_t, T);
|
||||
virtual void add(std:size_t, T);
|
||||
virtual T remove(std::size_t);
|
||||
};
|
||||
template <typename T>
|
||||
class List {
|
||||
public:
|
||||
virtual std::size_t size(void);
|
||||
virtual T get(std::size_t);
|
||||
virtual T set(std::size_t, T);
|
||||
virtual void add(std::size_t, T);
|
||||
virtual T remove(std::size_t);
|
||||
};
|
||||
|
||||
} // end namespace ods
|
||||
|
||||
#endif
|
||||
#endif
|
||||
|
|
|
@ -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
|
|
@ -142,6 +142,8 @@ Run times come in three flavours:
|
|||
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.
|
||||
|
||||
In progress: src/ch01ex05.cc.
|
||||
|
||||
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
|
||||
test the correctness and performance of more efficient implementations.
|
||||
|
|
|
@ -1,9 +1,11 @@
|
|||
AM_CPPFLAGS = -Wall -Wextra -pedantic -Wshadow -Wpointer-arith -Wcast-align
|
||||
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++
|
||||
|
||||
bin_PROGRAMS := ch01ex01 ch01ex03 ch01ex04
|
||||
bin_PROGRAMS := ch01ex01 ch01ex03 ch01ex04 ch01ex05 ch01ex06
|
||||
ch01ex01_SOURCES := ch01ex01.cc
|
||||
ch01ex03_SOURCES := ch01ex03.cc
|
||||
ch01ex04_SOURCES := ch01ex04.cc
|
||||
ch01ex05_SOURCES := ch01ex05.cc
|
||||
ch01ex06_SOURCES := simplist.cc ch01ex06.cc
|
||||
|
|
|
@ -1,15 +1,22 @@
|
|||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
// Using a USet, implement a Bag. A Bag is like a USet supports the add(x),
|
||||
// remove(x), and find (x) methods — but it allows duplicate
|
||||
// elements to be stored. The find(x) operation in a Bag returns some element (if any) 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.
|
||||
// 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 elements to be
|
||||
// stored. The find(x) operation in a Bag returns some element (if any) 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.
|
||||
|
||||
template<typename T>
|
||||
class Bag {
|
||||
public:
|
||||
// template<typename T>
|
||||
// class Bag {
|
||||
// public:
|
||||
//
|
||||
// private:
|
||||
// vector<T> v;
|
||||
// };
|
||||
|
||||
private:
|
||||
vector<T> v;
|
||||
};
|
||||
int
|
||||
main(void)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
}
|
|
@ -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");
|
||||
}
|
||||
};
|
Loading…
Reference in New Issue