mirror of https://github.com/xzeldon/htop.git
Misc Vector updates
- Move swap() macro to source file and implement as function - Implement Vector_get() and Vector_size() as inline functions to make them type safe and avoid lhs usage - Comment comparison statistics, they are only needed for performance testing
This commit is contained in:
parent
a63cfc8b7c
commit
e9246abff8
|
@ -127,12 +127,12 @@ static void ProcessList_buildTree(ProcessList* this, pid_t pid, int level, int i
|
||||||
Process* process = (Process*) (Vector_get(children, i));
|
Process* process = (Process*) (Vector_get(children, i));
|
||||||
if (!show)
|
if (!show)
|
||||||
process->show = false;
|
process->show = false;
|
||||||
int s = this->processes2->items;
|
int s = Vector_size(this->processes2);
|
||||||
if (direction == 1)
|
if (direction == 1)
|
||||||
Vector_add(this->processes2, process);
|
Vector_add(this->processes2, process);
|
||||||
else
|
else
|
||||||
Vector_insert(this->processes2, 0, process);
|
Vector_insert(this->processes2, 0, process);
|
||||||
assert(this->processes2->items == s+1); (void)s;
|
assert(Vector_size(this->processes2) == s+1); (void)s;
|
||||||
int nextIndent = indent | (1 << level);
|
int nextIndent = indent | (1 << level);
|
||||||
ProcessList_buildTree(this, process->pid, level+1, (i < size - 1) ? nextIndent : indent, direction, show ? process->showChildren : false);
|
ProcessList_buildTree(this, process->pid, level+1, (i < size - 1) ? nextIndent : indent, direction, show ? process->showChildren : false);
|
||||||
if (i == size - 1)
|
if (i == size - 1)
|
||||||
|
|
25
Vector.c
25
Vector.c
|
@ -18,6 +18,7 @@ Vector* Vector_new(const ObjectClass* type, bool owner, int size) {
|
||||||
|
|
||||||
if (size == DEFAULT_SIZE)
|
if (size == DEFAULT_SIZE)
|
||||||
size = 10;
|
size = 10;
|
||||||
|
assert(size > 0);
|
||||||
this = xMalloc(sizeof(Vector));
|
this = xMalloc(sizeof(Vector));
|
||||||
this->growthRate = size;
|
this->growthRate = size;
|
||||||
this->array = (Object**) xCalloc(size, sizeof(Object*));
|
this->array = (Object**) xCalloc(size, sizeof(Object*));
|
||||||
|
@ -87,14 +88,22 @@ void Vector_prune(Vector* this) {
|
||||||
this->items = 0;
|
this->items = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int comparisons = 0;
|
//static int comparisons = 0;
|
||||||
|
|
||||||
|
static void swap(Object** array, int indexA, int indexB) {
|
||||||
|
assert(indexA >= 0);
|
||||||
|
assert(indexB >= 0);
|
||||||
|
Object* tmp = array[indexA];
|
||||||
|
array[indexA] = array[indexB];
|
||||||
|
array[indexB] = tmp;
|
||||||
|
}
|
||||||
|
|
||||||
static int partition(Object** array, int left, int right, int pivotIndex, Object_Compare compare) {
|
static int partition(Object** array, int left, int right, int pivotIndex, Object_Compare compare) {
|
||||||
void* pivotValue = array[pivotIndex];
|
const Object* pivotValue = array[pivotIndex];
|
||||||
swap(array, pivotIndex, right);
|
swap(array, pivotIndex, right);
|
||||||
int storeIndex = left;
|
int storeIndex = left;
|
||||||
for (int i = left; i < right; i++) {
|
for (int i = left; i < right; i++) {
|
||||||
comparisons++;
|
//comparisons++;
|
||||||
if (compare(array[i], pivotValue) <= 0) {
|
if (compare(array[i], pivotValue) <= 0) {
|
||||||
swap(array, i, storeIndex);
|
swap(array, i, storeIndex);
|
||||||
storeIndex++;
|
storeIndex++;
|
||||||
|
@ -138,10 +147,10 @@ static void combSort(Object** array, int left, int right, Object_Compare compare
|
||||||
|
|
||||||
static void insertionSort(Object** array, int left, int right, Object_Compare compare) {
|
static void insertionSort(Object** array, int left, int right, Object_Compare compare) {
|
||||||
for (int i = left+1; i <= right; i++) {
|
for (int i = left+1; i <= right; i++) {
|
||||||
void* t = array[i];
|
Object* t = array[i];
|
||||||
int j = i - 1;
|
int j = i - 1;
|
||||||
while (j >= left) {
|
while (j >= left) {
|
||||||
comparisons++;
|
//comparisons++;
|
||||||
if (compare(array[j], t) <= 0)
|
if (compare(array[j], t) <= 0)
|
||||||
break;
|
break;
|
||||||
array[j+1] = array[j];
|
array[j+1] = array[j];
|
||||||
|
@ -202,7 +211,7 @@ Object* Vector_take(Vector* this, int idx) {
|
||||||
assert(idx >= 0 && idx < this->items);
|
assert(idx >= 0 && idx < this->items);
|
||||||
assert(Vector_isConsistent(this));
|
assert(Vector_isConsistent(this));
|
||||||
Object* removed = this->array[idx];
|
Object* removed = this->array[idx];
|
||||||
//assert (removed != NULL);
|
assert(removed);
|
||||||
this->items--;
|
this->items--;
|
||||||
if(idx < this->items) {
|
if(idx < this->items) {
|
||||||
memmove(&this->array[idx], &this->array[idx + 1], (this->items - idx) * sizeof(this->array[0]));
|
memmove(&this->array[idx], &this->array[idx + 1], (this->items - idx) * sizeof(this->array[0]));
|
||||||
|
@ -244,7 +253,7 @@ void Vector_moveDown(Vector* this, int idx) {
|
||||||
void Vector_set(Vector* this, int idx, void* data_) {
|
void Vector_set(Vector* this, int idx, void* data_) {
|
||||||
Object* data = data_;
|
Object* data = data_;
|
||||||
assert(idx >= 0);
|
assert(idx >= 0);
|
||||||
assert(Object_isA((Object*)data, this->type));
|
assert(Object_isA(data, this->type));
|
||||||
assert(Vector_isConsistent(this));
|
assert(Vector_isConsistent(this));
|
||||||
|
|
||||||
Vector_checkArraySize(this);
|
Vector_checkArraySize(this);
|
||||||
|
@ -280,7 +289,7 @@ static void Vector_merge(Vector* this, Vector* v2) {
|
||||||
|
|
||||||
void Vector_add(Vector* this, void* data_) {
|
void Vector_add(Vector* this, void* data_) {
|
||||||
Object* data = data_;
|
Object* data = data_;
|
||||||
assert(Object_isA((Object*)data, this->type));
|
assert(Object_isA(data, this->type));
|
||||||
assert(Vector_isConsistent(this));
|
assert(Vector_isConsistent(this));
|
||||||
int i = this->items;
|
int i = this->items;
|
||||||
Vector_set(this, this->items, data);
|
Vector_set(this, this->items, data);
|
||||||
|
|
12
Vector.h
12
Vector.h
|
@ -9,10 +9,9 @@ in the source distribution for its full text.
|
||||||
|
|
||||||
#include "Object.h"
|
#include "Object.h"
|
||||||
|
|
||||||
#define swap(a_,x_,y_) do{ void* tmp_ = a_[x_]; a_[x_] = a_[y_]; a_[y_] = tmp_; }while(0)
|
|
||||||
|
|
||||||
#ifndef DEFAULT_SIZE
|
#ifndef DEFAULT_SIZE
|
||||||
#define DEFAULT_SIZE -1
|
#define DEFAULT_SIZE (-1)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
typedef struct Vector_ {
|
typedef struct Vector_ {
|
||||||
|
@ -54,8 +53,13 @@ int Vector_count(const Vector* this);
|
||||||
|
|
||||||
#else /* NDEBUG */
|
#else /* NDEBUG */
|
||||||
|
|
||||||
#define Vector_get(v_, idx_) ((v_)->array[idx_])
|
static inline Object* Vector_get(Vector* this, int idx) {
|
||||||
#define Vector_size(v_) ((v_)->items)
|
return this->array[idx];
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline int Vector_size(const Vector* this) {
|
||||||
|
return this->items;
|
||||||
|
}
|
||||||
|
|
||||||
#endif /* NDEBUG */
|
#endif /* NDEBUG */
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue