Loading...
Searching...
No Matches
IntArray Class Reference
Inheritance diagram for IntArray:
[legend]

## Public Member Functions

| IntArray (int start_size=0)
| IntArray (const IntArray &source)
IntArray & | operator= (const IntArray &rhs)
int & | operator[] (int index)
int | operator[] (int index) const
int & | operator[] (char index)
int | operator[] (char index) const
int & | operator[] (double fraction)
int | operator[] (double fraction) const
int | Append (int value)
int | Append (const IntArray &rhs)
void | Push (int value)
int | Pop ()
int | Peek () const
int & | Last () const
void | PushIfNew (int value)
int | Delete (int index)
void | InsertAt (int index, int value)
int | Find (int value) const
int | FastFind (int value) const
int | BinarySearch (int value) const
void | Sort ()
void | Sort (IntArray &freeRider)
void | Zero ()
void | Set (int value)
void | SetSequence (int start=0, int increment=1)
int | Length () const
void | Dimension (int new_count)
void | Clear ()
int | Sum () const
int | Sum (int start) const
int | Sum (int start, int end) const
double | dSum () const
double | dSum (int start) const
double | dSum (int start, int end) const
int | SumProduct (const IntArray &weight) const
double | dSumProduct (const IntArray &weight) const
int | Max () const
int | Max (int start) const
int | Max (int start, int end) const
int | Min () const
int | Min (int start) const
int | Min (int start, int end) const
int | Count () const
int | CountIfGreater (int treshold) const
int | CountIfGreaterOrEqual (int treshold) const
void | Swap (int i, int j)
void | Reverse ()
| operator int * ()
void | Add (int term)
void | Subtract (int term)
void | Multiply (int factor)
void | Divide (int denominator)
void | Add (const IntArray &rhs)
IntArray & | operator+= (int rhs)
IntArray & | operator+= (const IntArray &rhs)
IntArray & | operator*= (int rhs)
IntArray & | operator-= (int rhs)
IntArray & | operator/= (int rhs)
int | InnerProduct (IntArray &v)
bool | operator== (const IntArray &rhs) const
bool | operator!= (const IntArray &rhs) const
bool | isAscending ()
bool | isDescending ()
void | Stack (const IntArray &rhs)
void | Swap (IntArray &rhs)
void | Print ()
void | Print (const char *label)
void | Print (FILE *output)
void | Print (FILE *output, const char *label)
int | Product ()
double | DoubleProduct ()
int | Hash (int initval=0)

## Static Public Attributes

static int | alloc = 4

## Detailed Description

Definition at line 23 of file IntArray.h.

## Constructor & Destructor Documentation

## IntArray() [1/2]

IntArray::IntArray | ( | int | start_size = 0 | ) |

Definition at line 27 of file IntArray.cpp.

{
count = start_size;
size = (count + alloc) / alloc * alloc;
items = new int [size];
}

## IntArray() [2/2]

IntArray::IntArray | ( | const IntArray & | source | ) |

Definition at line 34 of file IntArray.cpp.

{
count = source.count;
size = source.size;
items = new int [size];
for (int i = 0; i < count; i++)
items[i] = source.items[i];
}

## ~IntArray()

IntArray::~IntArray | ( |  | ) |

Definition at line 44 of file IntArray.cpp.

{
delete [] items;
}

## Member Function Documentation

## Add() [1/2]

void IntArray::Add | ( | const IntArray & | rhs | ) |

Definition at line 313 of file IntArray.cpp.

{
if (Length() != v.Length())
error("IntArray::Add - vectors have different lengths\n"
"IntArrays - Left[%d] += Right[%d] ",
Length(), v.Length());
for (int i = 0; i < Length(); i++)
items[i] += v[i];
}

## Add() [2/2]

void IntArray::Add | ( | int | term | ) |

Definition at line 249 of file IntArray.cpp.

{
for (int i = 0; i < count; i++)
items[i] += term;
}

## Append() [1/2]

int IntArray::Append | ( | const IntArray & | rhs | ) |

Definition at line 77 of file IntArray.cpp.

{
Grow(count + rhs.count);
for (int i = 0; i < rhs.count; i++)
items[count + i] = rhs.items[i];
count += rhs.count;
return count;
}

## Append() [2/2]

int IntArray::Append | ( | int | value | ) |

Definition at line 70 of file IntArray.cpp.

{
Grow(count + 1);
items[count++] = value;
return count;
}

## BinarySearch()

int IntArray::BinarySearch | ( | int | value | ) | const

Definition at line 178 of file IntArray.cpp.

{
int start = 0;
int stop = count - 1;
while (start <= stop)
{
int mid = (start + stop) / 2;
if (items[mid] == value)
return mid;
if (items[mid] > value)
stop = mid - 1;
else
start = mid + 1;
}
return -1;
}

