Skip to content

Commit

Permalink
Some refactoring of _hyListNumeric; added random replication to the u…
Browse files Browse the repository at this point in the history
…t_hyListNumeric.cpp; removed _hyListNumeric.cpp -- now directly pasted into the header file to make gcov work
  • Loading branch information
spond committed Feb 28, 2014
1 parent 74ab09d commit 22f7b28
Show file tree
Hide file tree
Showing 8 changed files with 280 additions and 304 deletions.
4 changes: 2 additions & 2 deletions contrib/gtest-1.7.0/cmake_install.cmake
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Install script for directory: /Volumes/TeraMonkey/Users/sergei/hyphy/contrib/gtest-1.7.0
# Install script for directory: /Users/sergei/hyphy/contrib/gtest-1.7.0

# Set the install prefix
IF(NOT DEFINED CMAKE_INSTALL_PREFIX)
Expand All @@ -12,7 +12,7 @@ IF(NOT DEFINED CMAKE_INSTALL_CONFIG_NAME)
STRING(REGEX REPLACE "^[^A-Za-z0-9_]+" ""
CMAKE_INSTALL_CONFIG_NAME "${BUILD_TYPE}")
ELSE(BUILD_TYPE)
SET(CMAKE_INSTALL_CONFIG_NAME "")
SET(CMAKE_INSTALL_CONFIG_NAME "Release")
ENDIF(BUILD_TYPE)
MESSAGE(STATUS "Install configuration: \"${CMAKE_INSTALL_CONFIG_NAME}\"")
ENDIF(NOT DEFINED CMAKE_INSTALL_CONFIG_NAME)
Expand Down
6 changes: 3 additions & 3 deletions src/core/include/hy_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ class _hyList : public virtual BaseObj {
* @return A long
*/

virtual PAYLOAD Element(const long);
virtual PAYLOAD Element(const long) const;

/**
* Checks if list is identical to other list
Expand Down Expand Up @@ -329,14 +329,14 @@ class _hyList : public virtual BaseObj {
* @return Nothing. Acts on the List object it was called from.
*/
inline void Swap (const unsigned long, const unsigned long); //swap two elements
virtual BaseRef toStr(void);
virtual BaseRef toStr(void) const;

/**
* Request space for this many elements
* @param slots The number of elements to allocate the space for
*/

void RequestSpace(const unsigned long);
void RequestSpace(const unsigned long, bool = false);
/**
* Trim excess memory from the list allocation
*/
Expand Down
233 changes: 211 additions & 22 deletions src/core/include/hy_list_numeric.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,27 +63,28 @@ class _hyListNumeric : public _hyListOrderable<PAYLOAD> {
//does nothing
_hyListNumeric();

//_hyList constructor
_hyListNumeric(const unsigned long);

// stack copy contructor
_hyListNumeric(const _hyListNumeric <PAYLOAD>&, const long = 0UL, const long = HY_LIST_INSERT_AT_END);

// data constructor (1 member list)
_hyListNumeric(const PAYLOAD);

// arithmetic series populator: size, first item, step
// data constructor from _hyListOrderable

_hyListNumeric(const _hyListOrderable<PAYLOAD>&);

// arithmetic series populator: size, first item, step
_hyListNumeric(const unsigned long, const PAYLOAD, const PAYLOAD);


/**
* Data constructor list of longs supplied as a variable
* @param long the first string to add to the list
* @param const unsigned long the number of additional long arguments supplied
* to the constructor
* @param 2-N: long to be added to the list
* Data constructor from a static list of PAYLOAD objects
* @param const unsigned long the number of PAYLOAD arguments supplied
* to the constructor in the second argument
* @param const PAYLOAD: the static list of items to pass to the constructor
*/
_hyListNumeric(const PAYLOAD, const unsigned long, ...);

_hyListNumeric (const unsigned long, const PAYLOAD []);

/**
* Populate a _hyListNumeric with integers incrementally.
Expand All @@ -97,7 +98,7 @@ class _hyListNumeric : public _hyListOrderable<PAYLOAD> {


/* instead of throwing an error, returns the missing value */
virtual PAYLOAD Element(const long);
virtual PAYLOAD Element(const long) const;

/**
* SLKP: 20090508
Expand All @@ -116,18 +117,16 @@ class _hyListNumeric : public _hyListOrderable<PAYLOAD> {
*/
void Offset(const PAYLOAD);

/**
* Appends a number to the passed string buffer
* @return void
*/
void AppendNumtoStr(_StringBuffer*, const PAYLOAD) const;




/**
* Convert (a sorted) list into a partition string for consumption by datafilters etc
* Example: _hyListNumeric(1,2,3,7,8,9,10).ListToPartitionString() = "1-3,7-10"
* @return The partition string
*/
_String* ListToPartitionString(void) const;
// define this only for "long" PAYLOADS
_StringBuffer * ListToPartitionString(void) const;


/**
Expand All @@ -142,13 +141,203 @@ class _hyListNumeric : public _hyListOrderable<PAYLOAD> {
*
*/

_hyListNumeric <PAYLOAD> *CountingSort(PAYLOAD, _hyListNumeric <long> * = nil);
_hyListNumeric <PAYLOAD> CountingSort(PAYLOAD, _hyListNumeric <long> * = nil);
virtual BaseRef toStr() const;

private:

/**
* Appends a number to the passed string buffer
* @return void
*/
void AppendNumtoStr(_StringBuffer*, const PAYLOAD) const;

};

virtual BaseRef toStr();
//#include "hy_list_numeric.cpp"

};
// FUNCTION DEFINITIONS -- moved here so that gcov can view them

