1/* $Id: CoinShallowPackedVector.cpp 1373 2011-01-03 23:57:44Z lou $ */
2// Copyright (C) 2000, International Business Machines
3// Corporation and others. All Rights Reserved.
4// This code is licensed under the terms of the Eclipse Public License (EPL).
5
6#if defined(_MSC_VER)
7// Turn off compiler warning about long names
8# pragma warning(disable:4786)
9#endif
10
11#include "CoinHelperFunctions.hpp"
12#include "CoinShallowPackedVector.hpp"
13
14//#############################################################################
15
16void
17CoinShallowPackedVector::clear()
18{
19 clearBase();
20 indices_ = NULL;
21 elements_ = NULL;
22 nElements_ = 0;
23}
24
25//#############################################################################
26
27CoinShallowPackedVector&
28CoinShallowPackedVector::operator=(const CoinPackedVectorBase & x)
29{
30 if (&x != this) {
31 indices_ = x.getIndices();
32 elements_ = x.getElements();
33 nElements_ = x.getNumElements();
34 CoinPackedVectorBase::clearBase();
35 CoinPackedVectorBase::copyMaxMinIndex(x);
36 try {
37 CoinPackedVectorBase::duplicateIndex();
38 }
39 catch (CoinError e) {
40 throw CoinError("duplicate index", "operator= from base",
41 "CoinShallowPackedVector");
42 }
43 }
44 return *this;
45}
46
47//#############################################################################
48
49CoinShallowPackedVector&
50CoinShallowPackedVector::operator=(const CoinShallowPackedVector & x)
51{
52 if (&x != this) {
53 indices_ = x.indices_;
54 elements_ = x.elements_;
55 nElements_ = x.nElements_;
56 CoinPackedVectorBase::clearBase();
57 CoinPackedVectorBase::copyMaxMinIndex(x);
58 try {
59 CoinPackedVectorBase::duplicateIndex();
60 }
61 catch (CoinError e) {
62 throw CoinError("duplicate index", "operator=",
63 "CoinShallowPackedVector");
64 }
65 }
66 return *this;
67}
68
69//#############################################################################
70
71void
72CoinShallowPackedVector::setVector(int size,
73 const int * inds, const double * elems,
74 bool testForDuplicateIndex)
75{
76 indices_ = inds;
77 elements_ = elems;
78 nElements_ = size;
79 CoinPackedVectorBase::clearBase();
80 try {
81 CoinPackedVectorBase::setTestForDuplicateIndex(testForDuplicateIndex);
82 }
83 catch (CoinError e) {
84 throw CoinError("duplicate index", "setVector",
85 "CoinShallowPackedVector");
86 }
87}
88
89//#############################################################################
90
91//-------------------------------------------------------------------
92// Default
93//-------------------------------------------------------------------
94CoinShallowPackedVector::CoinShallowPackedVector(bool testForDuplicateIndex) :
95 CoinPackedVectorBase(),
96 indices_(NULL),
97 elements_(NULL),
98 nElements_(0)
99{
100 try {
101 CoinPackedVectorBase::setTestForDuplicateIndex(testForDuplicateIndex);
102 }
103 catch (CoinError e) {
104 throw CoinError("duplicate index", "default constructor",
105 "CoinShallowPackedVector");
106 }
107}
108
109//-------------------------------------------------------------------
110// Explicit
111//-------------------------------------------------------------------
112CoinShallowPackedVector::CoinShallowPackedVector(int size,
113 const int * inds,
114 const double * elems,
115 bool testForDuplicateIndex) :
116 CoinPackedVectorBase(),
117 indices_(inds),
118 elements_(elems),
119 nElements_(size)
120{
121 try {
122 CoinPackedVectorBase::setTestForDuplicateIndex(testForDuplicateIndex);
123 }
124 catch (CoinError e) {
125 throw CoinError("duplicate index", "explicit constructor",
126 "CoinShallowPackedVector");
127 }
128}
129
130//-------------------------------------------------------------------
131// Copy
132//-------------------------------------------------------------------
133CoinShallowPackedVector::CoinShallowPackedVector(const CoinPackedVectorBase& x) :
134 CoinPackedVectorBase(),
135 indices_(x.getIndices()),
136 elements_(x.getElements()),
137 nElements_(x.getNumElements())
138{
139 CoinPackedVectorBase::copyMaxMinIndex(x);
140 try {
141 CoinPackedVectorBase::setTestForDuplicateIndex(x.testForDuplicateIndex());
142 }
143 catch (CoinError e) {
144 throw CoinError("duplicate index", "copy constructor from base",
145 "CoinShallowPackedVector");
146 }
147}
148
149//-------------------------------------------------------------------
150// Copy
151//-------------------------------------------------------------------
152CoinShallowPackedVector::CoinShallowPackedVector(const
153 CoinShallowPackedVector& x) :
154 CoinPackedVectorBase(),
155 indices_(x.getIndices()),
156 elements_(x.getElements()),
157 nElements_(x.getNumElements())
158{
159 CoinPackedVectorBase::copyMaxMinIndex(x);
160 try {
161 CoinPackedVectorBase::setTestForDuplicateIndex(x.testForDuplicateIndex());
162 }
163 catch (CoinError e) {
164 throw CoinError("duplicate index", "copy constructor",
165 "CoinShallowPackedVector");
166 }
167}
168
169//-------------------------------------------------------------------
170// Print
171//-------------------------------------------------------------------
172void CoinShallowPackedVector::print()
173{
174for (int i=0; i < nElements_; i++)
175 {
176 std::cout << indices_[i] << ":" << elements_[i];
177 if (i < nElements_-1)
178 std::cout << ", ";
179 }
180 std::cout << std::endl;
181}
182