## Clear()

void IntArray::Clear | ( |  | ) |

inline

Definition at line 118 of file IntArray.h.

{
count = 0;
}

## Count()

int IntArray::Count | ( |  | ) | const

inline

Definition at line 166 of file IntArray.h.

{
return count;
}

## CountIfGreater()

int IntArray::CountIfGreater | ( | int | treshold | ) | const

Definition at line 227 of file IntArray.cpp.

{
int result = 0;
for (int i = 0; i < count; i++)
if (items[i] > threshold)
result++;
return result;
}

## CountIfGreaterOrEqual()

int IntArray::CountIfGreaterOrEqual | ( | int | treshold | ) | const

Definition at line 238 of file IntArray.cpp.

{
int result = 0;
for (int i = 0; i < count; i++)
if (items[i] >= treshold)
result++;
return result;
}

## Delete()

int IntArray::Delete | ( | int | index | ) |

Definition at line 98 of file IntArray.cpp.

{
count--;
if (count - index)
memmove(items + index, items + index + 1, sizeof(int) *(count - index));
return count;
}

## Dimension()

void IntArray::Dimension | ( | int | new_count | ) |

inline

Definition at line 113 of file IntArray.h.

{
Grow(new_count);
count = new_count;
}

## Divide()

void IntArray::Divide | ( | int | denominator | ) |

Definition at line 261 of file IntArray.cpp.

{
for (int i = 0; i < count; i++)
items[i] /= denominator;
}

## DoubleProduct()

double IntArray::DoubleProduct | ( |  | ) |

Definition at line 387 of file IntArray.cpp.

{
double product = 1.0;
for (int i = 0; i < count; i++)
product *= items[i];
return product;
}

## dSum() [1/3]

double IntArray::dSum | ( |  | ) | const

inline

Definition at line 133 of file IntArray.h.

{
return dSum(0, count - 1);
}

## dSum() [2/3]

double IntArray::dSum | ( | int | start | ) | const

inline

Definition at line 137 of file IntArray.h.

{
return dSum(start, count - 1);
}

## dSum() [3/3]

double IntArray::dSum | ( | int | start,
|  | int | end ) const

Definition at line 134 of file IntArray.cpp.

{
double result = 0;
for (int i = start; i <= end; i++)
result += items[i];
return result;
}

## dSumProduct()

double IntArray::dSumProduct | ( | const IntArray & | weight | ) | const

Definition at line 414 of file IntArray.cpp.

{
if (count != weight.count)
error("IntArray::dSumProduct called with different sized arrays\n");
double sum = 0.0;
for (int i = 0; i < count; i++)
sum += items[i] * weight[i];
return sum;
}

## FastFind()

int IntArray::FastFind | ( | int | value | ) | const

inline

Definition at line 97 of file IntArray.h.

{
return BinarySearch(value);
}

## Find()

int IntArray::Find | ( | int | value | ) | const

Definition at line 170 of file IntArray.cpp.

{
for (int i = 0; i < count; i++)
if (value == items[i])
return i;
return -1;
}

## Hash()

int IntArray::Hash | ( | int | initval = 0 | ) |

Definition at line 397 of file IntArray.cpp.

{
return hash((unsigned char *) items, sizeof(int) * count, initval);
}

## InnerProduct()

int IntArray::InnerProduct | ( | IntArray & | v | ) |

Definition at line 324 of file IntArray.cpp.

{
if (Length() != v.Length())
error("IntArray::InnerProduct - vectors have different dimensions\n"
"IntArrays - Left[%d] * Right[%d] ",
Length(), v.Length());
int sum = 0;
for (int i = 0; i < Length(); i++)
sum += items[i] * v[i];
return sum;
}

## InsertAt()

void IntArray::InsertAt | ( | int | index,
|  | int | value )

Definition at line 106 of file IntArray.cpp.

{
Grow(count + 1);
if (count - index)
memmove(items + index + 1, items + index, sizeof(int) *(count - index));
items[index] = value;
count++;
}

## isAscending()

bool IntArray::isAscending | ( |  | ) |

Definition at line 297 of file IntArray.cpp.

{
for (int i = 1; i < count; i++)
if (items[i] < items[i - 1])
return false;
return true;
}

## isDescending()

bool IntArray::isDescending | ( |  | ) |

Definition at line 305 of file IntArray.cpp.

{
for (int i = 1; i < count; i++)
if (items[i] > items[i - 1])
return false;
return true;
}

## Last()

int & IntArray::Last | ( |  | ) | const

inline

Definition at line 86 of file IntArray.h.

{
return items[count - 1];
}

## Length()

int IntArray::Length | ( |  | ) | const

inline

Definition at line 109 of file IntArray.h.

{
return count;
}

## Max() [1/3]