// Does nothing
template<typename PAYLOAD>
_hyListNumeric<PAYLOAD>::_hyListNumeric() {
}

//Data constructor (1 member list)
template<typename PAYLOAD>
_hyListNumeric<PAYLOAD>::_hyListNumeric(const PAYLOAD item) : _hyListOrderable<PAYLOAD> (item) {
}


//Stack copy contructor
template<typename PAYLOAD>
_hyListNumeric<PAYLOAD>::_hyListNumeric(const _hyListNumeric <PAYLOAD> &l, const long from, const long to)
{
this->Clone(&l, from, to); }

// Data constructor from array
template<typename PAYLOAD>
_hyListNumeric<PAYLOAD>::_hyListNumeric(const unsigned long number, const PAYLOAD items[]) :
_hyListOrderable<PAYLOAD> (number, items) {
}

// Does nothing
template<typename PAYLOAD>
_hyListNumeric<PAYLOAD>::_hyListNumeric(const _hyListOrderable<PAYLOAD>& source):_hyListOrderable<PAYLOAD> (source) {
}

template<typename PAYLOAD>
_hyListNumeric<PAYLOAD>::_hyListNumeric (const unsigned long l, const PAYLOAD start, const PAYLOAD step) {
this->Initialize(false);
Populate (l, start, step);
}


template<typename PAYLOAD>
PAYLOAD _hyListNumeric<PAYLOAD>::Element(const long index) const {
if (this->lLength) {
if (index >= 0L && (unsigned long)index < this->lLength) {
return this->lData[index];
} else if ((unsigned long)(-index) <= this->lLength) {
return this->lData[this->lLength - (-index)];
}
}
return _HY_LIST_NUMERIC_INVALID_VALUE_;
}

template<typename PAYLOAD>
PAYLOAD _hyListNumeric<PAYLOAD>::Sum(void) const {
PAYLOAD sum = 0L;
for (unsigned long k = 0UL; k < this->lLength; k++) {
sum += this->lData[k];
}
return sum;
}

template<typename PAYLOAD>
void _hyListNumeric<PAYLOAD>::Offset(const PAYLOAD shift) {
for (unsigned long k = 0UL; k < this->lLength; k++) {
this->lData[k] += shift;
}
}

template<typename PAYLOAD>
void _hyListNumeric<PAYLOAD>::Populate (const unsigned long l, const PAYLOAD start, const PAYLOAD step) {
this->RequestSpace (l);
PAYLOAD current_value = start;
for (unsigned long k = 0UL; k < l; k++, current_value+=step) {
this->lData[k] = current_value;
}

this->lLength = l;
}


template<typename PAYLOAD>
void _hyListNumeric<PAYLOAD>::AppendNumtoStr(_StringBuffer* s, PAYLOAD num) const{

char buffer[128];
snprintf(buffer, 127, "%ld", long (num));
(*s) << buffer;
}


//Char* conversion
template<typename PAYLOAD>
BaseRef _hyListNumeric<PAYLOAD>::toStr(void) const {

if (this->countitems()) {
_StringBuffer* s = new _StringBuffer();

(*s) << '{';

for (unsigned long i = 0UL; i<this->lLength; i++) {
this->AppendNumtoStr(s, this->lData[i]);
if (i<this->lLength-1) {
(*s) << ',';
}
}
(*s) << '}';

return s;
} else {
return new _String ("{}");
}
}


