depend: Sources.make
$(CXX) -MM $(cc) > depend
-
clean:
rm -f $(obs) $(staticlib)
tar cfz $(DNAME).tar.gz $(DDIR)/*
rm -rf $(DDIR)/
-TAGS:
+TAGS: $(ALLSOURCES)
etags -CT $(inl) $(cc) $(hh)
smat.cc matrix.cc choleski.cc vector.cc dstream.cc\
matdebug.cc scalar.cc
-templatecc=cursor.tcc list.tcc tsmat.tcc plist.tcc interval.tcc
+templatecc=cursor.tcc list.tcc tsmat.tcc plist.tcc interval.tcc\
+ pcursor.tcc
inl=findcurs.inl link.inl list.inl cursor.inl plist.inl
hh=cursor.hh pcursor.hh lgetopt.hh link.hh list.hh dstream.hh \
- string.hh stringutil.hh vray.hh textdb.hh textstr.hh assoc.hh\
+ string.hh stringutil.hh varray.hh textdb.hh textstr.hh assoc.hh\
findcurs.hh unionfind.hh compare.hh handle.hh matrix.hh\
smat.hh vsmat.hh vector.hh real.hh choleski.hh\
tsmat.hh tvsmat.hh plist.hh associter.hh fproto.hh\
MAJVER=1
MINVER=0
-PATCHLEVEL=18
+PATCHLEVEL=19
PACKAGENAME=flower
#PROFILEFLAG=-pg
obs=$(cc:.cc=.o)
staticlib=libflower.a
-
-DFILES=$(hh) $(cc) $(inl) $(templatecc) Makefile Variables.make make_version\
+ALLSOURCES=$(hh) $(cc) $(inl) $(templatecc)
+DFILES=$(ALLSOURCES) Makefile Variables.make make_version\
Sources.make TODO README
DDIR=$(DNAME)
#ifndef ASSOC_HH
#define ASSOC_HH
-#include "vray.hh"
+#include "varray.hh"
#include <assert.h>
template<class K, class V>
template<class K, class V>
struct Assoc {
- svec< Assoc_ent_<K,V> > arr;
+ Array< Assoc_ent_<K,V> > arr;
/****************/
int find(K key) const {
- for (int i = 0; i < arr.sz(); i++) {
+ for (int i = 0; i < arr.size(); i++) {
if (!arr[i].free && key == arr[i].key)
return i;
}
}
int find_creat(K key) {
int free = -1;
- for (int i = 0; i < arr.sz(); i++) {
+ for (int i = 0; i < arr.size(); i++) {
if (key == arr[i].key) {
return i;
} else if (arr[i].free ) {
ae.free = false;
ae.key = key;
arr.add(ae);
- return arr.sz() -1;
+ return arr.size() -1;
}
public:
bool elt_query(K key) const {
i= next(0);
}
int next(int j) {
- while (j < assoc_.arr.sz() && assoc_.arr[j].free)
+ while (j < assoc_.arr.size() && assoc_.arr[j].free)
j++;
return j;
}
bool ok() const {
- return i < assoc_.arr.sz();
+ return i < assoc_.arr.size();
}
void OK()const {
assert(!ok() || !assoc_.arr[i].free);
Text_db cfg(fn);
while (! cfg.eof()){
Text_record r( cfg++);
- if (r.sz() != 2) {
+ if (r.size() != 2) {
r.message("not enough fields in Dstream init.");
continue;
}
#ifndef FPROTO_HH
#define FPROTO_HH
-template<class T> struct svec;
+template<class T> struct Array;
template<class T> struct sstack;
template<class T,class K> struct Assoc;
template<class T> struct List;
#ifndef SMAT_HH
#define SMAT_HH
-#include "vray.hh"
+#include "varray.hh"
#include "vsmat.hh"
#include "real.hh"
/// simplest matrix storage. refer to its baseclass for the doco.
assert(valid(i,j));
return els[i][j];
}
- virtual svec<Real> row(int i) const;
- virtual svec<Real> column(int j) const;
+ virtual Array<Real> row(int i) const;
+ virtual Array<Real> column(int j) const;
Full_storage() {
init();
#ifndef SSTACK_HH
#define SSTACK_HH
-#include "vray.hh"
+#include "varray.hh"
-/// A simple stack based on svec.
+/// A simple stack based on Array.
template<class T>
-struct sstack : svec<T> {
+struct sstack : Array<T> {
T top() { return last(); }
T pop() {
assert(!empty());
T l = last();
- svec<T>::pop();
+ Array<T>::pop();
return l;
}
void push(T l) { add(l); }
};
/**
- Same as for #svec# goes here.
+ Same as for #Array# goes here.
*/
{
while (1) {
String s;
- svec<String> fields;
+ Array<String> fields;
assert(!eof());
while ((s = get_word()) != "")
if (get_line() != "")
assert(false);
- assert (fields.sz());
+ assert (fields.size());
return Text_record(fields, get_name(), line());
}
}
/**: do "#" comments, read quote enclosed fields */
-/// a "const" svec. Contents can't be changed.
-class Text_record : svec<String>
+/// a "const" Array. Contents can't be changed.
+class Text_record : Array<String>
{
int line_no;
String filename;
cerr << '\n'<< filename << ": "<< line_no << s << "\n";
}
String operator[](int j) {
- return svec<String>::operator[](j);
+ return Array<String>::operator[](j);
}
- Text_record(svec<String> s, String fn, int j) : svec<String>(s) {
+ Text_record(Array<String> s, String fn, int j) : Array<String>(s) {
filename = fn; line_no = j;
}
- svec<String>::sz;
+ Array<String>::size;
};
/// abstraction for a datafile
#ifndef SMAT_HH
#define SMAT_HH
-#include "vray.hh"
+#include "varray.hh"
#include "vsmat.hh"
/// simplest matrix storage. refer to its baseclass for the doco.
assert(valid(i,j));
return els[i][j];
}
- virtual svec<T> row(int i) const;
- virtual svec<T> column(int j) const;
+ virtual Array<T> row(int i) const;
+ virtual Array<T> column(int j) const;
Full_storage() {
init();
#ifndef VSMAT_HH
#define VSMAT_HH
-#include "vray.hh"
+#include "varray.hh"
/// a matrix storage baseclass.
class virtual_smat {
virtual const Real& elem(int i, int j) const = 0;
#if 1
- virtual svec<Real> row(int i) const = 0;
- virtual svec<Real> column(int j) const = 0;
+ virtual Array<Real> row(int i) const = 0;
+ virtual Array<Real> column(int j) const = 0;
#endif
/// add a row
#ifndef UNIONFIND_HH
#define UNIONFIND_HH
-#include "vray.hh"
+#include "varray.hh"
/// which points of a graph are connected?
struct Union_find {
Union_find(int sz);
private:
- svec<int> classes;
+ Array<int> classes;
};
/*
--- /dev/null
+/*
+ (c) Han-Wen Nienhuys 1995,96
+
+ Distributed under GNU GPL
+*/
+
+#ifndef ARRAY_H
+#define ARRAY_H
+#include <assert.h>
+
+/// copy a bare (C-)array from #src# to #dest# sized #count#
+template<class T>
+inline void arrcpy(T*dest, T*src, int count) {
+ for (int i=0; i < count ; i++)
+ *dest++ = *src++;
+}
+
+///scaleable array template, for T with def ctor.
+template<class T>
+class Array {
+protected:
+ /// maximum length of array.
+ int max;
+
+ /// the data itself
+ T *thearray;
+
+ /// stretch or shrink array.
+ void remax(int newmax) {
+ T* newarr = new T[newmax];
+ size_ = (newmax < size_) ? newmax : size_;
+ arrcpy(newarr, thearray, size_);
+
+ delete[] thearray;
+ thearray = newarr;
+ max = newmax;
+ }
+ int size_;
+
+public:
+ /// check invariants
+ void OK() const {
+ assert(max >= size_ && size_ >=0);
+ if (max) assert(thearray);
+ }
+ /// report the size_. See {setsize_}
+
+ int size() const { return size_; }
+
+ /// POST: size() == 0
+ void clear() { size_ = 0; }
+
+ Array() { thearray = 0; max =0; size_ =0; }
+
+ /// set the size_ to #s#
+ void set_size(int s) {
+ if (s >= max) remax(s);
+ size_ = s;
+ }
+ /** POST: size() == s.
+ Warning: contents are unspecified */
+
+ ~Array() { delete[] thearray; }
+
+ /// return a "new"ed copy of array
+ T* copy_array() const {
+ T* Tarray = new T[size_];
+ arrcpy(Tarray, thearray, size_);
+ return Tarray;
+ }
+ // depracated
+ operator T* () const {
+ return copy_array();
+ }
+ void operator=(Array const & src) {
+ set_size (src.size_);
+ arrcpy(thearray,src.thearray, size_);
+ }
+ Array(const Array & src) {
+ thearray = src.copy_array();
+ max = size_ = src.size_;
+ }
+
+ /// tighten array size_.
+ void precompute () { remax(size_); }
+
+ /// this makes Array behave like an array
+ T &operator[] (const int i) const {
+ assert(i >=0&&i<size_);
+ return ((T*)thearray)[i];
+ }
+
+ /// add to the end of array
+ void add(T x) {
+ if (size_ == max)
+ remax(2*max + 1);
+
+ // T::operator=(T &) is called here. Safe to use with automatic
+ // vars
+ thearray[size_++] = x;
+ }
+
+ /// junk last entry.
+ void pop() { size_ -- ; }
+
+ /// return last entry
+ T& last(int j=0) {
+ return (*this)[size_-j-1];
+ }
+ T last(int j=0) const {
+ return (*this)[size_-j-1];
+ }
+ void swap (int i,int j) {
+ T t((*this)[i]);
+ (*this)[i]=(*this)[j];
+ (*this)[j]=t;
+ }
+ bool empty() { return !size_; }
+ void insert(T k, int j) {
+ assert(j >=0 && j<= size_);
+ set_size(size_+1);
+ for (int i=size_-1; i > j; i--)
+ thearray[i] = thearray[i-1];
+ thearray[j] = k;
+ }
+ void del(int i) {
+ assert(i >=0&& i < size_);
+ arrcpy(thearray+i, thearray+i+1, size_-i-1);
+ size_--;
+ }
+ // quicksort.
+ void sort (int (*compare)(T& , T& ),
+ int lower = -1, int upper = -1 ) {
+ if (lower < 0) {
+ lower = 0 ;
+ upper = size()-1;
+ }
+ if (lower >= upper)
+ return;
+ swap(lower, (lower+upper)/2);
+ int last = lower;
+ for (int i= lower +1; i <= upper; i++)
+ if (compare(thearray[i], thearray[lower]) < 0 )
+ swap( ++last,i);
+ swap(lower, last);
+ sort(compare, lower, last-1);
+ sort(compare, last+1, upper);
+ }
+ void concat(Array<T> const &src) {
+ int s = size_;
+ set_size(size_ + src.size_);
+ arrcpy(thearray+s,src.thearray, src.size_);
+ }
+ Array<T> subvec(int lower, int upper) {
+ assert(lower >= 0 && lower <=upper&& upper <= size_);
+ Array<T> r;
+ int s =upper-lower;
+ r.set_size(s);
+ arrcpy(r.thearray, thearray + lower, s);
+ return r;
+ }
+};
+/**
+
+ This template implements a scaleable vector. With (or without) range
+ checking. It may be flaky for objects with complicated con- and
+ destructors. The type T should have a default constructor. It is
+ best suited for simple types, such as int, double or String
+
+ */
+
+#endif
#include <math.h>
#include "real.hh"
-#include "vray.hh"
+#include "varray.hh"
class Dstream;
class String;
/// a row of numbers
class Vector {
- svec<Real> dat;
+ Array<Real> dat;
public:
void OK() const { dat.OK();}
- int dim() const { return dat.sz(); }
+ int dim() const { return dat.size(); }
Vector() { }
Vector(const Vector&n);
Vector(int n) {
Real norm_sq() {
return ((*this) * (*this));
}
- operator svec<Real> () { return dat; }
+ operator Array<Real> () { return dat; }
void print() const;
/// set to j-th element of unit-base
void set_unit(int j) ;
};
/**
- a vector. Storage is handled in svec, Vector only does the mathematics.
+ a vector. Storage is handled in Array, Vector only does the mathematics.
*/
inline Vector
#ifndef VSMAT_HH
#define VSMAT_HH
-#include "vray.hh"
+#include "varray.hh"
#include "real.hh"
/// a matrix storage baseclass.
class virtual_smat {
virtual const Real& elem(int i, int j) const = 0;
#if 1
- virtual svec<Real> row(int i) const = 0;
- virtual svec<Real> column(int j) const = 0;
+ virtual Array<Real> row(int i) const = 0;
+ virtual Array<Real> column(int j) const = 0;
#endif
/// add a row