int IntArray::Max | ( |  | ) | const

inline

Definition at line 146 of file IntArray.h.

{
return Max(0, count - 1);
}

## Max() [2/3]

int IntArray::Max | ( | int | start | ) | const

inline

Definition at line 150 of file IntArray.h.

{
return Max(start, count - 1);
}

## Max() [3/3]

int IntArray::Max | ( | int | start,
|  | int | end ) const

Definition at line 144 of file IntArray.cpp.

{
if (start >= count) return 0;
int result = items[start];
for (int i = start + 1; i <= end; i++)
if (result < items[i])
result = items[i];
return result;
}

## Min() [1/3]

int IntArray::Min | ( |  | ) | const

inline

Definition at line 156 of file IntArray.h.

{
return Min(0, count - 1);
}

## Min() [2/3]

int IntArray::Min | ( | int | start | ) | const

inline

Definition at line 160 of file IntArray.h.

{
return Min(start, count - 1);
}

## Min() [3/3]

int IntArray::Min | ( | int | start,
|  | int | end ) const

Definition at line 157 of file IntArray.cpp.

{
if (start >= count) return 0;
int result = items[start];
for (int i = start + 1; i <= end; i++)
if (result > items[i])
result = items[i];
return result;
}

## Multiply()

void IntArray::Multiply | ( | int | factor | ) |

Definition at line 255 of file IntArray.cpp.

{
for (int i = 0; i < count; i++)
items[i] *= factor;
}

## operator int *()

IntArray::operator int * | ( |  | ) |

inline

Definition at line 182 of file IntArray.h.

{
return items;
}

## operator!=()

bool IntArray::operator!= | ( | const IntArray & | rhs | ) | const

Definition at line 289 of file IntArray.cpp.

{
return !(*this == rhs);
}

## operator*=()

IntArray & IntArray::operator*= | ( | int | rhs | ) |

inline

Definition at line 209 of file IntArray.h.

{
Multiply(rhs);
return *this;
}

## operator+=() [1/2]

IntArray & IntArray::operator+= | ( | const IntArray & | rhs | ) |

inline

Definition at line 203 of file IntArray.h.

{
Add(rhs);
return *this;
}

## operator+=() [2/2]

IntArray & IntArray::operator+= | ( | int | rhs | ) |

inline

Definition at line 197 of file IntArray.h.

{
Add(rhs);
return *this;
}

## operator-=()

IntArray & IntArray::operator-= | ( | int | rhs | ) |

inline

Definition at line 215 of file IntArray.h.

{
Add(-rhs);
return *this;
}

## operator/=()

IntArray & IntArray::operator/= | ( | int | rhs | ) |

inline

Definition at line 221 of file IntArray.h.

{
Divide(rhs);
return *this;
}

## operator=()

IntArray & IntArray::operator= | ( | const IntArray & | rhs | ) |

Definition at line 115 of file IntArray.cpp.

{
Grow(rhs.count);
count = rhs.count;
for (int i = 0; i < count; i++)
items[i] = rhs.items[i];
return *this;
}

## operator==()

bool IntArray::operator== | ( | const IntArray & | rhs | ) | const

Definition at line 277 of file IntArray.cpp.

{
if (count != rhs.count)
return false;
for (int i = 0; i < rhs.count; i++)
if (items[i] != rhs.items[i])
return false;
return true;
}

## operator[]() [1/6]

int & IntArray::operator[] | ( | char | index | ) |

inline

Definition at line 52 of file IntArray.h.

{
return items[int(index)];
}

## operator[]() [2/6]

int IntArray::operator[] | ( | char | index | ) | const

inline

Definition at line 56 of file IntArray.h.

{
return items[int(index)];
}

## operator[]() [3/6]

int & IntArray::operator[] | ( | double | fraction | ) |

inline

Definition at line 62 of file IntArray.h.

{
return items[(int)(count * fraction)];
}

## operator[]() [4/6]

int IntArray::operator[] | ( | double | fraction | ) | const

inline

Definition at line 66 of file IntArray.h.

{
return items[(int)(count * fraction)];
}

## operator[]() [5/6]

int & IntArray::operator[] | ( | int | index | ) |

inline

Definition at line 41 of file IntArray.h.

{
return items[index];
}

## operator[]() [6/6]

int IntArray::operator[] | ( | int | index | ) | const

inline

Definition at line 45 of file IntArray.h.

{
return items[index];
}

## Peek()

int IntArray::Peek | ( |  | ) | const

inline

Definition at line 82 of file IntArray.h.

{
return items[count - 1];
}

## Pop()

int IntArray::Pop | ( |  | ) |

inline

Definition at line 78 of file IntArray.h.

{
return items[--count];
}

## Print() [1/4]

void IntArray::Print | ( |  | ) |

inline

