Update to 2.0.0 tree from current Fremantle build
[opencv] / 3rdparty / include / flann / matrix.h
1 /***********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright 2008-2009  Marius Muja (mariusm@cs.ubc.ca). All rights reserved.
5  * Copyright 2008-2009  David G. Lowe (lowe@cs.ubc.ca). All rights reserved.
6  *
7  * THE BSD LICENSE
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  *************************************************************************/
30
31 #ifndef DATASET_H
32 #define DATASET_H
33
34 #include <stdio.h>
35 #include "random.h"
36
37
38 namespace flann
39 {
40 /**
41 * Class implementing a generic rectangular dataset.
42 */
43 template <typename T>
44 class Matrix {
45
46     /**
47     * Flag showing if the class owns its data storage.
48     */
49     bool ownData;
50
51     void shallow_copy(const Matrix& rhs)
52     {
53         data = rhs.data;
54         rows = rhs.rows;
55         cols = rhs.cols;
56         ownData = false;
57     }
58
59 public:
60     long rows;
61     long cols;
62     T* data;
63
64
65     Matrix(long rows_, long cols_, T* data_ = NULL) :
66          ownData(false), rows(rows_), cols(cols_), data(data_)
67         {
68         if (data_==NULL) {
69                     data = new T[rows*cols];
70             ownData = true;
71         }
72         }
73
74     Matrix(const Matrix& d)
75     {
76         shallow_copy(d);
77     }
78
79     const Matrix& operator=(const Matrix& rhs)
80     {
81         if (this!=&rhs) {
82             shallow_copy(rhs);
83         }
84         return *this;
85     }
86
87         ~Matrix()
88         {
89         if (ownData) {
90                   delete[] data;
91         }
92         }
93
94     /**
95     * Operator that return a (pointer to a) row of the data.
96     */
97     T* operator[](long index)
98     {
99         return data+index*cols;
100     }
101
102     T* operator[](long index) const
103     {
104         return data+index*cols;
105     }
106
107
108
109     Matrix<T>* sample(long size, bool remove = false)
110     {
111         UniqueRandom rand(rows);
112         Matrix<T> *newSet = new Matrix<T>(size,cols);
113
114         T *src,*dest;
115         for (long i=0;i<size;++i) {
116             long r = rand.next();
117             dest = (*newSet)[i];
118             src = (*this)[r];
119             for (long j=0;j<cols;++j) {
120                 dest[j] = src[j];
121             }
122             if (remove) {
123                 dest = (*this)[rows-i-1];
124                 src = (*this)[r];
125                 for (long j=0;j<cols;++j) {
126                     swap(*src,*dest);
127                     src++;
128                     dest++;
129                 }
130             }
131         }
132
133         if (remove) {
134             rows -= size;
135         }
136
137         return newSet;
138     }
139
140     Matrix<T>* sample(long size) const
141     {
142         UniqueRandom rand(rows);
143         Matrix<T> *newSet = new Matrix<T>(size,cols);
144
145         T *src,*dest;
146         for (long i=0;i<size;++i) {
147             long r = rand.next();
148             dest = (*newSet)[i];
149             src = (*this)[r];
150             for (long j=0;j<cols;++j) {
151                 dest[j] = src[j];
152             }
153         }
154
155         return newSet;
156     }
157
158 };
159
160
161 }
162
163 #endif //DATASET_H