1 | // $Id: Index.cc 1486 2008-09-09 21:17:19Z jari $ |
---|
2 | |
---|
3 | /* |
---|
4 | Copyright (C) 2008 Peter Johansson |
---|
5 | |
---|
6 | This file is part of the yat library, http://dev.thep.lu.se/yat |
---|
7 | |
---|
8 | The yat library is free software; you can redistribute it and/or |
---|
9 | modify it under the terms of the GNU General Public License as |
---|
10 | published by the Free Software Foundation; either version 3 of the |
---|
11 | License, or (at your option) any later version. |
---|
12 | |
---|
13 | The yat library is distributed in the hope that it will be useful, |
---|
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
16 | General Public License for more details. |
---|
17 | |
---|
18 | You should have received a copy of the GNU General Public License |
---|
19 | along with this program; if not, write to the Free Software |
---|
20 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
---|
21 | 02111-1307, USA. |
---|
22 | */ |
---|
23 | |
---|
24 | #include "Index.h" |
---|
25 | |
---|
26 | #include <cassert> |
---|
27 | |
---|
28 | namespace theplu { |
---|
29 | namespace yat { |
---|
30 | namespace utility { |
---|
31 | |
---|
32 | Index::Index(void) |
---|
33 | : index_(SmartPtr<const std::vector<size_t> >(new std::vector<size_t>)) |
---|
34 | {} |
---|
35 | |
---|
36 | |
---|
37 | Index::Index(size_t n) |
---|
38 | { |
---|
39 | std::vector<size_t>* vec = new std::vector<size_t>; |
---|
40 | vec->reserve(n); |
---|
41 | for (size_t i=0; i<n; ++i) |
---|
42 | vec->push_back(i); |
---|
43 | index_ = SmartPtr<const std::vector<size_t> >(vec); |
---|
44 | } |
---|
45 | |
---|
46 | |
---|
47 | Index::Index(const SmartPtr<const std::vector<size_t> >& sp) |
---|
48 | : index_(sp) {} |
---|
49 | |
---|
50 | |
---|
51 | Index::Index(const std::vector<size_t>& vec) |
---|
52 | : index_(utility::SmartPtr<const std::vector<size_t> > |
---|
53 | (new std::vector<size_t>(vec))) |
---|
54 | {} |
---|
55 | |
---|
56 | |
---|
57 | Index::Index(const Index& a, const Index& b) |
---|
58 | { |
---|
59 | std::vector<size_t>* vec = new std::vector<size_t>; |
---|
60 | vec->reserve(b.size()); |
---|
61 | for (size_t i=0; i<b.size(); ++i) { |
---|
62 | assert(b[i]<a.size()); |
---|
63 | vec->push_back(a[b[i]]); |
---|
64 | } |
---|
65 | index_ = SmartPtr<const std::vector<size_t> >(vec); |
---|
66 | } |
---|
67 | |
---|
68 | |
---|
69 | size_t Index::size(void) const |
---|
70 | { |
---|
71 | return index_->size(); |
---|
72 | } |
---|
73 | |
---|
74 | |
---|
75 | const std::vector<size_t>& Index::vector(void) const |
---|
76 | { |
---|
77 | return *index_; |
---|
78 | } |
---|
79 | |
---|
80 | |
---|
81 | const size_t& Index::operator[](size_t i) const |
---|
82 | { |
---|
83 | return (*index_)[i]; |
---|
84 | } |
---|
85 | |
---|
86 | }}} // of namespace utility, yat, and theplu |
---|