Definition at line 239 of file IntArray.h.

{
Print(stdout);
}

## Print() [2/4]

void IntArray::Print | ( | const char * | label | ) |

inline

Definition at line 243 of file IntArray.h.

{
Print(stdout, label);
}

## Print() [3/4]

void IntArray::Print | ( | FILE * | output | ) |

Definition at line 353 of file IntArray.cpp.

{
Print(output, "Array of Integers");
}

## Print() [4/4]

void IntArray::Print | ( | FILE * | output,
|  | const char * | label )

Definition at line 358 of file IntArray.cpp.

{
fprintf(output, "%s [%d elements]: ", label, count);
for (int i = 0; i < count; i++)
fprintf(output, "%d ", items[i]);
fprintf(output, "\n");
}

## Product()

int IntArray::Product | ( |  | ) |

Definition at line 377 of file IntArray.cpp.

{
int product = 1;
for (int i = 0; i < count; i++)
product *= items[i];
return product;
}

## Push()

void IntArray::Push | ( | int | value | ) |

inline

Definition at line 74 of file IntArray.h.

{
Append(value);
}

## PushIfNew()

void IntArray::PushIfNew | ( | int | value | ) |

Definition at line 368 of file IntArray.cpp.

{
for (int i = 0; i < count; i++)
if (items[i] == value)
return;
Push(value);
}

## Reverse()

void IntArray::Reverse | ( |  | ) |

Definition at line 221 of file IntArray.cpp.

{
for (int i = 0, j = count - 1; i < j; i++, j--)
Swap(i, j);
}

## Set()

void IntArray::Set | ( | int | value | ) |

Definition at line 86 of file IntArray.cpp.

{
for (int i = 0; i < count; i++)
items[i] = value;
}

## SetSequence()

void IntArray::SetSequence | ( | int | start = 0,
|  | int | increment = 1 )

Definition at line 92 of file IntArray.cpp.

{
for (int i = 0; i < count; i++, start += increment)
items[i] = start;
}

## Sort() [1/2]

void IntArray::Sort | ( |  | ) |

Definition at line 210 of file IntArray.cpp.

{
QuickSort(items, count, sizeof(int), COMPAREFUNC Compare);
}

## Sort() [2/2]

void IntArray::Sort | ( | IntArray & | freeRider | ) |

Definition at line 215 of file IntArray.cpp.

{
QuickSort2(items, freeRider.items, count, sizeof(int), COMPAREFUNC Compare);
}

## Stack()

void IntArray::Stack | ( | const IntArray & | rhs | ) |

Definition at line 267 of file IntArray.cpp.

{
int end = count;
Dimension(count + a.count);
for (int i = 0; i < a.count; i++)
items[i + end] = a[i];
}

## Subtract()

void IntArray::Subtract | ( | int | term | ) |

inline

Definition at line 188 of file IntArray.h.

{
Add(-term);
}

## Sum() [1/3]

int IntArray::Sum | ( |  | ) | const

inline

Definition at line 123 of file IntArray.h.

{
return Sum(0, count - 1);
}

## Sum() [2/3]

int IntArray::Sum | ( | int | start | ) | const

inline

Definition at line 127 of file IntArray.h.

{
return Sum(start, count - 1);
}

## Sum() [3/3]

int IntArray::Sum | ( | int | start,
|  | int | end ) const

Definition at line 124 of file IntArray.cpp.

{
int result = 0;
for (int i = start; i <= end; i++)
result += items[i];
return result;
}

## SumProduct()

int IntArray::SumProduct | ( | const IntArray & | weight | ) | const

Definition at line 402 of file IntArray.cpp.

{
if (count != weight.count)
error("IntArray::SumProduct called with different sized arrays\n");
int sum = 0;
for (int i = 0; i < count; i++)
sum += items[i] * weight[i];
return sum;
}

## Swap() [1/2]

void IntArray::Swap | ( | int | i,
|  | int | j )

inline

Definition at line 173 of file IntArray.h.

{
int tmp = items[i];
items[i] = items[j];
items[j] = tmp;
}

## Swap() [2/2]

void IntArray::Swap | ( | IntArray & | rhs | ) |

Definition at line 338 of file IntArray.cpp.

{
int * temp = rhs.items;
rhs.items = items;
items = temp;
int swap = rhs.count;
rhs.count = count;
count = swap;
swap = rhs.size;
rhs.size = size;
size = swap;
}

## Zero()

void IntArray::Zero | ( |  | ) |

Definition at line 199 of file IntArray.cpp.

{
for (int i = 0; i < count; i++)
items[i] = 0;
}

## Member Data Documentation

## alloc

int IntArray::alloc = 4

static

Definition at line 33 of file IntArray.h.

The documentation for this class was generated from the following files:

- general/IntArray.h
- general/IntArray.cpp