SNYP40A1
November 25th, 2008, 10:27 PM
I am writing code to translate an integer vector (not vector<int>, but a slightly different "proprietary" datatype -- I am programming a tester and the tester API for some reason thought it was necessary to invent a new datatype for a vector, but it's basically a vector as far as I can tell. So I have a very long array of about 4 million integers (which will be only 1's and 0's) and I want to translate that into a C++ string of 1's and 0's. Here is what I am currently doing:
bool CFuncTest::getResult(std::string pinname, std::string &result)
{
result.clear();
result.reserve(4000500);
int i = 0;
if(acquireddatamap.find(pinname.c_str()) == acquireddatamap.end()) return false;
OFCArray<unsigned int>* capresultptr = &(acquireddatamap[pinname.c_str()]);
for(OFCArray<unsigned int>::iterator itr = capresultptr->begin(); itr != capresultptr->end(); itr++)
{
//if(*itr == 0) result += '0';
//else result += '1';
if(*itr == 0) ch[i] = '0';
else ch[i] = '1';
i++;
}
ch[i] = '\0';
result = ch;
return true;
}
Before, I was simply concatenating the data onto a class-variable string (class variable so that it is never popped off the stack and re-allocated later). And I set the capacity to just over 4M so hopefully the string should always retain that capacity so concatenation should not require memory reallocation. That's my hope. However, I found that it is actually faster to declare a character array once (char* class variable and dynamically allocate char array in constructor), then translate the array into the char array as shown above, then finally copy the char array into the string. However, is there a computationally more efficient way to do this assuming that I ultimately want the data in a string?
Here is the API for the OFCArray type shown above:
Public Types
typedef ELEMENTTYPE value_type
typedef value_type * pointer
typedef const value_type * const_pointer
typedef value_type & reference
typedef const value_type & const_reference
typedef OFCPtrIterator< value_type,
ptrdiff_t, pointer, reference,
pointer, reference > iterator
typedef OFCPtrIterator< value_type,
ptrdiff_t, pointer, reference,
const_pointer, const_reference > const_iterator
typedef OFCReverseIterator<
iterator > reverse_iterator
typedef OFCReverseIterator<
const_iterator > const_reverse_iterator
Public Methods
OFCArray ()
Default constructor: creates an empty array.
OFCArray (size_t numElements)
Constructor creates an array with the specified number of elements initialized with the default value.
OFCArray (size_t numElements, const ELEMENTTYPE &value)
Constructor creates an array with the specified number of elements initialized with the specified value.
OFCArray (const OFCArray< ELEMENTTYPE > &srcArray)
Copy constructor.
virtual ~OFCArray ()
Destructor.
void resize (size_t numElements)
Dynamically changes number of elements.
size_t size () const
Returns current number of elements.
size_t capacity () const
Returns a umber of pre-allocated elements.
void push_back (const ELEMENTTYPE &value)
Add new element to the end of the array.
void reserve (size_t count)
Reserves memory.
void clear ()
Empties out the array, eq. to resize(0).
void erase (size_t idx)
Erase specified element of the array.
ELEMENTTYPE & operator[] (size_t idx)
Array element accessor.
const ELEMENTTYPE & operator[] (size_t idx) const
Array element accessor.
OFCArray< ELEMENTTYPE > & operator= (const OFCArray< ELEMENTTYPE > &srcArray)
Assignment operator.
OFCArray< ELEMENTTYPE > & append (const OFCArray< ELEMENTTYPE > &srcArray)
Add elements of one array to the end of another.
const_iterator begin () const
Returns constant begin iterator.
iterator begin ()
Returns begin iterator.
const_iterator end () const
Returns constant end iterator.
iterator end ()
Returns end iterator.
const_reverse_iterator rbegin () const
Returns constant begin reverse iterator.
reverse_iterator rbegin ()
Returns begin reverse iterator.
const_reverse_iterator rend () const
Returns constant end reverse iterator.
reverse_iterator rend ()
Returns end reverse iterator.
bool CFuncTest::getResult(std::string pinname, std::string &result)
{
result.clear();
result.reserve(4000500);
int i = 0;
if(acquireddatamap.find(pinname.c_str()) == acquireddatamap.end()) return false;
OFCArray<unsigned int>* capresultptr = &(acquireddatamap[pinname.c_str()]);
for(OFCArray<unsigned int>::iterator itr = capresultptr->begin(); itr != capresultptr->end(); itr++)
{
//if(*itr == 0) result += '0';
//else result += '1';
if(*itr == 0) ch[i] = '0';
else ch[i] = '1';
i++;
}
ch[i] = '\0';
result = ch;
return true;
}
Before, I was simply concatenating the data onto a class-variable string (class variable so that it is never popped off the stack and re-allocated later). And I set the capacity to just over 4M so hopefully the string should always retain that capacity so concatenation should not require memory reallocation. That's my hope. However, I found that it is actually faster to declare a character array once (char* class variable and dynamically allocate char array in constructor), then translate the array into the char array as shown above, then finally copy the char array into the string. However, is there a computationally more efficient way to do this assuming that I ultimately want the data in a string?
Here is the API for the OFCArray type shown above:
Public Types
typedef ELEMENTTYPE value_type
typedef value_type * pointer
typedef const value_type * const_pointer
typedef value_type & reference
typedef const value_type & const_reference
typedef OFCPtrIterator< value_type,
ptrdiff_t, pointer, reference,
pointer, reference > iterator
typedef OFCPtrIterator< value_type,
ptrdiff_t, pointer, reference,
const_pointer, const_reference > const_iterator
typedef OFCReverseIterator<
iterator > reverse_iterator
typedef OFCReverseIterator<
const_iterator > const_reverse_iterator
Public Methods
OFCArray ()
Default constructor: creates an empty array.
OFCArray (size_t numElements)
Constructor creates an array with the specified number of elements initialized with the default value.
OFCArray (size_t numElements, const ELEMENTTYPE &value)
Constructor creates an array with the specified number of elements initialized with the specified value.
OFCArray (const OFCArray< ELEMENTTYPE > &srcArray)
Copy constructor.
virtual ~OFCArray ()
Destructor.
void resize (size_t numElements)
Dynamically changes number of elements.
size_t size () const
Returns current number of elements.
size_t capacity () const
Returns a umber of pre-allocated elements.
void push_back (const ELEMENTTYPE &value)
Add new element to the end of the array.
void reserve (size_t count)
Reserves memory.
void clear ()
Empties out the array, eq. to resize(0).
void erase (size_t idx)
Erase specified element of the array.
ELEMENTTYPE & operator[] (size_t idx)
Array element accessor.
const ELEMENTTYPE & operator[] (size_t idx) const
Array element accessor.
OFCArray< ELEMENTTYPE > & operator= (const OFCArray< ELEMENTTYPE > &srcArray)
Assignment operator.
OFCArray< ELEMENTTYPE > & append (const OFCArray< ELEMENTTYPE > &srcArray)
Add elements of one array to the end of another.
const_iterator begin () const
Returns constant begin iterator.
iterator begin ()
Returns begin iterator.
const_iterator end () const
Returns constant end iterator.
iterator end ()
Returns end iterator.
const_reverse_iterator rbegin () const
Returns constant begin reverse iterator.
reverse_iterator rbegin ()
Returns begin reverse iterator.
const_reverse_iterator rend () const
Returns constant end reverse iterator.
reverse_iterator rend ()
Returns end reverse iterator.