[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Toon-members] TooN internal/allocator.hh internal/size_mismat...
From: |
Edward Rosten |
Subject: |
[Toon-members] TooN internal/allocator.hh internal/size_mismat... |
Date: |
Mon, 07 Sep 2009 11:13:15 +0000 |
CVSROOT: /cvsroot/toon
Module name: TooN
Changes by: Edward Rosten <edrosten> 09/09/07 11:13:15
Modified files:
internal : allocator.hh size_mismatch.hh vector.hh
Added files:
test : resize_test.cc
Log message:
Initial implementation of resizable vectors. This is not an attempt to
turn
TooN in to a general container library, so they do not retain their
data when
they are resized. Dynamic vectors are not resizable and will remain so
to
provide more guarantees than resizable vectors.
Resize happens on an explicit .resize() and assignment from a Vector or
operator. Currently assignment from unsized operators will not compile.
The syntax is:
Vector<Resizable> v; //Will be zero size by default.
v.resize(10); //Filled with junk (affected by TooN debugging options)
v = makeVector(1, 2, 3);
v = Zeros(6);
v = Ones; //This will not compile.
etc...
CVSWeb URLs:
http://cvs.savannah.gnu.org/viewcvs/TooN/internal/allocator.hh?cvsroot=toon&r1=1.33&r2=1.34
http://cvs.savannah.gnu.org/viewcvs/TooN/internal/size_mismatch.hh?cvsroot=toon&r1=1.5&r2=1.6
http://cvs.savannah.gnu.org/viewcvs/TooN/internal/vector.hh?cvsroot=toon&r1=1.49&r2=1.50
http://cvs.savannah.gnu.org/viewcvs/TooN/test/resize_test.cc?cvsroot=toon&rev=1.1
Patches:
Index: internal/allocator.hh
===================================================================
RCS file: /cvsroot/toon/TooN/internal/allocator.hh,v
retrieving revision 1.33
retrieving revision 1.34
diff -u -b -r1.33 -r1.34
--- internal/allocator.hh 10 Jul 2009 14:05:55 -0000 1.33
+++ internal/allocator.hh 7 Sep 2009 11:13:15 -0000 1.34
@@ -128,9 +128,14 @@
int size() const {
return Size;
}
+
+ void try_resize(int)
+ {}
+ template<class Op> void try_resize(const Operator<Op>&)
+ {}
};
-template<class Precision> struct VectorAlloc<-1, Precision> {
+template<class Precision> struct VectorAlloc<Dynamic, Precision> {
Precision * const my_data;
const int my_size;
@@ -162,9 +167,83 @@
delete[] my_data;
}
+ void try_resize(int)
+ {}
+
+ template<class Op> void try_resize(const Operator<Op>&)
+ {}
};
+template<class Precision> struct VectorAlloc<Resizable, Precision> {
+ protected:
+ Precision * my_data;
+ int my_size;
+
+
+ public:
+
+ VectorAlloc(const VectorAlloc& v)
+ :my_data(new Precision[v.my_size]), my_size(v.my_size)
+ {
+ for(int i=0; i < my_size; i++)
+ my_data[i] = v.my_data[i];
+ }
+
+ VectorAlloc()
+ :my_data(0), my_size(0)
+ {
+ }
+
+ VectorAlloc(int s)
+ :my_data(new Precision[s]), my_size(s)
+ {
+ debug_initialize(my_data, my_size);
+ }
+
+ template <class Op>
+ VectorAlloc(const Operator<Op>& op)
+ : my_data(new Precision[op.size()]), my_size(op.size())
+ {
+ debug_initialize(my_data, my_size);
+ }
+
+ int size() const {
+ return my_size;
+ }
+
+ ~VectorAlloc(){
+ delete[] my_data;
+ }
+
+ void resize(int s)
+ {
+ if(s != my_size)
+ {
+ //First, invalidate the old data
+ my_size=0;
+ delete[] my_data;
+
+ my_data = new Precision[s];
+ my_size = s;
+ }
+
+ debug_initialize(my_data, my_size);
+ }
+
+ void try_resize(int s)
+ {
+ resize(s);
+ }
+
+
+ template<class Op> void try_resize(const Operator<Op>& op)
+ {
+ resize(op.size());
+ }
+
+};
+
template<int S, class Precision> struct VectorSlice
{
int size() const {
@@ -182,6 +261,12 @@
template<class Op>
VectorSlice(const Operator<Op>& op) : my_data(op.data()) {}
+
+ void try_resize(int)
+ {}
+
+ template<class Op> void try_resize(const Operator<Op>&)
+ {}
};
template<class Precision> struct VectorSlice<-1, Precision>
@@ -199,6 +284,12 @@
int size() const {
return my_size;
}
+
+ void try_resize(int)
+ {}
+
+ template<class Op> void try_resize(const Operator<Op>&)
+ {}
};
Index: internal/size_mismatch.hh
===================================================================
RCS file: /cvsroot/toon/TooN/internal/size_mismatch.hh,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -b -r1.5 -r1.6
--- internal/size_mismatch.hh 29 Apr 2009 21:55:51 -0000 1.5
+++ internal/size_mismatch.hh 7 Sep 2009 11:13:15 -0000 1.6
@@ -33,16 +33,16 @@
// class to generate compile time error
// general case which doesn't exist
template<int Size1, int Size2>
-struct SizeMismatch;
+struct SizeMismatch_;
// special cases which do exist
template<int Size>
-struct SizeMismatch<Size,Size>{
+struct SizeMismatch_<Size,Size>{
static inline void test(int, int){}
};
template<int Size>
-struct SizeMismatch<-1,Size>{
+struct SizeMismatch_<Dynamic,Size>{
static inline void test(int size1, int size2){
if(size1!=size2){
#ifdef TOON_TEST_INTERNALS
@@ -56,7 +56,7 @@
};
template<int Size>
-struct SizeMismatch<Size,-1>{
+struct SizeMismatch_<Size,Dynamic>{
static inline void test(int size1, int size2){
if(size1!=size2){
#ifdef TOON_TEST_INTERNALS
@@ -70,7 +70,7 @@
};
template <>
-struct SizeMismatch<-1,-1>{
+struct SizeMismatch_<Dynamic,Dynamic>{
static inline void test(int size1, int size2){
if(size1!=size2){
#ifdef TOON_TEST_INTERNALS
@@ -89,7 +89,7 @@
}
template<int Size1, int Size2>
-struct SizeMismatch
+struct SizeMismatch_
{
static inline void test(int, int)
{
@@ -101,4 +101,14 @@
}
};
+template<int Size1, int Size2>
+struct SizeMismatch
+{
+ static inline void test(int s1, int s2)
+ {
+ SizeMismatch_< (Size1 == Dynamic || Size1 ==
Resizable)?Dynamic:Size1,
+ (Size2 == Dynamic || Size2 ==
Resizable)?Dynamic:Size2 >::test(s1, s2);
+ }
+};
+
}
Index: internal/vector.hh
===================================================================
RCS file: /cvsroot/toon/TooN/internal/vector.hh,v
retrieving revision 1.49
retrieving revision 1.50
diff -u -b -r1.49 -r1.50
--- internal/vector.hh 27 Jul 2009 16:39:43 -0000 1.49
+++ internal/vector.hh 7 Sep 2009 11:13:15 -0000 1.50
@@ -126,6 +126,8 @@
**/
template<int Size=Dynamic, typename Precision=DefaultPrecision, typename
Base=Internal::VBase>
struct Vector : public Base::template VLayout<Size, Precision> {
+protected:
+ using Base::template VLayout<Size, Precision>::try_resize;
public:
// sneaky hack: only one of these constructors will work with any given base
// class but they don't generate errors unless the user tries to use one of
them
@@ -230,6 +232,7 @@
/// operator = from copy
inline Vector& operator= (const Vector& from){
+ try_resize(from.size()); //This might do nothing
SizeMismatch<Size,Size>::test(size(), from.size());
const int s=size();
for(int i=0; i<s; i++){
@@ -241,6 +244,7 @@
/// operator = another Vector
template<int Size2, typename Precision2, typename Base2>
Vector<Size,Precision,Base >& operator= (const Vector<Size2,
Precision2, Base2>& from){
+ try_resize(from.size()); //This might do nothing
SizeMismatch<Size,Size2>::test(size(), from.size());
const int s=size();
for(int i=0; i<s; i++){
@@ -252,6 +256,7 @@
/// assignment from an Operator object
template <class Op>
inline Vector & operator=(const Operator<Op>& op){
+ try_resize(op); //This might do nothing
op.eval(*this);
return *this;
}
Index: test/resize_test.cc
===================================================================
RCS file: test/resize_test.cc
diff -N test/resize_test.cc
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ test/resize_test.cc 7 Sep 2009 11:13:15 -0000 1.1
@@ -0,0 +1,30 @@
+#include <TooN/TooN.h>
+using namespace std;
+using namespace TooN;
+
+int main()
+{
+ Vector<Resizable> r;
+ Vector<> v1(makeVector(1, 2));
+
+ r = v1;
+ cout << r << endl;
+ Vector<3> v2 = makeVector(2, 3, 4);
+ r = v2;
+ cout << r << endl;
+
+ r = Ones(10);
+
+ cout << r << endl;
+
+ r = makeVector(6, 7, 8, 9, 0);
+ cout << r << endl;
+
+ r.resize(2);
+ r[0]= 5;
+ r[1] = 6;
+
+ cout << r << endl;
+
+
+}
- [Toon-members] TooN internal/allocator.hh internal/size_mismat...,
Edward Rosten <=