template<typename PAYLOAD>
_StringBuffer* _hyListNumeric<PAYLOAD>::ListToPartitionString (void) const {

_StringBuffer *result = new _StringBuffer (64UL);

for (unsigned long k=0UL; k<this->lLength; k++) {
unsigned long m;
for (m=k+1UL; m<this->lLength; m++)
if (this->lData[m]-this->lData[m-1]!=1L) {
break;
}
if (m>k+2) {
this->AppendNumtoStr(result, this->lData[k]);
(*result) << '-';
this->AppendNumtoStr(result, this->lData[m-1]);
if (m<this->lLength) {
(*result) << ',';
}
k = m-1;
} else {
this->AppendNumtoStr(result, this->lData[k]);
if (k<this->lLength-1) {
(*result) << ',';
}
}
}
return result;
}

template<typename PAYLOAD>
_hyListNumeric <PAYLOAD> _hyListNumeric<PAYLOAD>::CountingSort (PAYLOAD upperBound, _hyListNumeric <long> * ordering)
{
if (ordering) {
ordering->Clear();
}

_hyListNumeric<PAYLOAD> result;

#include "hy_list_numeric.cpp"
if (this->lLength) {
if (upperBound == _HY_LIST_NUMERIC_INVALID_VALUE_) {
upperBound = this->Max()+1UL;
}

_hyListNumeric<PAYLOAD> count;

count.RequestSpace (upperBound+1UL);
result.RequestSpace (this->lLength, true);


count.Populate(upperBound, 0UL, 0UL);

for (unsigned long pass1 = 0UL; pass1 < this->lLength; pass1++) {
count[this->lData[pass1]]++;
}

for (unsigned long pass2 = 1UL; pass2 < upperBound; pass2++) {
count[pass2] += count[pass2-1];
}

if (ordering) {
ordering->Populate (this->lLength, 0UL, 0UL);
for (long pass3 = this->lLength-1; pass3 >=0L; pass3--) {
result[--count[this->Element(pass3)]] = this->Element(pass3);
(*ordering)[count.Element(this->Element(pass3))] = pass3;
}
} else {
for (long pass3 = this->lLength-1; pass3 >= 0L; pass3--) {
result[--count[this->Element(pass3)]] = this->Element(pass3);
}
}
}
return result;
}

#endif
2 changes: 1 addition & 1 deletion src/core/include/hy_strings.h
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ class _String : public BaseObj {
* Converts a good ole char*
* \n\n \b Example: \code string.toStr(); \endcode
*/
virtual BaseRef toStr(void);
virtual BaseRef toStr(void) const;

/**
* Return good ole char*
Expand Down
5 changes: 2 additions & 3 deletions src/core/strings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1040,9 +1040,8 @@ _Parameter _String::toNum(void) {
}

//Return good ole char*
BaseRef _String::toStr(void) {
AddAReference();
return this;
BaseRef _String::toStr(void) const{
return new _String (*this);
}

_Parameter _String::ProcessTreeBranchLength(void) {
Expand Down
9 changes: 6 additions & 3 deletions src/core/templates/hy_list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ void _hyList<PAYLOAD>::append_multiple(const PAYLOAD item, const unsigned long c
}

template<typename PAYLOAD>
void _hyList<PAYLOAD>::RequestSpace(const unsigned long slots)
void _hyList<PAYLOAD>::RequestSpace(const unsigned long slots, bool set_length)
{
if (slots > laLength) {
laLength = (slots / HY_LIST_ALLOCATION_CHUNK + 1) * HY_LIST_ALLOCATION_CHUNK;
Expand All @@ -253,6 +253,9 @@ void _hyList<PAYLOAD>::RequestSpace(const unsigned long slots)
checkPointer(lData = (PAYLOAD *)MemAllocate(laLength * sizeof(PAYLOAD)));
}
}
if (set_length) {
lLength = slots;
}
}

template<typename PAYLOAD>
Expand Down Expand Up @@ -499,7 +502,7 @@ void _hyList<PAYLOAD>::Duplicate(BaseRefConst theRef)
//Element location functions (0,llength - 1)
//Negative indices return offsets from the end of the list
template<typename PAYLOAD>
PAYLOAD _hyList<PAYLOAD>::Element(const long index)
PAYLOAD _hyList<PAYLOAD>::Element(const long index) const
{
if (index >= 0L && (unsigned long)index < lLength) {
return lData[index];
Expand Down Expand Up @@ -701,7 +704,7 @@ void _hyList<PAYLOAD>::Swap(const unsigned long i, const unsigned long j)
}

template<typename PAYLOAD>
BaseRef _hyList<PAYLOAD>::toStr(void)
BaseRef _hyList<PAYLOAD>::toStr(void) const
{
_StringBuffer * stringified = new _StringBuffer ();
(*stringified) << "_hyList with ";
Expand Down
Loading

0 comments on commit 22f7b28

Please sign in to comment.