function takeInput()
{
    fscanf(STDIN, "%d", $cases);
    $stringArray = array();
    for ($i = 0; $i < $cases; $i++) {
        $stringArray[] = readString();
    }
    return $stringArray;
}
Example #2
0
function decodeLength($fd, $length, &$json, $level = 0)
{
    $pad = str_pad("", $level * 4, " ");
    $rest = 0;
    while ($rest + 5 < $length) {
        $type = readByte($fd);
        $nlen = readByte($fd);
        $dlen = readInt($fd, 4);
        $name = readString($fd, $nlen);
        $rest += 6 + $nlen + $dlen;
        if ($type == 1 || $type == 5) {
            $subj = array();
            decodeLength($fd, $dlen, $subj, $level + 1);
            if ($name == "") {
                $json[] = $subj;
            } else {
                $json[$name] = $subj;
            }
            //echo "$pad$type $nlen $dlen $name\n";
            continue;
        }
        if ($type == 2) {
            $number = readNumber($fd, $dlen);
            if ($name == "") {
                $json[] = $number;
            } else {
                $json[$name] = $number;
            }
            //echo "$pad$type $nlen $dlen $name => " . gmp_strval($number) . "\n";
            continue;
        }
        if ($type == 3) {
            $data = readString($fd, $dlen);
            if ($name == "") {
                $json[] = $data;
            } else {
                $json[$name] = $data;
            }
            //echo "$pad$type $nlen $dlen $name => $data\n";
            continue;
        }
        $data = readString($fd, $dlen);
        $dump = "";
        for ($inx = 0; $inx < strlen($data); $inx++) {
            $dump .= ord($data[$inx]) . " ";
        }
        echo "=========================={$type} {$nlen} {$dlen} {$name} {$dump}\n";
        exit(0);
    }
}
Example #3
0
function search($file, $word, &$statsList)
{
    $index = computeIndex($word);
    if ($index != -1) {
        fseek($file, $index * 4 + 4);
        // 4 bytes per entry, skip header
        $index = readInt($file);
        if ($index) {
            $start = sizeof($statsList);
            $count = $start;
            fseek($file, $index);
            $w = readString($file);
            while ($w) {
                $statIdx = readInt($file);
                if ($word == substr($w, 0, strlen($word))) {
                    // found word that matches (as substring)
                    $statsList[$count++] = array("word" => $word, "match" => $w, "index" => $statIdx, "full" => strlen($w) == strlen($word), "docs" => array());
                }
                $w = readString($file);
            }
            $totalHi = 0;
            $totalFreqHi = 0;
            $totalFreqLo = 0;
            for ($count = $start; $count < sizeof($statsList); $count++) {
                $statInfo =& $statsList[$count];
                $multiplier = 1;
                // whole word matches have a double weight
                if ($statInfo["full"]) {
                    $multiplier = 2;
                }
                fseek($file, $statInfo["index"]);
                $numDocs = readInt($file);
                $docInfo = array();
                // read docs info + occurrence frequency of the word
                for ($i = 0; $i < $numDocs; $i++) {
                    $idx = readInt($file);
                    $freq = readInt($file);
                    $docInfo[$i] = array("idx" => $idx, "freq" => $freq >> 1, "rank" => 0.0, "hi" => $freq & 1);
                    if ($freq & 1) {
                        $totalHi++;
                        $totalFreqHi += $freq * $multiplier;
                    } else {
                        $totalFreqLo += $freq * $multiplier;
                    }
                }
                // read name and url info for the doc
                for ($i = 0; $i < $numDocs; $i++) {
                    fseek($file, $docInfo[$i]["idx"]);
                    $docInfo[$i]["name"] = readString($file);
                    $docInfo[$i]["url"] = readString($file);
                }
                $statInfo["docs"] = $docInfo;
            }
            $totalFreq = ($totalHi + 1) * $totalFreqLo + $totalFreqHi;
            for ($count = $start; $count < sizeof($statsList); $count++) {
                $statInfo =& $statsList[$count];
                $multiplier = 1;
                // whole word matches have a double weight
                if ($statInfo["full"]) {
                    $multiplier = 2;
                }
                for ($i = 0; $i < sizeof($statInfo["docs"]); $i++) {
                    $docInfo =& $statInfo["docs"];
                    // compute frequency rank of the word in each doc
                    $freq = $docInfo[$i]["freq"];
                    if ($docInfo[$i]["hi"]) {
                        $statInfo["docs"][$i]["rank"] = (double) ($freq * $multiplier + $totalFreqLo) / $totalFreq;
                    } else {
                        $statInfo["docs"][$i]["rank"] = (double) ($freq * $multiplier) / $totalFreq;
                    }
                }
            }
        }
    }
    return $statsList;
}
Example #4
0
function search($file, $word, &$statsList)
{
    $index = computeIndex($word);
    if ($index != -1) {
        fseek($file, $index * 4 + 4);
        // 4 bytes per entry, skip header
        $index = readInt($file);
        if ($index) {
            $start = sizeof($statsList);
            $count = $start;
            fseek($file, $index);
            $w = readString($file);
            while ($w) {
                $statIdx = readInt($file);
                if ($word == substr($w, 0, strlen($word))) {
                    // found word that matches (as substring)
                    $statsList[$count++] = array("word" => $word, "match" => $w, "index" => $statIdx, "full" => strlen($w) == strlen($word), "docs" => array());
                }
                $w = readString($file);
            }
            $totalFreq = 0;
            for ($count = $start; $count < sizeof($statsList); $count++) {
                $statInfo =& $statsList[$count];
                fseek($file, $statInfo["index"]);
                $numDocs = readInt($file);
                $docInfo = array();
                // read docs info + occurrence frequency of the word
                $totalFreq = 0;
                for ($i = 0; $i < $numDocs; $i++) {
                    $idx = readInt($file);
                    $freq = readInt($file);
                    $docInfo[$i] = array("idx" => $idx, "freq" => $freq, "rank" => 0.0);
                    $totalFreq += $freq;
                    if ($statInfo["full"]) {
                        $totalFreq += $freq;
                    }
                }
                // read name an url info for the doc
                for ($i = 0; $i < $numDocs; $i++) {
                    fseek($file, $docInfo[$i]["idx"]);
                    $docInfo[$i]["name"] = readString($file);
                    $docInfo[$i]["url"] = readString($file);
                }
                $statInfo["docs"] = $docInfo;
            }
            for ($count = $start; $count < sizeof($statsList); $count++) {
                $statInfo =& $statsList[$count];
                for ($i = 0; $i < sizeof($statInfo["docs"]); $i++) {
                    $docInfo =& $statInfo["docs"];
                    // compute frequency rank of the word in each doc
                    $statInfo["docs"][$i]["rank"] = (double) $docInfo[$i]["freq"] / $totalFreq;
                }
            }
        }
    }
    return $statsList;
}
function trackToFlash($id)
{
    if (file_exists("tracks/" . $id . ".track")) {
        $file = fopen("tracks/" . $id . ".track", "r");
        $startLine = false;
        $final = "";
        $lineid = 0;
        while (ftell($file) < filesize("tracks/" . $id . ".track")) {
            $skipIt = false;
            $length = readShort($file);
            if ($length == 0) {
                if (readByte($file) == 9) {
                    //End Array/Object
                    $skipIt = true;
                    if ($startLine == true) {
                        $startLine = false;
                    }
                } else {
                    skip($file, -1);
                    //Invalid item of length 0
                    $skipIt = true;
                }
            }
            if ($skipIt == false) {
                $name = readString($file, $length);
                $type = readByte($file);
                if ($type == 0) {
                    //Double
                    if ($startLine == true) {
                        $append = "s";
                    } else {
                        $append = "l";
                    }
                    if ($name != 'level') {
                        $val = readDouble($file);
                        if ((string) $val == 'NAN') {
                            $val = 'undefined';
                        }
                        $final .= $append . $lineid . "_" . $name . "=" . $val . "&";
                    } else {
                        skip($file, 8);
                    }
                }
                if ($type == 1) {
                    //Boolean (Show)
                    skip($file, 1);
                }
                if ($type == 2) {
                    //String
                    $strlength = readShort($file);
                    skip($file, $strlength);
                }
                //Type 3 & 6 Do Nothing
                if ($type == 8) {
                    //Array
                    if ($name == "startLine") {
                        $startLine = true;
                    } else {
                        $lineid = (int) $name;
                    }
                    skip($file, 4);
                }
            }
        }
        fclose($file);
        $final = substr($final, 0, strlen($final) - 1);
        return $final;
    }
}
Example #6
0
                 $accelLines[$track]++;
             }
             if ($value == 2) {
                 $backLines[$track]++;
             }
         }
     }
 }
 if ($type == 1) {
     //Boolean (Show)
     skip($file, 1);
 }
 if ($type == 2) {
     //String
     $strlength = readShort($file);
     $value = readString($file, $strlength);
     $trackLengthOff[$currentTrack] = ftell($file) - $trackStartOff[$currentTrack] + 3;
     if ($name == 'label') {
         $trackName[$track] = $value;
         break;
     }
 }
 if ($type == 3) {
     //Object
     $currentTrack = (int) $name;
     $trackStartOff[$currentTrack] = ftell($file);
 }
 // Type 6: Undefined: Do Nothing
 if ($type == 8) {
     //Array
     if ($name == "startLine") {