array wrapper
Hello,
thank you for maintaining gcc and libraries.
Inspired a little by libstdc++5-doc:
/usr/share/doc/gcc-3.2-base/libstdc++/html/23_containers/wrappers_h.txt
I use attached array wrapper.
Probably it could be useful for others, but don't know where to post
it.
It is nice to erite in my numerical program
WALK_THROUGH_WITH(Elements, e) {
// compute something on element *e
}
instaed of fidling with pointers. I pay nothing for it, no run time,
no memory.
Best regards
Hans
--
You can find a Vim desktop calendar here: http://www.moolenaar.net/
Happy Vimming in 2003!
/*
* Demo of the not so big generalization (templating) of the array;
* Hans Ginzel <hans@matfyz.cz>
* created: 7.1.2003
*/
#include <iostream>
#include "array.h"
#define E_MEM 5
int compare_int(const void *aa, const void *bb) {
const int *a = (const int *)aa,
*b = (const int *)bb;
return *a < *b ? -1
: *a > *b ? 1
: 0;
}
int main() {
TArray<int, unsigned int> A;
if (A.Init(5)==0) {
cerr << "Cannot allocate memory for Container" << endl;
return E_MEM;
}
cout << "A.size = " << A.size() << endl;
A[3]=5;
cout << "A[3] = " << A[3] << endl;
int i=10;
for (typeof(A.begin()) c = A.begin(); A.eof(c); A.next(c), i--) {
*c = i*i;
cout << *c << endl;
if (i==7) cerr << "Item " // n-th element, indexing from zero
<< A.index(c) << " contains error." << endl;
}
A.sort(&compare_int);
cout << endl << "Sorted: " << endl;
WALK_THROUGH_WITH(A, c) { cout << *c << endl; }
typeofAI(A) n = A.index(1);
unsigned int o = 2;
cout << endl << "A[1] = " << *n
<< "\t A[2+2] = " << *A.offset(n, o) << endl;
return 0;
}
// vi: set ts=4 sw=4 nowrap:
/*
* Not so big generalization (templating) of the array;
* without any peformance or memory lost.
*
* Hans Ginzel <hans@matfyz.cz>
* created: 7.1.2003
*
* Probably use -O3 or -finline-functions by compiling.
* See /usr/share/doc/gcc-3.2-base/libstdc++/html/23_containers/wrappers_h.txt
*
* This header file is free software WITHOUT ANY WARANTY;
* you can redistribute it and/or modify it under the terms of the
* GNU General Public License (http://www.gnu.org/copyleft/gpl.html).
*
*/
#ifndef ARRAY_H
#define ARRAY_H
#include <stdlib.h>
#define ptr_int long
template <class T, class N>
class TArray {
private:
N _n;
T *_p;
public:
TArray() { _p = 0; _n = 0; }
~TArray() { if (_p!=0) { delete[] _p; } }
inline N Init (const N n) { _p = new T[n];
return _n = _p==0 ? 0 : n; }
inline T * begin () const { return _p; }
inline T * end () const { return _p + _n; }
inline bool begof (const T *p) const { return p < _p; } // prior to begin of array
inline bool eof (const T *p) const { return p >= _p + _n; } // beyond the end of array
inline T * next (T *&p) const { return p++; }
inline T * prev (T *&p) const { return p--; }
inline T * succ (T *&p) const { return p+1; }
inline T * prec (T *&p) const { return p-1; }
inline T * first () const { return _p; }
inline T * last () const { return _p + _n - 1; }
inline T & operator [] (N const i) { return _p[i]; }
inline T * index (const N n) const { return _p + n; }
inline N index (T *p) const { return (N)(p - _p); }
inline N size () const { return _n; }
inline T * offset (T *p,
const signed ptr_int o) const { return p + o; }
inline void sort () { qsort(_p, _n, sizeof(T), &T::compare); } // Declare compare() static in T!
inline void sort ( int(*compare)(const void *, const void *) )
{ qsort(_p, _n, sizeof(T), compare); }
inline void sort ( T *start, N count )
{ qsort(start, count, sizeof(T), &T::compare); }
inline void sort ( T *start, N count,
int(*compare)(const void *, const void *) )
{ qsort(start, count, sizeof(T), compare); }
};
#define typeofAI(A) typeof((A).begin()) // typeof(Array.Item) *
#define WALK_THROUGH_WITH(A, v) for (typeofAI(A) (v) = (A).begin();\
! (A).eof((v)); A.next((v)))
#undef ptr_int
#endif /*ARRAY_H*/
// vi: set ts=4 sw=4 nowrap:
Reply to: