source: trunk/yat/classifier/DataLookup2D.cc @ 1000

Last change on this file since 1000 was 1000, checked in by Jari Häkkinen, 13 years ago

trac moved to new location.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date ID
File size: 3.6 KB
Line 
1// $Id$
2
3/*
4  Copyright (C) 2005 Markus Ringnér, Peter Johansson
5  Copyright (C) 2006 Jari Häkkinen, Markus Ringnér, Peter Johansson
6
7  This file is part of the yat library, http://trac.thep.lu.se/yat
8
9  The yat library is free software; you can redistribute it and/or
10  modify it under the terms of the GNU General Public License as
11  published by the Free Software Foundation; either version 2 of the
12  License, or (at your option) any later version.
13
14  The yat library is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  General Public License for more details.
18
19  You should have received a copy of the GNU General Public License
20  along with this program; if not, write to the Free Software
21  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
22  02111-1307, USA.
23*/
24
25#include "DataLookup2D.h"
26
27#include <vector>
28
29#include <cassert>
30#ifndef NDEBUG
31#include <algorithm>
32#endif
33
34namespace theplu {
35namespace yat {
36namespace classifier {
37
38
39  DataLookup2D::DataLookup2D(const bool own)
40    : ref_count_(NULL)
41  {
42    if (own)
43      ref_count_ = new u_int(1);
44  }
45
46
47  DataLookup2D::DataLookup2D(const DataLookup2D& m,
48                             const std::vector<size_t>& row, 
49                             const std::vector<size_t>& col)
50    : ref_count_(NULL)
51  {
52    assert(row_index_.empty());
53    row_index_.reserve(row.size());
54    for (size_t i=0; i<row.size(); i++) {
55      assert(row[i]<m.row_index_.size());
56      row_index_.push_back(m.row_index_[row[i]]);
57    }
58    assert(column_index_.empty());
59    column_index_.reserve(col.size());
60    for (size_t i=0; i<col.size(); i++) {
61      assert(col[i]<m.column_index_.size());
62      column_index_.push_back(m.column_index_[col[i]]);
63    }
64  }
65   
66
67
68  DataLookup2D::DataLookup2D(const DataLookup2D& m,
69                             const std::vector<size_t>& index, 
70                             const bool row)
71    : ref_count_(NULL)
72  {
73    if (row){
74      assert(row_index_.empty());
75      row_index_.reserve(index.size());
76      for (size_t i=0; i<index.size(); i++) {
77        assert(index[i]<m.row_index_.size());
78        row_index_.push_back(m.row_index_[index[i]]);
79      }
80      column_index_= m.column_index_;
81    }
82    else{
83      assert(column_index_.empty());
84      column_index_.reserve(index.size());
85      for (size_t i=0; i<index.size(); i++) {
86        column_index_.push_back(m.column_index_[index[i]]);
87      }
88      row_index_= m.row_index_;
89    }
90  }   
91
92
93  DataLookup2D::DataLookup2D(const std::vector<size_t>& row, 
94                             const std::vector<size_t>& col,
95                             const bool own)
96    : row_index_(row),column_index_(col), ref_count_(NULL)
97  {
98    if (own)
99      ref_count_ = new u_int(1);
100  }
101
102
103
104  DataLookup2D::DataLookup2D(const DataLookup2D& mv)
105    : row_index_(mv.row_index_),column_index_(mv.column_index_), 
106      ref_count_(NULL)
107  {
108  }
109
110
111  DataLookup2D::DataLookup2D(const size_t rows, const size_t columns)
112    : row_index_(std::vector<size_t>(rows,0)), 
113      column_index_(std::vector<size_t>(columns,0)), ref_count_(NULL)
114
115  {
116  } 
117
118
119  size_t DataLookup2D::columns(void) const
120  {
121    return column_index_.size();
122  }
123
124
125  size_t DataLookup2D::rows(void) const
126  {
127    return row_index_.size();
128  }
129
130
131  const DataLookup2D& DataLookup2D::operator=(const DataLookup2D& other)
132  {
133    // never copy yourself
134    if (this!=&other){
135      row_index_ = other.row_index_;
136      column_index_ = other.column_index_;
137    }
138    return *this;
139  }
140
141  std::ostream& operator<<(std::ostream& s, const DataLookup2D& m)
142  {
143    s.setf(std::ios::dec);
144    s.precision(12);
145    for(size_t i=0, j=0; i<m.rows(); i++)
146      for (j=0; j<m.columns(); j++) {
147        s << m(i,j);
148        if (j<m.columns()-1)
149          s << s.fill();
150        else if (i<m.rows()-1)
151          s << "\n";
152      }
153    return s;
154  }
155
156}}} // of namespace classifier, yat, and theplu
Note: See TracBrowser for help on using the repository browser.