From acdd80339e2b0b85e3568d956a86b72dab4007ea Mon Sep 17 00:00:00 2001 From: Dirk Steinke Date: Sun, 29 Jun 2014 20:22:54 +0200 Subject: [PATCH] Added Slice ADT and associated test. --- include/jitcs_adt_slice.h | 75 +++++++++++++++++++++++++++++++++++++++++++++++ include/jitcs_typefuncs.h | 19 ++++++++++++ makefile | 3 +- tests/test_adt_slice.cpp | 30 +++++++++++++++++++ 4 files changed, 126 insertions(+), 1 deletion(-) create mode 100644 include/jitcs_adt_slice.h create mode 100644 include/jitcs_typefuncs.h create mode 100644 tests/test_adt_slice.cpp diff --git a/include/jitcs_adt_slice.h b/include/jitcs_adt_slice.h new file mode 100644 index 0000000..63e28a9 --- /dev/null +++ b/include/jitcs_adt_slice.h @@ -0,0 +1,75 @@ +//===-- jitcs_adt_slice.h ---------------------------------------*- C++ -*-===// +// +// +//===----------------------------------------------------------------------===// + +#ifndef _JITCS_ADT_SLICE_H_ +#define _JITCS_ADT_SLICE_H_ + +#include "jitcs_base.h" +#include "jitcs_typefuncs.h" + +namespace jitcs { + +template +struct _SliceBase { + typedef typename ConstType::Type ItemType; + typedef ItemType* iterator; + typedef ItemType const* const_iterator; + + ItemType *_ptr; + size_t _size; + + _SliceBase() : _ptr(nullptr), _size(0) {} + _SliceBase(const _SliceBase&) = default; + _SliceBase& operator =(const _SliceBase&) = default; +// template _SliceBase(const _SliceBase& arr) : _data(arr._data), _n(arr._n) {} + template _SliceBase(const _SliceBase& arr) : _ptr(arr._ptr), _size(arr._size) {} +// template _SliceBase& operator =(const _SliceBase& arr) +// { _data = arr._data; _n = arr._n; } + template _SliceBase& operator =(const _SliceBase& arr) + { _ptr = arr._ptr; _size = arr._size; } + + _SliceBase(ItemType *ptr, size_t no) : _ptr(ptr), _size(no) {} + + size_t size() const { return _size; } + + bool isValidIndex(size_t r) const { return r >= 0 && r < size(); } + + ItemType const& operator [](size_t i) const { assert(isValidIndex(i)); return _ptr[i]; } + + iterator begin() { return _ptr; } + iterator end() { return _ptr + _size; } + const_iterator begin() const { return _ptr; } + const_iterator end() const { return _ptr + _size; } +}; + +template +struct Slice : public _SliceBase { + typedef _SliceBase super; +public: + Slice() = default; + Slice(const Slice&) = default; + Slice& operator =(const Slice& arr) = default; + Slice(ItemType *ptr, size_t no) : super(ptr, no) {} + + ItemType& operator [](size_t i) { assert(isValidIndex(i)); return _ptr[i]; } + const ItemType & operator [](size_t i) const { assert(isValidIndex(i)); return _ptr[i]; } +}; + +template +struct ConstSlice : public _SliceBase { + typedef _SliceBase super; +public: + ConstSlice() = default; + ConstSlice(const ConstSlice& arr) = default; + ConstSlice& operator =(const ConstSlice& arr) = default; + ConstSlice(const Slice& arr) : super(arr) {} + ConstSlice& operator =(const Slice& arr) { return super::operator =(arr); } + ConstSlice(ItemType *ptr, size_t no) : super(ptr, no) {} +}; + +} // End jitcs namespace + +#endif +// _JITCS_ADT_SLICE_H_ diff --git a/include/jitcs_typefuncs.h b/include/jitcs_typefuncs.h new file mode 100644 index 0000000..cbb2c7d --- /dev/null +++ b/include/jitcs_typefuncs.h @@ -0,0 +1,19 @@ +//===-- jitcs_typefuncs.h ---------------------------------------*- C++ -*-===// +// +// +//===----------------------------------------------------------------------===// + +#ifndef _JITCS_TYPEFUNCS_H_ +#define _JITCS_TYPEFUNCS_H_ + +#include "jitcs_base.h" + +namespace jitcs { + +template struct ConstType; +template struct ConstType { typedef T Type; }; +template struct ConstType { typedef const T Type; }; + +} // end jitcs namespace +#endif +// _JITCS_TYPEFUNCS_H_ diff --git a/makefile b/makefile index d9dd286..05f84e5 100644 --- a/makefile +++ b/makefile @@ -6,7 +6,8 @@ CFLAGS_DBG64 = -m64 CFLAGS_REL64 = -O3 -m64 LUA = luax51.bat SRC_SRC = cpu.cpp dumper.cpp tmpalloc.cpp memmgr.cpp -SRC_UNITTEST = test_unittest.cpp test_main.cpp test_fnctypeinfo.cpp +SRC_UNITTEST = test_unittest.cpp test_main.cpp test_fnctypeinfo.cpp \ + test_adt_slice.cpp OBJDIR = _objs LIBDIR = _lib BINDIR = _bin diff --git a/tests/test_adt_slice.cpp b/tests/test_adt_slice.cpp new file mode 100644 index 0000000..30bbe5d --- /dev/null +++ b/tests/test_adt_slice.cpp @@ -0,0 +1,30 @@ +#include "jitcs_adt_slice.h" +#include "unittest.h" + +using namespace jitcs; + +static void test(UnitTest& t) { + size_t data[5] = {0x12345678, 0xdeadbeef, 0x1, ~(size_t)0, 0}; + Slice arr; + t.check("Slice/0", arr.size() == 0); + Slice::iterator b = arr.begin(), e = arr.end(); + t.check("Slice/1", b == e); + + arr = Slice(data, 5); + t.check("Slice/2", arr.size() == 5 && arr._ptr == data); + t.check("Slice/3", arr.isValidIndex(4) && !arr.isValidIndex(5)); + t.check("Slice/4", arr[1] == 0xdeadbeef); + + b = arr.begin(), e = arr.end(); + t.check("Slice/5", b != e); + ++b; + t.check("Slice/6", *b == 0xdeadbeef); + ++b; + ++b; + ++b; + t.check("Slice/7", b != e); + ++b; + t.check("Slice/8", b == e); +} + +static UnitTestRun _("ADT/Slice", test); -- 2.11.4.GIT