📄 vector.h
字号:
/*------------------------------------------------------------------------------*
* File Name: vector.h *
* Creation: TD 4-16-03 *
* Purpose: Origin C header file for Origin basic Data Vector types *
* Copyright (c) OriginLab Corp. 2002 - 2007 *
* All Rights Reserved *
* Modifications:
*------------------------------------------------------------------------------*/
#ifndef _VECTOR_H
#define _VECTOR_H
#include <common.h>
#ifndef _STRING_H
#include <string.h> // Most likely will need strings
#endif // _STRING_H
#include <OC_types.h> // Structures used in Origin internal functions
#ifndef _WKSHEET_H
#include <Wksheet.h>
#endif // _WKSHEET_H
// SeeAlso vectorbase::GetSize and vectorbase::SetSize methods
#define GetUpperIndex GetUpperBound
#define GetLowerIndex GetLowerBound
#define SetUpperIndex SetUpperBound
#define SetLowerIndex SetLowerBound
enum {
RECT_WINDOW = 0
};
// Please note: Origin C is not yet a fully implmemnted C++ language. Only internal
// classes can be declared as C++ classes. User defined C++ classes can only be
// supported via user supplied DLLs.
/** >Composite Data Types
The Origin C vectorbase class is an abstract base class used for polymorphic handling
of vector and Dataset related template class types. Consequently, Origin C objects of
type vectorbase can not be constructed. Derived classes, such as vector and Dataset,
inherit vectorbase class methods and should be used instead.
*/
class vectorbase
{
protected:
/**#
Objects of type vectorbase can not be constructed in Origin C. vectorbase is an
abstract class used for polymorphic handling of vector and Dataset template
class types which inherit vectorbase class methods.
*/
vectorbase(); // Vectorbase default constructor, for internal use only.
public:
/**
Dynamically set the size of (number of elements in) a vectorbase derived object.
Example:
vector vV;
vV.SetSize( 361 );
for( int ii = 0; ii < 361; ii++ )
{
vV[ii] = cos(ii*PI/180);
}
Parameters:
nSize=Number of elements in re-sized vectorbase derived object
nGrowBy=Currently not used, users should omit from call to pass default -1
Return:
Returns TRUE on successful exit and FALSE on error.
SeeAlso:
Dataset::SetUpperBound
*/
BOOL SetSize(UINT nSize, int nGrowBy = -1); // Set the size of a vectorbase derived object.
/**
Get the size of (number of elements in) a vectorbase derived object.
Example:
vector vV;
vV.SetSize( 99 );
ASSERT( vV.GetSize() == 99 );
Return:
Returns the size of the vectorbase derived object.
SeeAlso:
vectorbase::GetUpperBound
*/
UINT GetSize() const; // Get the size of a vectorbase derived object.
/**
Get the upper index of the vectorbase range. Index values returned
are 0 based offsets. SetUpperBound is supported only for Datasets
(not vectors) while GetUpperBound is supported for all vectorbase
derived objects. This allows for creation of more general purpose
functions that can take vectorbase objects as arguments in place
of Dataset objects. Essentially, the upper bound can be read but
not written for vectors. Please note that GetUpperBound is always
GetSize - 1. GetUpperIndex is a synonym for GetUpperBound.
Example:
// Worksheet column Data1_A must exist prior to execution
int ii, jj, kk;
Dataset dsA("Data1_A");
double dSumA = 0;
jj = dsA.GetLowerBound();
kk = dsA.GetUpperBound();
for(ii = jj; ii <= kk; ii++)
dSumA += dsA[ii];
ASSERT( dsA.GetUpperIndex() == dsA.GetUpperBound() );
Return:
The upper display index of the vector (0 based offset)
SeeAlso:
Dataset::SetUpperBound, vectorbase::GetSize, vectorbase::SetSize, vectorbase::GetLowerBound, Dataset::SetLowerBound
*/
int GetUpperBound(); // Get the upper index of the Dataset display range.
/**
Get the lower index of the vectorbase range. Index values returned
are 0 based offsets. SetLowerBound is supported only for Datasets
(not vectors) while GetLowerBound is supported for all vectorbase
derived objects. This allows for creation of more general purpose
functions that can take vectorbase objects as arguments in place
of Dataset objects. Essentially, the lower bound can be read but
not written for vectors. If the vectorbase object is not a Dataset
then GetLowerBound should always return 0. GetLowerIndex is a synonym
for GetLowerBound.
Example:
// Worksheet column Data1_A must exist prior to execution
int ii, jj, kk;
Dataset dsA("Data1_A");
double dSumA = 0;
jj = dsA.GetLowerBound();
kk = dsA.GetUpperBound();
for(ii = jj; ii <= kk; ii++)
dSumA += dsA[ii];
ASSERT( dsA.GetLowerIndex() == dsA.GetLowerBound() );
Return:
The lower display index of the Dataset (0 based offset)
SeeAlso:
Dataset::SetLowerBound, vectorbase::GetSize, vectorbase::SetSize, vectorbase::GetUpperBound, Dataset::SetUpperBound
*/
int GetLowerBound(); // Get the lower index of the Dataset display range.
/**
Sort the elements of the vectorbase derived object. Not yet implemented for complex
Example:
vector<string> vV;
vV.SetSize(5);
vV[0]="-1";
vV[1]="5";
vV[2]="Hello";
vV[3]="";
vV[4]="GoodeBye";
vV.Sort();// sort Text
// here is a example to show to sort col(a) and put into col(b)
Worksheet wks("Data1");
Column colA(wks, 0);
vector va(colA); // copy data, skip all missing values on both ends
va.Sort(); // sort numeric
Dataset dsB(wks, 1);
dsB = va;
Parameters:
wOrder = Default value SORT_ASCENDING sorts in ascending order, SORT_DESCENDING sorts in descending order
bMissingValuesSmall = Input flag specifies whether missing values are considered to be the smallest
or largest possible value
*/
#if _OC_VER <= 0x0703
BOOL Sort(BOOL wOption = SORT_ASCENDING, BOOL bMissingValuesSmall = TRUE);
#endif
/**
Sort the elements of the vectorbase derived object and retrive the original indeces. Not yet implemented for complex
Example:
vector<uint> vnIndeces;
vector<double> vdWeight = {0, 0.1, 0.4, 0, 0.2};
vdWeight[0] = NANUM;
vdWeight[3] = NANUM;
vdWeight.Sort(SORT_ASCENDING, FALSE, vnIndeces);
//result vdWeight = 0.1 0.2 0.4 NANUM NANUM
//result vnIndeces = 1 4 2 0 3
Parameters:
wOrder = Default value SORT_ASCENDING sorts in ascending order, SORT_DESCENDING sorts in descending order
bMissingValuesSmall = Input flag specifies whether missing values are considered to be the smallest
or largest possible value
vnIndeces = Optional array to retrive the original indeces that corresponds to the sorted values
SeeAlso:
Reorder
*/
#if _OC_VER > 0x0703
BOOL Sort(BOOL wOption = SORT_ASCENDING, BOOL bMissingValuesSmall = TRUE, vector<uint>& vnIndeces = NULL);
/**
Rearrange the vector according to the given indeces
Parameters:
vnIndeces = 0 offset indeces to reorder vector. The indeces must be all unique and include all of the indeces in current vector
Example:
vector<uint> vn = {0,2,1};
vector<string> vs1 = {"AA", "BB", "CC"};
vs1.Reorder(vn);
// result vs1 = AA CC BB
// the following code sort one vector based on another
vector<uint> vnIndeces;
vector<string> vs1 = {"AA", "BB", "CC"};
vector<double> vdWeight = {0.1, 0.4, 0.2};
vdWeight.Sort(SORT_ASCENDING, TRUE, vnIndeces);
vs1.Reorder(vnIndeces);
// result vs1 = AA CC BB
*/
BOOL Reorder(const vector<uint>& vnIndeces);
#endif //_OC_VER > 0x0703
/**
Get the differences of a vector.
Example:
vector<int> vv;
vv.SetSize(4);
vv[0] = 23;
vv[1] = 12;
vv[2] = 7;
vv[3] = 28;
vector<int> vr;
BOOL bRet = vv.Difference(vr);
ASSERT(bRet);
ASSERT(vr[0] == vv[1] - vv[0]);
ASSERT(vr[1] == vv[2] - vv[1]);
ASSERT(vr[2] == vv[3] - vv[2]);
Parameters:
vecDiff will have the differences of target vector,
The length of vecDiff will be n-1 ,n is the length of target vector.
Returns:
Returns TRUE on successful exit and FALSE on error.
*/
BOOL Difference(vectorbase &vecDiff );
/**
Append data from a vectorbase derived object to this vectorbase derived object.
Example:
vector vV; // Initialize variables
Dataset dsB("Data1_B"); // Dataset Data1_B must exist in Origin
dsB.SetSize(10); // Initialize dataset
for(int ii = 0; ii < 10; ii++)
{
dsB[ii] = 1000 * ii;
}
vV.SetSize(5); // Initalize vector
for(ii = 0; ii < 5; ii++)
{
vV[ii] = 10 * ii;
}
dsB.Append(vV); // Append vector to dataset
Parameters:
vV=vector or Dataset to append
Returns:
Returns TRUE on successful exit and FALSE on error.
*/
BOOL Append(vectorbase &vv); // Append data to a vectobase derived object.
/**
Compute a sum of all the elements in this vector. The argument type must be
greater than or equal to the underlying base type of the vector.
Example:
int ii, nSum;
vector<short> vShort = {1,2,3,4,5};
printf("vShort:\n");
for(ii = 0; ii < vShort.GetSize(); ii++)
printf("%d\t",vShort[ii]);
printf("\n\n");
vShort.Sum(nSum);
printf("Sum of vShort is %d", nSum);
Parameters:
nSum=Output sum of all the elements in the vector
Return:
Returns 0 on success and a non-zero error code on failure.
SeeAlso:
Data_sum
*/
int Sum(int & nSum);
/**
Compute a sum of all the elements in this vector. The argument type must be
greater than or equal to the underlying base type of the vector.
Example:
int ii;
double dSum;
vector vDouble = {1.1,2.1,3.1,4.1,5.1};
printf("vDouble:\n");
for(ii = 0; ii < vDouble.GetSize(); ii++)
printf("%g\t",vDouble[ii]);
printf("\n\n");
vDouble.Sum(dSum);
printf("Sum of vDouble is %g", dSum);
Parameters:
dSum=Output sum of all the elements in the vector
Return:
Returns 0 on success and a non-zero error code on failure.
SeeAlso:
Data_sum
*/
int Sum(double & dSum);
/**
Compute a sum of all the elements in this vector. The argument type must be
greater than or equal to the underlying base type of the vector.
Example:
int ii;
complex cxSum;
vector<complex> vComplex = {1+2i,2-1i,3+4i,4-3i,5+2i};
printf("vComplex:\n");
for(ii = 0; ii < vComplex.GetSize(); ii++)
printf("%g+%gi\t",vComplex[ii].m_re,vComplex[ii].m_im);
printf("\n\n");
vComplex.Sum(cxSum);
printf("Sum of vComplex is %g+%gi", cxSum.m_re, cxSum.m_im);
Parameters:
cxSum=Output sum of all the elements in the vector
Return:
Returns 0 on success and a non-zero error code on failure.
SeeAlso:
Data_sum
*/
int Sum(complex & cxSum);
/**
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -