From d10491efbd0b899ef6afb7323d3351b8b2d72025 Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Mon, 7 Jul 2014 15:20:21 +1000 Subject: [PATCH] pascal: add simple pascal string/array types --- Makefile.am | 2 + pascal.cpp | 127 ++++++++++++++++++++++++++++++++++++++++++++++++++++ pascal.hpp | 51 +++++++++++++++++++++ 3 files changed, 180 insertions(+) create mode 100644 pascal.cpp create mode 100644 pascal.hpp diff --git a/Makefile.am b/Makefile.am index b3495aed..c662b7ea 100644 --- a/Makefile.am +++ b/Makefile.am @@ -108,6 +108,8 @@ UTIL_FILES = \ noise/lut.hpp \ options.cpp \ options.hpp \ + pascal.cpp \ + pascal.hpp \ platform.hpp \ point.cpp \ point.hpp \ diff --git a/pascal.cpp b/pascal.cpp new file mode 100644 index 00000000..c8e2fd5e --- /dev/null +++ b/pascal.cpp @@ -0,0 +1,127 @@ +/* + * This file is part of libgim. + * + * libgim is free software: you can redistribute it and/or modify it under the + * terms of the GNU General Public License as published by the Free Software + * Foundation, either version 3 of the License, or (at your option) any later + * version. + * + * libgim is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License + * along with libgim. If not, see . + * + * Copyright 2010 Danny Robson + */ + +#include "pascal.hpp" + +#include + +using util::parray; + + +//----------------------------------------------------------------------------- +template +parray::parray (size_t _size, T *_data): + size (_size), + data (_data) +{ ; } + + +//----------------------------------------------------------------------------- +template +T& +parray::operator[] (size_t idx) { + return data[idx]; +} + + +template +const T& +parray::operator[] (size_t idx) const { + return data[idx]; +} + + +//----------------------------------------------------------------------------- +template +T& +parray::at (size_t idx) { + if (idx >= size) + throw std::out_of_range ("invalid index for parray"); + + return (*this)[idx]; +} + + +template +const T& +parray::at (size_t idx) const { + if (idx >= size) + throw std::out_of_range ("invalid index for parray"); + + return (*this)[idx]; +} + + +//----------------------------------------------------------------------------- +template +T* +parray::begin (void) { + return data; +} + + +template +T* +parray::end (void) { + return data + size; +} + + +//----------------------------------------------------------------------------- +template +const T* +parray::cbegin (void) const { + return data + size; +} + + +template +const T* +parray::cend (void) const { + return data + size; +} + + +//----------------------------------------------------------------------------- +template +std::ostream& +operator<< (std::ostream &os, parray p) { + os << "[" << p.size << ", " << std::hex << p.data << std::dec << "]"; + return os; +} + + +template std::ostream& operator<< (std::ostream&, parray); +template std::ostream& operator<< (std::ostream&, parray); +template std::ostream& operator<< (std::ostream&, parray); + +template std::ostream& operator<< (std::ostream&, parray); +template std::ostream& operator<< (std::ostream&, parray); +template std::ostream& operator<< (std::ostream&, parray); + +//----------------------------------------------------------------------------- +namespace util { + template struct parray; + template struct parray; + template struct parray; + + template struct parray; + template struct parray; + template struct parray; +} diff --git a/pascal.hpp b/pascal.hpp new file mode 100644 index 00000000..aa8ab931 --- /dev/null +++ b/pascal.hpp @@ -0,0 +1,51 @@ +/* + * This file is part of libgim. + * + * libgim is free software: you can redistribute it and/or modify it under the + * terms of the GNU General Public License as published by the Free Software + * Foundation, either version 3 of the License, or (at your option) any later + * version. + * + * libgim is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License + * along with libgim. If not, see . + * + * Copyright 2010 Danny Robson + */ + +#ifndef __UTIL_PASCAL_HPP +#define __UTIL_PASCAL_HPP + +#include +#include + +namespace util { + template + struct parray { + parray (size_t size, T *data); + + size_t size; + T *data; + + T& operator[] (size_t idx); + const T& operator[] (size_t idx) const; + + T& at (size_t idx); + const T& at (size_t idx) const; + + T* begin (void); + T* end (void); + + const T* cbegin (void) const; + const T* cend (void) const; + }; +} + +template +std::ostream& operator<< (std::ostream&, util::parray); + +#endif