1 | // $Id: Index.cc 1528 2008-09-24 15:46:28Z peter $ |
---|
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 yat. If not, see <http://www.gnu.org/licenses/>. |
---|
20 | */ |
---|
21 | |
---|
22 | #include "Index.h" |
---|
23 | |
---|
24 | #include <cassert> |
---|
25 | |
---|
26 | namespace theplu { |
---|
27 | namespace yat { |
---|
28 | namespace utility { |
---|
29 | |
---|
30 | Index::Index(void) |
---|
31 | : index_(SmartPtr<const std::vector<size_t> >(new std::vector<size_t>)) |
---|
32 | {} |
---|
33 | |
---|
34 | |
---|
35 | Index::Index(size_t n) |
---|
36 | { |
---|
37 | std::vector<size_t>* vec = new std::vector<size_t>; |
---|
38 | vec->reserve(n); |
---|
39 | for (size_t i=0; i<n; ++i) |
---|
40 | vec->push_back(i); |
---|
41 | index_ = SmartPtr<const std::vector<size_t> >(vec); |
---|
42 | } |
---|
43 | |
---|
44 | |
---|
45 | Index::Index(const SmartPtr<const std::vector<size_t> >& sp) |
---|
46 | : index_(sp) {} |
---|
47 | |
---|
48 | |
---|
49 | Index::Index(const std::vector<size_t>& vec) |
---|
50 | : index_(utility::SmartPtr<const std::vector<size_t> > |
---|
51 | (new std::vector<size_t>(vec))) |
---|
52 | {} |
---|
53 | |
---|
54 | |
---|
55 | Index::Index(const Index& a, const Index& b) |
---|
56 | { |
---|
57 | std::vector<size_t>* vec = new std::vector<size_t>; |
---|
58 | vec->reserve(b.size()); |
---|
59 | for (size_t i=0; i<b.size(); ++i) { |
---|
60 | assert(b[i]<a.size()); |
---|
61 | vec->push_back(a[b[i]]); |
---|
62 | } |
---|
63 | index_ = SmartPtr<const std::vector<size_t> >(vec); |
---|
64 | } |
---|
65 | |
---|
66 | |
---|
67 | Index::const_iterator Index::begin(void) const |
---|
68 | { |
---|
69 | return index_->begin(); |
---|
70 | } |
---|
71 | |
---|
72 | |
---|
73 | Index::const_iterator Index::end(void) const |
---|
74 | { |
---|
75 | return index_->end(); |
---|
76 | } |
---|
77 | |
---|
78 | |
---|
79 | size_t Index::size(void) const |
---|
80 | { |
---|
81 | return index_->size(); |
---|
82 | } |
---|
83 | |
---|
84 | |
---|
85 | const std::vector<size_t>& Index::vector(void) const |
---|
86 | { |
---|
87 | return *index_; |
---|
88 | } |
---|
89 | |
---|
90 | |
---|
91 | const size_t& Index::operator[](size_t i) const |
---|
92 | { |
---|
93 | return (*index_)[i]; |
---|
94 | } |
---|
95 | |
---|
96 | }}} // of namespace utility, yat, and theplu |
---|