NB broken. Working on the list implementation.
This commit is contained in:
parent
9bba1bcbd1
commit
f1220734a4
|
@ -4,17 +4,19 @@
|
||||||
|
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
|
|
||||||
|
namespace ods {
|
||||||
|
|
||||||
// Lists are sequences of values.
|
// Lists are sequences of values.
|
||||||
template <typename T>
|
template <typename T>
|
||||||
class List {
|
class List {
|
||||||
public:
|
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
|
||||||
|
|
|
@ -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
|
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.
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
||||||
|
|
|
@ -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