Example #1
0
static int
compareRatio( double a, double b )
{
    if( (int)a == TR_RATIO_INF && (int)b == TR_RATIO_INF ) return 0;
    if( (int)a == TR_RATIO_INF ) return 1;
    if( (int)b == TR_RATIO_INF ) return -1;
    return compareDouble( a, b );
}
Example #2
0
static int
compareByProgress( GtkTreeModel * m, GtkTreeIter * a, GtkTreeIter * b, gpointer user_data )
{
    int ret = 0;
    tr_torrent * t;
    const tr_stat *sa, *sb;

    gtk_tree_model_get( m, a, MC_TORRENT_RAW, &t, -1 );
    sa = tr_torrentStatCached( t );
    gtk_tree_model_get( m, b, MC_TORRENT_RAW, &t, -1 );
    sb = tr_torrentStatCached( t );

    if( !ret ) ret = compareDouble( sa->percentComplete, sb->percentComplete );
    if( !ret ) ret = compareDouble( sa->seedRatioPercentDone, sb->seedRatioPercentDone );
    if( !ret ) ret = compareByRatio( m, a, b, user_data );
    return ret;
}
Example #3
0
void charFreqSort(charFreq *charsFreq, int len) {
	//Not the fastest but good enough for only 26 elements
	int i,j;
	charFreq temp;
	for(i=0; i < len; i++) {
		for(j=0;j < len;j++) {
			if (compareDouble(charsFreq[i].freq, charsFreq[j].freq)) {
				temp.c = charsFreq[i].c;
				temp.freq = charsFreq[i].freq;
				*(charsFreq + i) = *(charsFreq + j);
				*(charsFreq + j) = temp;
			}
		}
	}
}
Example #4
0
static int
compareByActivity( GtkTreeModel * m, GtkTreeIter * a, GtkTreeIter * b, gpointer user_data )
{
    int ret = 0;
    tr_torrent *ta, *tb;
    const tr_stat *sa, *sb;
    double aUp, aDown, bUp, bDown;

    gtk_tree_model_get( m, a, MC_SPEED_UP, &aUp,
                              MC_SPEED_DOWN, &aDown,
                              MC_TORRENT_RAW, &ta,
                              -1 );
    gtk_tree_model_get( m, b, MC_SPEED_UP, &bUp,
                              MC_SPEED_DOWN, &bDown,
                              MC_TORRENT_RAW, &tb,
                              -1 );
    sa = tr_torrentStatCached( ta );
    sb = tr_torrentStatCached( tb );

    if( !ret ) ret = compareDouble( aUp+aDown, bUp+bDown );
    if( !ret ) ret = compareUint64( sa->uploadedEver, sb->uploadedEver );
    if( !ret ) ret = compareByName( m, a, b, user_data );
    return ret;
}
Example #5
0
static unsigned int handleAlpha(ruleset *tree, char *sid, char *mid, char *state, alpha *alphaNode, jsonProperty *allProperties, 
                                void **rulesBinding, unsigned short actionType, unsigned short *commandCount) {
    alpha *alphaStack[MAX_STACK_SIZE];
    jsonProperty *propertyStack[MAX_STACK_SIZE];
    alphaStack[0] = alphaNode;    
    propertyStack[0] = NULL;
    alpha *currentAlpha;
    jsonProperty *currentProperty;
    unsigned short top = 1;
    unsigned int result = ERR_EVENT_NOT_HANDLED;
    unsigned int propertyHash = 1;

    while (top > 0) {       
        --top;
        currentAlpha = alphaStack[top];
        currentProperty = propertyStack[top];
        unsigned char alphaOp = currentAlpha->operator;
        unsigned char propertyMatch = 1;
        if (alphaOp != OP_NEX && alphaOp != OP_EX && alphaOp != OP_TYPE)
        {
            char *propertyLast = currentProperty->lastValue;
            char *propertyFirst = currentProperty->firstValue;
            unsigned char propertyType = currentProperty->type;
            char temp;
            if (!currentProperty->isMaterial) {
                switch(propertyType) {
                    case JSON_INT:
                        ++propertyLast;
                        temp = propertyLast[0];
                        propertyLast[0] = '\0';
                        currentProperty->value.i = atol(propertyFirst);
                        propertyLast[0] = temp;
                        break;
                    case JSON_DOUBLE:
                        ++propertyLast;
                        temp = propertyLast[0];
                        propertyLast[0] = '\0';
                        currentProperty->value.i = atof(propertyFirst);
                        propertyLast[0] = temp;
                        break;
                    case JSON_BOOL:
                        ++propertyLast;
                        unsigned int leftLength = propertyLast - propertyFirst;
                        unsigned char b = 1;
                        if (leftLength == 5 && strncmp("false", propertyFirst, 5)) {
                            b = 0;
                        }
                        currentProperty->value.b = b;
                        break;
                }

                currentProperty->isMaterial = 1;
            }
            
            unsigned short type = propertyType << 8;
            type = type + currentAlpha->right.type;
            int leftLength;
            switch(type) {
                case COMP_BOOL_BOOL:
                    propertyMatch = compareBool(currentProperty->value.b, currentAlpha->right.value.b, alphaOp);
                    break;
                case COMP_BOOL_INT: 
                    propertyMatch = compareInt(currentProperty->value.b, currentAlpha->right.value.i, alphaOp);
                    break;
                case COMP_BOOL_DOUBLE: 
                    propertyMatch = compareDouble(currentProperty->value.b, currentAlpha->right.value.d, alphaOp);
                    break;
                case COMP_BOOL_STRING:
                    propertyMatch = compareString(propertyFirst, propertyLast, 
                                    &tree->stringPool[currentAlpha->right.value.stringOffset], alphaOp);
                    break;
                case COMP_INT_BOOL:
                    propertyMatch = compareInt(currentProperty->value.i, currentAlpha->right.value.b, alphaOp);
                    break;
                case COMP_INT_INT: 
                    propertyMatch = compareInt(currentProperty->value.i, currentAlpha->right.value.i, alphaOp);
                    break;
                case COMP_INT_DOUBLE: 
                    propertyMatch = compareDouble(currentProperty->value.i, currentAlpha->right.value.d, alphaOp);
                    break;
                case COMP_INT_STRING:
                    propertyMatch = compareString(propertyFirst, propertyLast, 
                                    &tree->stringPool[currentAlpha->right.value.stringOffset], alphaOp);
                    break;
                case COMP_DOUBLE_BOOL:
                    propertyMatch = compareDouble(currentProperty->value.i, currentAlpha->right.value.b, alphaOp);
                    break;
                case COMP_DOUBLE_INT: 
                    propertyMatch = compareDouble(currentProperty->value.i, currentAlpha->right.value.i, alphaOp);
                    break;
                case COMP_DOUBLE_DOUBLE: 
                    propertyMatch = compareDouble(currentProperty->value.i, currentAlpha->right.value.d, alphaOp);
                    break;
                case COMP_DOUBLE_STRING:
                    propertyMatch = compareString(propertyFirst, propertyLast, 
                                    &tree->stringPool[currentAlpha->right.value.stringOffset], alphaOp);
                    break;
                case COMP_STRING_BOOL:
                    if (currentAlpha->right.value.b) {
                        propertyMatch = compareString(propertyFirst, propertyLast, "true", alphaOp);
                    }
                    else {
                        propertyMatch = compareString(propertyFirst, propertyLast, "false", alphaOp);
                    }
                    break;
                case COMP_STRING_INT: 
                    {
                        leftLength = propertyLast - propertyFirst + 1;
                        char rightStringInt[leftLength];
                        snprintf(rightStringInt, leftLength, "%ld", currentAlpha->right.value.i);
                        propertyMatch = compareString(propertyFirst, propertyLast, rightStringInt, alphaOp);
                    }
                    break;
                case COMP_STRING_DOUBLE: 
                    {
                        leftLength = propertyLast - propertyFirst + 1;
                        char rightStringDouble[leftLength];
                        snprintf(rightStringDouble, leftLength, "%f", currentAlpha->right.value.d);
                        propertyMatch = compareString(propertyFirst, propertyLast, rightStringDouble, alphaOp);
                        break;
                    }
                case COMP_STRING_STRING:
                    propertyMatch = compareString(propertyFirst, propertyLast, 
                                    &tree->stringPool[currentAlpha->right.value.stringOffset], alphaOp);
                    break;
            }
        }

        if (propertyMatch) {
            unsigned int nextLength = currentAlpha->nextLength;
            unsigned int nextOffset = currentAlpha->nextOffset;   
            for (unsigned int i = 0; i < nextLength; ++i) {
                node *currentNode = &tree->nodePool[tree->nextPool[nextOffset + i]];
                unsigned int nodeHash;
                if (currentNode->type != NODE_ALPHA) {
                    result = handleBeta(tree, sid, mid, state, currentNode, rulesBinding, actionType, commandCount);
                }
                else {
                    nodeHash = currentNode->value.a.hash;
                    propertyHash = 1;
                    for (int ii = 0; propertyHash && (propertyHash != nodeHash) && (ii < MAX_CONFLICTS); ++ii) {
                        currentProperty = &allProperties[(nodeHash & HASH_MASK) + (ii * MAX_BUCKET_LENGTH)];
                        propertyHash = currentProperty->hash;
                    }
                    if (((nodeHash == propertyHash) && (currentNode->value.a.operator != OP_NEX)) || 
                        ((nodeHash != propertyHash) && (currentNode->value.a.operator == OP_NEX))) {
                        if (top == MAX_STACK_SIZE) {
                            return ERR_MAX_STACK_SIZE;
                        }

                        alphaStack[top] =  &currentNode->value.a;
                        propertyStack[top] = currentProperty;
                        ++top;
                    }
                }
            }
        }
    }

    return result;
}
Example #6
0
 bool isPerpendicular(const Line &line) const {
     return compareDouble(parameters.A * line.parameters.A, -parameters.B * line.parameters.B);
 }
Example #7
0
 bool isParallel(const Line &line) const {
     return compareDouble(parameters.A, line.parameters.A) &&
             compareDouble(parameters.B, line.parameters.B);
 }