Ejemplo n.º 1
0
// LoadPSF for Memory
static PSFINFO *memLoadPSF(char *addr, long size, int level, int type) // Type==1 for just info load.
{
    u8 *in,*out=0;
	u8 head[4];

    u32 reserved;
    u32 complen;
    u32 crc32;
    uLongf outlen;
	PSFINFO *psfi=NULL;
	PSFINFO *tmpi=NULL;
	u32 poffset, plength;
	u32 offset = 0;

	if( addr == NULL || size == 0 ) return(0);

	memread((void*)(head), 4, addr, size, &offset);
	if(memcmp(head,"PSF\x01",4)) return(0);

	psfi=malloc(sizeof(PSFINFO));
	memset(psfi,0,sizeof(PSFINFO));
    psfi->stop=~0;
    psfi->fade=0;

	memread((void*)(&reserved), 4, addr, size, &offset);
	memread((void*)(&complen), 4, addr, size, &offset);
	complen=BFLIP32(complen);

	memread((void*)(&crc32), 4, addr, size, &offset);
	crc32=BFLIP32(crc32);

	memseek(reserved, size, &offset);

	if(type) {
		memseek(complen, size, &offset);
	}
    else {
        in=malloc(complen);
        out=malloc(1024*1024*2+0x800);
		if( !in || !out ) { free(psfi); return (0); }
		memread((void*)(in), complen, addr, size, &offset);

        outlen=1024*1024*2;
        uncompress(out,&outlen,in,complen);
        free(in);

        psxRegs->pc = out[0x10] | out[0x11]<<8 | out[0x12]<<16 | out[0x13]<<24;
		psxRegs->GPR.n.gp = out[0x14] | out[0x15]<<8 | out[0x16]<<16 | out[0x17]<<24;
		psxRegs->GPR.n.sp = out[0x30] | out[0x31]<<8 | out[0x32]<<16 | out[0x33]<<24;

        if (psxRegs->GPR.n.sp == 0) psxRegs->GPR.n.sp = 0x801fff00;

#ifdef DEBUG
		fprintf(stderr, "%d Level: PC %x GP %x SP %x\n",
			level, psxRegs->pc, psxRegs->GPR.n.gp, psxRegs->GPR.n.sp);
#endif

 		if(level) {
			poffset = out[0x18] | out[0x19]<<8 | out[0x1a]<<16 | out[0x1b]<<24;
			poffset &= 0x3fffffff;	// kill any MIPS cache segment indicators
			plength = out[0x1c] | out[0x1d]<<8 | out[0x1e]<<16 | out[0x1f]<<24;

#ifdef DEBUG
			fprintf(stderr, "%d Level: offset %x plength: %d\n", level, poffset, plength);
#endif

			LoadPSXMem(poffset,plength,out+0x800);
			free(out);

			// I don't really know how does this
			// work, but apparently it breaks the
			// loop that makes ff6 and ct to take
			// several seconds to start ...
			if( crc32 == 0xEFDE8EEE || crc32 == 0x545D9F65 )
				psxMemWrite32(0x5A66C, 0);

			// Same as above for Popolocrois and Einhänder
			if( crc32 == 0xF535726 )
				psxMemWrite32(0x5A990, 0);
		}
	}

    {
		u8 tagdata[5];
		if( memread((void*)(tagdata), 5, addr, size, &offset) == 5 ) {
			if(!memcmp(tagdata,"[TAG]",5)) {
				char linebuf[1024];

				while( memfgets(linebuf, 1024, addr, size, &offset)>0 ) {
					int x;
					char *key=0,*value=0;

					if(!GetKeyVal(linebuf,&key,&value)) {
						if(key) free(key);
						if(value) free(value);
						continue;
					}

					AddKV(&psfi->tags,key,value);

					if(!level) {
       					static char *yoinks[8]={"title","artist","game","year","genre",
                                  "copyright","psfby","comment"};
						char **yoinks2[8]={&psfi->title,&psfi->artist,&psfi->game,&psfi->year,&psfi->genre,
                                    &psfi->copyright,&psfi->psfby,&psfi->comment};
						for(x=0;x<8;x++)
							if(!strcasecmp(key,yoinks[x]))
								*yoinks2[x]=value;
						if(!strcasecmp(key,"length"))
							psfi->stop=TimeToMS(value);
						else if(!strcasecmp(key,"fade"))
							psfi->fade=TimeToMS(value);
					}

					if(!strcasecmp(key,"_lib") && !type && __psflibs) {

						/* Search file name "value" from the __psflibs array */
						int n, found = 0;
						for(n=0;n<__psflibs->count;n++)
							if( !strcasecmp(value, __psflibs->libn[n].name) ) {
								found = 1;
								break;
							}

						if(!found || !(tmpi=memLoadPSF(__psflibs->libn[n].addr, __psflibs->libn[n].size, level+1,0))) {
							free(key);
							free(value);
							if(!level) free(out);

							FreeTags(psfi->tags);
							free(psfi);
							return(0);
						}

						FreeTags(tmpi->tags);
						free(tmpi);
					}
				}
			}
		}
	}

	/* Now, if we're at level 0(main PSF), load the main executable, and any libN stuff*/
    if(!level && !type) {
    	poffset = out[0x18] | out[0x19]<<8 | out[0x1a]<<16 | out[0x1b]<<24;
		poffset &= 0x3fffffff;	// kill any MIPS cache segment indicators
		plength = out[0x1c] | out[0x1d]<<8 | out[0x1e]<<16 | out[0x1f]<<24;

		// TODO - investigate: should i just make
		//        plength = outlen-0x800?

		// Philosoma has an illegal "plength".  *sigh*
		if (plength > (outlen-0x800))
		{
			plength = outlen-0x800;
		}

		if( psfi->game ) {
			// Suikoden Tenmei has an illegal "plength". *sigh sigh*
			if( !strncmp(psfi->game, "Suikoden: Tenmei no Chikai", 26) ) {
				plength = outlen-0x800;
			}

			// Einhänder has an illegal "plength". *sigh sigh sigh*
			if( !strncmp(psfi->game, "Einhänder", 9) ) {
				plength = outlen-0x800;
			}
		}

#ifdef DEBUG
		fprintf(stderr, "%d Level: offset %x plength: %d\n", level, poffset, plength);
#endif

		LoadPSXMem(poffset,plength,out+0x800);
		free(out);
    }

	if(!type && __psflibs)	/* Load libN */ {
		LIBNCACHE *cache;
		PSFTAG *tag;
		unsigned int libncount=0;
		unsigned int cur=0;

		tag=psfi->tags;
		while(tag) {
			if(!strncasecmp(tag->key,"_lib",4) && tag->key[4])
			libncount++;
			tag=tag->next;
		}

		if(libncount) {
			cache=malloc(sizeof(LIBNCACHE)*libncount);

			tag=psfi->tags;
			while(tag) {
				if(!strncasecmp(tag->key,"_lib",4) && tag->key[4]) {
					cache[cur].num=atoi(&tag->key[4]);
					cache[cur].value=tag->value;
					cur++;
				}
				tag=tag->next;
			}
			qsort(cache, libncount, sizeof(LIBNCACHE), ccomp);
			for(cur=0;cur<libncount;cur++) {
				u32 ba[3];

 				if(cache[cur].num < 2) continue;

				ba[0]=psxRegs->pc;
				ba[1]=psxRegs->GPR.n.gp;
				ba[2]=psxRegs->GPR.n.sp;

				/* Search file name "value" from the __psflibs array */
				int n, found = 0;
				for(n=0;n<__psflibs->count;n++)
					if( !strcasecmp(cache[cur].value, __psflibs->libn[n].name) ) {
						found = 1;
						break;
					}

				if(!found || !(tmpi=memLoadPSF(__psflibs->libn[n].addr,__psflibs->libn[n].size,level+1,0))) {
					//free(key);
					//free(value);
					//free(tmpfn);
					//fclose(fp);
					//return(0);
				}
				FreeTags(tmpi->tags);
				free(tmpi);

				psxRegs->pc=ba[0];
       			psxRegs->GPR.n.gp=ba[1];
				psxRegs->GPR.n.sp=ba[2];
			}
			free(cache);
		}	// if(libncount)

	}	// if(!type)

	return(psfi);
}
Ejemplo n.º 2
0
void sunionDiffGenericCommand(redisClient *c, robj **setkeys, int setnum, robj *dstkey, int op) {
    robj **sets = zmalloc(sizeof(robj*)*setnum);
    setTypeIterator *si;
    robj *ele, *dstset = NULL;
    int j, cardinality = 0;
    int diff_algo = 1;

    for (j = 0; j < setnum; j++) {
        robj *setobj = dstkey ?
            lookupKeyWrite(c->db,setkeys[j]) :
            lookupKeyRead(c->db,setkeys[j]);
        if (!setobj) {
            sets[j] = NULL;
            continue;
        }
        if (checkType(c,setobj,REDIS_SET)) {
            zfree(sets);
            return;
        }
        sets[j] = setobj;
    }

    /* Select what DIFF algorithm to use.
     *
     * Algorithm 1 is O(N*M) where N is the size of the element first set
     * and M the total number of sets.
     *
     * Algorithm 2 is O(N) where N is the total number of elements in all
     * the sets.
     *
     * We compute what is the best bet with the current input here. */
    if (op == REDIS_OP_DIFF && sets[0]) {
        long long algo_one_work = 0, algo_two_work = 0;

        for (j = 0; j < setnum; j++) {
            if (sets[j] == NULL) continue;

            algo_one_work += setTypeSize(sets[0]);
            algo_two_work += setTypeSize(sets[j]);
        }

        /* Algorithm 1 has better constant times and performs less operations
         * if there are elements in common. Give it some advantage. */
        algo_one_work /= 2;
        diff_algo = (algo_one_work <= algo_two_work) ? 1 : 2;

        if (diff_algo == 1 && setnum > 1) {
            /* With algorithm 1 it is better to order the sets to subtract
             * by decreasing size, so that we are more likely to find
             * duplicated elements ASAP. */
            qsort(sets+1,setnum-1,sizeof(robj*),
                qsortCompareSetsByRevCardinality);
        }
    }

    /* We need a temp set object to store our union. If the dstkey
     * is not NULL (that is, we are inside an SUNIONSTORE operation) then
     * this set object will be the resulting object to set into the target key*/
    dstset = createIntsetObject();

    if (op == REDIS_OP_UNION) {
        /* Union is trivial, just add every element of every set to the
         * temporary set. */
        for (j = 0; j < setnum; j++) {
            if (!sets[j]) continue; /* non existing keys are like empty sets */

            si = setTypeInitIterator(sets[j]);
            while((ele = setTypeNextObject(si)) != NULL) {
                if (setTypeAdd(dstset,ele)) cardinality++;
                decrRefCount(ele);
            }
            setTypeReleaseIterator(si);
        }
    } else if (op == REDIS_OP_DIFF && sets[0] && diff_algo == 1) {
        /* DIFF Algorithm 1:
         *
         * We perform the diff by iterating all the elements of the first set,
         * and only adding it to the target set if the element does not exist
         * into all the other sets.
         *
         * This way we perform at max N*M operations, where N is the size of
         * the first set, and M the number of sets. */
        si = setTypeInitIterator(sets[0]);
        while((ele = setTypeNextObject(si)) != NULL) {
            for (j = 1; j < setnum; j++) {
                if (!sets[j]) continue; /* no key is an empty set. */
                if (sets[j] == sets[0]) break; /* same set! */
                if (setTypeIsMember(sets[j],ele)) break;
            }
            if (j == setnum) {
                /* There is no other set with this element. Add it. */
                setTypeAdd(dstset,ele);
                cardinality++;
            }
            decrRefCount(ele);
        }
        setTypeReleaseIterator(si);
    } else if (op == REDIS_OP_DIFF && sets[0] && diff_algo == 2) {
        /* DIFF Algorithm 2:
         *
         * Add all the elements of the first set to the auxiliary set.
         * Then remove all the elements of all the next sets from it.
         *
         * This is O(N) where N is the sum of all the elements in every
         * set. */
        for (j = 0; j < setnum; j++) {
            if (!sets[j]) continue; /* non existing keys are like empty sets */

            si = setTypeInitIterator(sets[j]);
            while((ele = setTypeNextObject(si)) != NULL) {
                if (j == 0) {
                    if (setTypeAdd(dstset,ele)) cardinality++;
                } else {
                    if (setTypeRemove(dstset,ele)) cardinality--;
                }
                decrRefCount(ele);
            }
            setTypeReleaseIterator(si);

            /* Exit if result set is empty as any additional removal
             * of elements will have no effect. */
            if (cardinality == 0) break;
        }
    }

    /* Output the content of the resulting set, if not in STORE mode */
    if (!dstkey) {
        addReplyMultiBulkLen(c,cardinality);
        si = setTypeInitIterator(dstset);
        while((ele = setTypeNextObject(si)) != NULL) {
            addReplyBulk(c,ele);
            decrRefCount(ele);
        }
        setTypeReleaseIterator(si);
        decrRefCount(dstset);
    } else {
        /* If we have a target key where to store the resulting set
         * create this key with the result set inside */
        int deleted = dbDelete(c->db,dstkey);
        if (setTypeSize(dstset) > 0) {
            dbAdd(c->db,dstkey,dstset);
            addReplyLongLong(c,setTypeSize(dstset));
            notifyKeyspaceEvent(REDIS_NOTIFY_SET,
                op == REDIS_OP_UNION ? "sunionstore" : "sdiffstore",
                dstkey,c->db->id);
        } else {
            decrRefCount(dstset);
            addReply(c,shared.czero);
            if (deleted)
                notifyKeyspaceEvent(REDIS_NOTIFY_GENERIC,"del",
                    dstkey,c->db->id);
        }
        signalModifiedKey(c->db,dstkey);
        server.dirty++;
    }
    zfree(sets);
}
Ejemplo n.º 3
0
int gt_dlist_unit_test(GtError *err)
{
  GtDlist *dlist;
  GtDlistelem *dlistelem;
  int i, j, size, *data,
      elem_a = 7,
      elem_b = 6,
      elems[MAX_SIZE],
      elems_backup[MAX_SIZE],
      had_err = 0;
  gt_error_check(err);

  /* boundary case: empty dlist */
  dlist = gt_dlist_new(intcompare);
  gt_ensure(had_err, !gt_dlist_size(dlist));
  gt_dlist_delete(dlist);

  dlist = gt_dlist_new(NULL);
  gt_ensure(had_err, !gt_dlist_size(dlist));
  gt_dlist_delete(dlist);

  /* boundary case: dlist containing one element */
  dlist = gt_dlist_new(intcompare);
  gt_dlist_add(dlist, &elem_a);
  gt_ensure(had_err, gt_dlist_size(dlist) == 1);
  gt_ensure(had_err,
         elem_a == *(int*) gt_dlistelem_get_data(gt_dlist_first(dlist)));
  gt_dlist_delete(dlist);

  dlist = gt_dlist_new(NULL);
  gt_dlist_add(dlist, &elem_a);
  gt_ensure(had_err, gt_dlist_size(dlist) == 1);
  gt_ensure(had_err,
         elem_a == *(int*) gt_dlistelem_get_data(gt_dlist_first(dlist)));
  gt_dlist_delete(dlist);

  /* boundary case: dlist containing two elements */
  dlist = gt_dlist_new(intcompare);
  gt_dlist_add(dlist, &elem_a);
  gt_dlist_add(dlist, &elem_b);
  gt_ensure(had_err, gt_dlist_size(dlist) == 2);
  gt_ensure(had_err,
         elem_b == *(int*) gt_dlistelem_get_data(gt_dlist_first(dlist)));
  gt_dlist_delete(dlist);

  dlist = gt_dlist_new(NULL);
  gt_dlist_add(dlist, &elem_a);
  gt_dlist_add(dlist, &elem_b);
  gt_ensure(had_err, gt_dlist_size(dlist) == 2);
  gt_ensure(had_err,
         elem_a == *(int*) gt_dlistelem_get_data(gt_dlist_first(dlist)));
  gt_dlist_delete(dlist);

  for (i = 0; i < NUM_OF_TESTS && !had_err; i++) {
    /* construct the random elements for the list */
    size = gt_rand_max(MAX_SIZE);
    for (j = 0; j < size; j++) {
      elems[j] = gt_rand_max(INT_MAX);
      elems_backup[j] = elems[j];
    }

    /* sort the backup elements */
    qsort(elems_backup, size, sizeof (int), intcompare);

    /* test with compare function */
    dlist = gt_dlist_new(intcompare);
    gt_ensure(had_err, !gt_dlist_size(dlist));
    for (j = 0; j < size && !had_err; j++) {
      gt_dlist_add(dlist, elems + j);
      gt_ensure(had_err, gt_dlist_size(dlist) == j+1);

      for (dlistelem = gt_dlist_first(dlist); dlistelem != NULL;
           dlistelem = gt_dlistelem_next(dlistelem)) {
      }
    }
    j = 0;
    for (dlistelem = gt_dlist_first(dlist); dlistelem != NULL;
         dlistelem = gt_dlistelem_next(dlistelem)) {
      data = gt_dlistelem_get_data(dlistelem);
      gt_ensure(had_err, *data == elems_backup[j]);
      j++;
    }
    /* test gt_dlist_find() */
    for (j = 0; j < size; j++) {
      dlistelem = gt_dlist_find(dlist, elems_backup + j);
      gt_ensure(had_err, dlistelem);
      gt_ensure(had_err,
             *(int*) gt_dlistelem_get_data(dlistelem) == elems_backup[j]);
    }
    /* remove first element */
    if (gt_dlist_size(dlist)) {
      gt_dlist_remove(dlist, gt_dlist_first(dlist));
      if (gt_dlist_size(dlist)) {
        data = gt_dlistelem_get_data(gt_dlist_first(dlist));
        gt_ensure(had_err, *data == elems_backup[1]);
      }
    }
    /* remove last element */
    if (gt_dlist_size(dlist)) {
      gt_dlist_remove(dlist, gt_dlist_last(dlist));
      if (gt_dlist_size(dlist)) {
        data = gt_dlistelem_get_data(gt_dlist_last(dlist));
        gt_ensure(had_err, *data == elems_backup[size - 2]);
      }
    }
    /* XXX: fix this */
#if 0
    /* remove middle element */
    if (gt_dlist_size(dlist) >= 2) {
      dlistelem = gt_dlist_first(dlist);
      for (j = 1; j < gt_dlist_size(dlist) / 2; j++)
        dlistelem = gt_dlistelem_next(dlistelem);
      gt_dlist_remove(dlist, dlistelem);
      dlistelem = gt_dlist_first(dlist);
      for (j = 1; j < gt_dlist_size(dlist) / 2 + 1; j++)
        dlistelem = gt_dlistelem_next(dlistelem);
      data = gt_dlistelem_get_data(gt_dlist_last(dlist));
      gt_ensure(had_err, *data == elems_backup[size / 2 + 1]);
    }
#endif
    gt_dlist_delete(dlist);

    /* test without compare function */
    dlist = gt_dlist_new(NULL);
    gt_ensure(had_err, !gt_dlist_size(dlist));
    for (j = 0; j < size && !had_err; j++) {
      gt_dlist_add(dlist, elems + j);
      gt_ensure(had_err, gt_dlist_size(dlist) == j+1);
    }
    j = 0;
    for (dlistelem = gt_dlist_first(dlist); dlistelem != NULL;
         dlistelem = gt_dlistelem_next(dlistelem)) {
      data = gt_dlistelem_get_data(dlistelem);
      gt_assert(j < MAX_SIZE);
      gt_ensure(had_err, *data == elems[j]);
      j++;
    }
    /* remove first element */
    if (gt_dlist_size(dlist)) {
      gt_dlist_remove(dlist, gt_dlist_first(dlist));
      if (gt_dlist_size(dlist)) {
        data = gt_dlistelem_get_data(gt_dlist_first(dlist));
        gt_ensure(had_err, *data == elems[1]);
      }
    }
    /* remove last element */
    if (gt_dlist_size(dlist)) {
      gt_dlist_remove(dlist, gt_dlist_last(dlist));
      if (gt_dlist_size(dlist)) {
        data = gt_dlistelem_get_data(gt_dlist_last(dlist));
        gt_ensure(had_err, *data == elems[size - 2]);
      }
    }
    gt_dlist_delete(dlist);
  }

  return had_err;
}
Ejemplo n.º 4
0
int main (int argc, char *argv[]) {

	int i,y,x,z,n;

	FILE *fileha;

	struct iindexfileFormat *iindexfileArray;

	struct iindexfileFormat *iindexfile;
	struct DictionaryFormat DictionaryPost;

	int nrOffIindexFiles;
	int gotEof;

	unsigned long mergeTerm;
	unsigned long totaltAntall;

        unsigned long DocID;
        unsigned long TermAntall;
        unsigned short hit;

	unsigned long currentTerm;

	unsigned long term;
	unsigned long Antall;

	unsigned long startAdress;
	unsigned long stoppAdress;

	char FinalIindexFileName[128];
	char FinalDictionaryFileName[128];

	char FinalIindexFilePath[128];
        char FinalDictionaryFilePath[128];

	int DocIDcount = 0;
	int count;

	FILE *FinalIindexFileFA;
	FILE *FinalDictionaryFileFA;

	char PathForIindex[128];
	char PathForLotIndex[128];

	int startIndex;
	int stoppIndex;

        if (argc < 2) {
                printf("Dette programet printer ut en iindex. Gi den en fil inn\n\n");
                exit(0);
        }

	GetFilePathForIindex(PathForIindex,atol(argv[1]));


	sprintf(FinalIindexFilePath,"%s/iindex/%s/index/%s",PathForIindex,"Main","aa");
	sprintf(FinalIindexFileName,"%s/%i.txt",FinalIindexFilePath,atol(argv[1]));
	printf("FinalIindexFileName %s\n",FinalIindexFileName);
	makePath(FinalIindexFilePath);
	if ((FinalIindexFileFA = fopen(FinalIindexFileName,"wb")) == NULL) {
		perror(FinalIindexFileName);
		exit(1);
	}

	sprintf(FinalDictionaryFilePath,"%s/iindex/%s/dictionary/%s",PathForIindex,"Main","aa");
	sprintf(FinalDictionaryFileName,"%s/%i.txt",FinalDictionaryFilePath,atol(argv[1]));
	printf("FinalDictionaryFileName %s\n",FinalDictionaryFileName);
	makePath(FinalDictionaryFilePath);
	if ((FinalDictionaryFileFA = fopen(FinalDictionaryFileName,"wb")) == NULL) {
                perror(FinalDictionaryFileName);
		exit(1);
        }

	nrOffIindexFiles = argc -2;

	iindexfile = malloc(nrOffIindexFiles * sizeof(struct iindexfileFormat));
	iindexfileArray = malloc(nrOffIindexFiles * sizeof(struct iindexfileFormat));

	printf("out file %s\nnrOffIindexFiles %i\n",FinalIindexFileName,nrOffIindexFiles);
	
	startIndex = atoi(argv[2]);
        stoppIndex = atoi(argv[3]) +1;

	count=0;
	for (i=startIndex;i<stoppIndex;i++) {
		//printf("iindex file nr %i\n",i);

		GetFilPathForLot(PathForLotIndex,i);
		///iindex/Main/index/aa/63.txt
		sprintf(PathForLotIndex,"%siindex/%s/index/%s/%s.txt",PathForLotIndex,argv[4],argv[5],argv[1]);

		printf("iindex file %s\n",PathForLotIndex);

                if ((iindexfile[count].fileha = fopen(PathForLotIndex,"rb")) == NULL) {
                        perror(PathForLotIndex);
                }
		else {

                	iindexfile[count].eof = 0;

                	//leser inn første term
                	fread(&iindexfile[count].lastTerm,sizeof(unsigned long),1,iindexfile[count].fileha);
                	fread(&iindexfile[count].lastAntall,sizeof(unsigned long),1,iindexfile[count].fileha);

                	iindexfile[count].nr = count;
			iindexfile[count].Have =0;

			iindexfileArray[count] = iindexfile[count];

			count++;
		}
	}	
	nrOffIindexFiles = count;
	/*
	//inaliserer iindeksene
	for (i=0;i<nrOffIindexFiles;i++) {
		printf("iindex file %s\n",argv[i +2]);

		if ((iindexfile[i].fileha = fopen(argv[i +2],"rb")) == NULL) {
                	perror(argv[i +2]);
			exit(1);
        	}

		iindexfile[i].eof = 0;

		//leser inn første term
		fread(&iindexfile[i].lastTerm,sizeof(unsigned long),1,iindexfile[i].fileha);
                fread(&iindexfile[i].lastAntall,sizeof(unsigned long),1,iindexfile[i].fileha);

		iindexfile[i].nr = i;
		
	}
	*/


	gotEof = 0;
	//for (y=0;y<nrOffIindexFiles;y++) {
	while (nrOffIindexFiles != 0) {
		//hvis vi har fåt en en of file siden sist sorterer vi slik at den kommer nederst
		if (gotEof) {

			qsort(iindexfile, nrOffIindexFiles , sizeof(struct iindexfileFormat), compare_elements_eof);

			gotEof = 0;
			nrOffIindexFiles--;
		}

		//sorter de etter term
		qsort(iindexfile, nrOffIindexFiles , sizeof(struct iindexfileFormat), compare_elements);

		for (i=0;i<nrOffIindexFiles;i++) {
		
			printf("%i: t %lu : %lu eof %i\n",iindexfile[i].nr,iindexfile[i].lastTerm,iindexfile[i].lastAntall,iindexfile[i].eof);
			
		}

		//finner elementene som skal kopieres inn
		mergeTerm = iindexfile[0].lastTerm;
		i = 0;
		totaltAntall = 0;
		while (mergeTerm == iindexfile[i].lastTerm) {
			totaltAntall += iindexfile[i].lastAntall;
			iindexfile[i].Have = 1;
			i++;
		}

		printf("totaltAntall %lu for term %ul\n",totaltAntall,iindexfile[0].lastTerm);

		currentTerm = iindexfile[0].lastTerm;

		startAdress = (unsigned long)ftell(FinalIindexFileFA);
		fwrite(&iindexfile[0].lastTerm,sizeof(iindexfile[0].lastTerm),1,FinalIindexFileFA);
		fwrite(&totaltAntall,sizeof(totaltAntall),1,FinalIindexFileFA);

		

		i = 0;
		//while (mergeTerm == iindexfile[i].lastTerm) {
		//kjører gjenom alle filene og skriver ut de som har index
		for (i=0;i<nrOffIindexFiles;i++) {

			printf("trying %i, Have %i, term %lu\n",iindexfileArray[i].nr,iindexfileArray[i].Have,iindexfile[i].lastTerm);

			if (iindexfileArray[i].Have) {
			iindexfileArray[i].Have = 0;

			printf("aa: %i skriv %lu : %lu fra nr %i\n",i,iindexfile[i].lastTerm,iindexfile[i].lastAntall,iindexfile[i].nr);

			
			/////////////////////////////////////////////////

			for (x=0;x<iindexfile[i].lastAntall;x++) {
                        	//side hedder
				if ((n=fread(&DocID,sizeof(unsigned long),1,iindexfile[i].fileha)) == -1) {
                        		perror("cant read");
					exit(1);
                        	}
				DocIDcount++;

				if ((n=fread(&TermAntall,sizeof(unsigned long),1,iindexfile[i].fileha)) == -1) {
					perror("cant read");
					exit(1);
				}
			
				//printf("DocID %lu %lu: \n",DocID,TermAntall);

				//skriver til final index
				fwrite(&DocID,sizeof(unsigned long),1,FinalIindexFileFA);
				fwrite(&TermAntall,sizeof(unsigned long),1,FinalIindexFileFA);


                        	for (z = 0;z < TermAntall; z++) {
					if ((n=fread(&hit,sizeof(unsigned short),1,iindexfile[i].fileha)) == -1) {
						perror("cant read");
                                        	exit(1);
                                	}

					//skriver til final index
					fwrite(&hit,sizeof(unsigned short),1,FinalIindexFileFA);


                                        //printf("%i,",hit);

                        	}
                        	//printf("\n");
                	}


			////////////////////////////////////////////////
			

			//leser inn neste term
                        fread(&iindexfile[i].lastTerm,sizeof(unsigned long),1,iindexfile[i].fileha);
                        fread(&iindexfile[i].lastAntall,sizeof(unsigned long),1,iindexfile[i].fileha);

			if (feof(iindexfile[i].fileha)) {
				iindexfile[i].eof = 1;
				gotEof = 1;
			}
			
			//i++;
			}
		}

		stoppAdress = (unsigned long)ftell(FinalIindexFileFA);;
                DictionaryPost.WordID = currentTerm;
                DictionaryPost.Adress = startAdress;
                DictionaryPost.SizeForTerm = stoppAdress - startAdress;

                fwrite(&DictionaryPost,sizeof(DictionaryPost),1,FinalDictionaryFileFA);

		printf("WordID %ul, Adress: %ul, SizeForTerm: %ul\n",DictionaryPost.WordID,DictionaryPost.Adress,DictionaryPost.SizeForTerm);

		printf("\n");
	}

	fclose(FinalDictionaryFileFA);
	fclose(FinalIindexFileFA);

	for (i=2;i<nrOffIindexFiles;i++) {
		fclose(iindexfile[i].fileha);
	}

	printf("DocIDcount: %i\n",DocIDcount);

exit(1);
        if ((fileha = fopen(argv[1],"rb")) == NULL) {
                perror(argv[1]);
        }



	while (!feof(fileha)) {
		//wordid hedder
        	fread(&term,sizeof(unsigned long),1,fileha);
        	fread(&Antall,sizeof(unsigned long),1,fileha);

		printf("term %u antall: %u: \n",term,Antall);

		for (i=0;i<Antall;i++) {
			//side hedder
			fread(&DocID,sizeof(unsigned long),1,fileha);
        		fread(&TermAntall,sizeof(unsigned long),1,fileha);

			//printf("DocID %u %u: ",DocID,TermAntall);

			for (y = 0;y < TermAntall; y++) {
                		        fread(&hit,sizeof(unsigned short),1,fileha);

					//printf("%i,",hit);

			}
			//printf("\n");
		}	
		//printf("\n\n");
	}
	fclose(fileha);
}
Ejemplo n.º 5
0
int main(int argc, char** argv)
{
  if (argc<4)
  {
    fprintf(stderr,"%s node_in_file edge_in_file map_out_path",argv[0]);
    exit(-1);
  }
  const char* node_in_fname = argv[1];
  const char* edge_in_fname = argv[2];
  const char* map_out_path = argv[3];
  MFRNodeArray nodes(node_in_fname);
  MFREdgeArray edges(edge_in_fname, nodes);

  
  fprintf(stderr,"Sort edges\n");fflush(stderr);
  qsort(edges.edges, edges.nredges, sizeof(MFREdgeInt), strength_compare);

  fprintf(stderr,"Max edge strength: %d\n",edges.edges[0].strength);
  if (edges.edges[0].strength < edges.edges[edges.nredges-1].strength)
  {
    fprintf(stderr, "@@@@@@@@@@@@@@@@@@@@\nWrong sorting\n@@@@@@@@@@@@@@@@@@@@@@\n");
    exit(-1);
  }
  fprintf(stderr,"Finished sorting edges\n");fflush(stderr);

  fprintf(stderr,"Count edges and store with nodes\n");fflush(stderr);
  {
    int i;
    for (i=0;i<edges.nredges;i++)
    {
      edges.edges[i].nodeA->totalnredges ++;
      edges.edges[i].nodeB->totalnredges ++;
    }
  }
//ier@
  fprintf(stderr,"Finished counting edges\n");fflush(stderr);



  fprintf(stderr,"Cleaning names (should perhaps be done earlier?)\n");fflush(stderr);
  nodes.CleanNames();
  fprintf(stderr,"Finished cleaning names\n");fflush(stderr);


//  nodes.debug_show_if_sorted(12);
  
  MFRQuadTree quadTree(nodes, quadLevels);
  
#define MAX_NODES_PER_QUAD 50  
  
  quadTree.BuildTree(MAX_NODES_PER_QUAD);

  quadTree.AssignQuadIds();

  fprintf(stderr,"Writing out quadtree\n");fflush(stderr);

  nrquadnodeswritten = 0;

  int nredges = edges.nredges;
  int i;

#define MAX_EDGES_PER_NODE 25

  fprintf(stderr,"Collecting edges for quads (limited to max 25 per node)\n");fflush(stderr);
  CollectEdgesIntoQuads(nodes, edges, quadTree, MAX_EDGES_PER_NODE);
  fprintf(stderr, "Done assigning edges to quads (max 25 edges per node)\n");fflush(stderr);
  quadTree.DetermineStats(edges);
  fprintf(stderr,"Writing nodeid lookup table\n");fflush(stderr);
  WriteHashtable(map_out_path, nodes, edges, quadTree);
  WriteMap(map_out_path, nodes, edges, quadTree);
  {
    char fnamebuilder[1024];
    if (quadLevels)
    {
      sprintf(fnamebuilder,"%squad_",map_out_path);
      WriteRootJSON(fnamebuilder, nodes, edges, quadTree, quadTree.root, ECompactModeCompact);

      sprintf(fnamebuilder,"%sequad_",map_out_path);
      WriteRootJSON(fnamebuilder, nodes, edges, quadTree, quadTree.root, ECompactModeExpanded);

      fprintf(stderr,"Collecting edges for quads (unlimited)\n");fflush(stderr);
      CollectEdgesIntoQuads(nodes, edges, quadTree, 0);
      fprintf(stderr, "Done assigning edges to quads (unlimited)\n");fflush(stderr);
      sprintf(fnamebuilder,"%sxquad_",map_out_path);
      WriteRootJSON(fnamebuilder, nodes, edges, quadTree, quadTree.root, ECompactModeFull);
    }
    else
    {
      sprintf(fnamebuilder,"%squad_",map_out_path);
      WriteRootJSON(fnamebuilder, nodes, edges, quadTree, quadTree.root, 0);
    }
  }
  fprintf(stderr,"Finisned\n");fflush(stderr);
  return 0;
}
Ejemplo n.º 6
0
static vorbis_info_floor *floor1_unpack (vorbis_info *vi,oggpack_buffer *opb){
  codec_setup_info     *ci=vi->codec_setup;
  int j,k,count=0,maxclass=-1,rangebits;

  vorbis_info_floor1 *info=_ogg_calloc(1,sizeof(*info));
  /* read partitions */
  info->partitions=oggpack_read(opb,5); /* only 0 to 31 legal */
  for(j=0;j<info->partitions;j++){
    info->partitionclass[j]=oggpack_read(opb,4); /* only 0 to 15 legal */
    if(info->partitionclass[j]<0)goto err_out;
    if(maxclass<info->partitionclass[j])maxclass=info->partitionclass[j];
  }

  /* read partition classes */
  for(j=0;j<maxclass+1;j++){
    info->class_dim[j]=oggpack_read(opb,3)+1; /* 1 to 8 */
    info->class_subs[j]=oggpack_read(opb,2); /* 0,1,2,3 bits */
    if(info->class_subs[j]<0)
      goto err_out;
    if(info->class_subs[j])info->class_book[j]=oggpack_read(opb,8);
    if(info->class_book[j]<0 || info->class_book[j]>=ci->books)
      goto err_out;
    for(k=0;k<(1<<info->class_subs[j]);k++){
      info->class_subbook[j][k]=oggpack_read(opb,8)-1;
      if(info->class_subbook[j][k]<-1 || info->class_subbook[j][k]>=ci->books)
        goto err_out;
    }
  }

  /* read the post list */
  info->mult=oggpack_read(opb,2)+1;     /* only 1,2,3,4 legal now */
  rangebits=oggpack_read(opb,4);
  if(rangebits<0)goto err_out;

  for(j=0,k=0;j<info->partitions;j++){
    count+=info->class_dim[info->partitionclass[j]];
    for(;k<count;k++){
      int t=info->postlist[k+2]=oggpack_read(opb,rangebits);
      if(t<0 || t>=(1<<rangebits))
        goto err_out;
    }
  }
  info->postlist[0]=0;
  info->postlist[1]=1<<rangebits;

  /* don't allow repeated values in post list as they'd result in
     zero-length segments */
  {
    int *sortpointer[VIF_POSIT+2];
    for(j=0;j<count+2;j++)sortpointer[j]=info->postlist+j;
    qsort(sortpointer,count+2,sizeof(*sortpointer),icomp);

    for(j=1;j<count+2;j++)
      if(*sortpointer[j-1]==*sortpointer[j])goto err_out;
  }

  return(info);

 err_out:
  floor1_free_info(info);
  return(NULL);
}
Ejemplo n.º 7
0
int main()
{
        int T, size;
	int i, j, k, temp, unique;
	INTERVAL *node = NULL;
	int open;

        scanf("%d", &T);	
	while(T--)
	{

	        scanf("%d", &size);
		if(size>0)
		{
			node = (INTERVAL *) malloc(sizeof(INTERVAL) * size * 2);

			for(i=0; i<size*2; i=i+2){
				scanf("%d%d", &(node[i].val),&(node[i+1].val) );
				node[i].type = start;
				node[i+1].type = end;
			}

			qsort(node, size*2, sizeof(INTERVAL), int_compare);

			/*
			for(i=0; i<size*2; i = i+2){
				printf("\n%d %d\n", node[i].val,node[i].type );
				printf("%d %d\n", node[i+1].val,node[i+1].type );
			}
			*/

			open = 1;
			printf("(%d,",node[0].val);
		
			for(i=1; i<size*2; i++){

				if( node[i].type == start)
					open++;
				else if( node[i].type == end)
					open--;
				//printf("\nafter parsing %d, open : %d\n", node[i].val, open);

				if(!open)
				{
					if(i+1<size*2)
					{
						if( node[i].val == node[i+1].val)
							continue;
					}		
					printf("%d)",node[i].val);
	
					if(i+1<size*2)
						printf(" (%d,",node[i+1].val);
				}
			}	

			if(node)	
			{
				free(node);
			}
		
			printf("\n");
		}
		
        }
	return 0;
}
Ejemplo n.º 8
0
static void dns_cb(struct dns_ctx *ctx, void *result, void *data) {
  int r = dns_status(ctx);
  int len, i;
  struct dns_check_info *ci = data;
  struct dns_parse p;
  struct dns_rr rr;
  unsigned nrr;
  unsigned char dn[DNS_MAXDN];
  const unsigned char *pkt, *cur, *end;
  char *result_str[MAX_RR] = { NULL };
  char *result_combined = NULL;

  /* If out ci isn't active, we must have timed out already */
  if(!__isactive_ci(ci)) {
    if(result) free(result);
    return;
  }

  ci->timed_out = 0;
  /* If we don't have a result, explode */
  if (!result) {
    ci->error = strdup(dns_strerror(r));
    goto cleanup;
  }

  /* Process the packet */
  pkt = result; end = pkt + r; cur = dns_payload(pkt);
  dns_getdn(pkt, &cur, end, dn, sizeof(dn));
  dns_initparse(&p, NULL, pkt, cur, end);
  p.dnsp_qcls = 0;
  p.dnsp_qtyp = 0;
  nrr = 0;

  while((r = dns_nextrr(&p, &rr)) > 0) {
    if (!dns_dnequal(dn, rr.dnsrr_dn)) continue;
    if ((ci->query_ctype == DNS_C_ANY || ci->query_ctype == rr.dnsrr_cls) &&
        (ci->query_rtype == DNS_T_ANY || ci->query_rtype == rr.dnsrr_typ))
      ++nrr;
    else if (rr.dnsrr_typ == DNS_T_CNAME && !nrr) {
      if (dns_getdn(pkt, &rr.dnsrr_dptr, end,
                    p.dnsp_dnbuf, sizeof(p.dnsp_dnbuf)) <= 0 ||
          rr.dnsrr_dptr != rr.dnsrr_dend) {
        ci->error = strdup("protocol error");
        break;
      }
      else {
        int32_t on = 1;
        /* This actually updates what we're looking for */
        dns_dntodn(p.dnsp_dnbuf, ci->dn, sizeof(dn));
        noit_stats_set_metric(&ci->current, "cname", METRIC_INT32, &on);

        /* Now follow the leader */
        noitL(nldeb, "%s. CNAME %s.\n", dns_dntosp(dn),
              dns_dntosp(p.dnsp_dnbuf));
        dns_dntodn(p.dnsp_dnbuf, dn, sizeof(dn));
        noitL(nldeb, " ---> '%s'\n", dns_dntosp(dn));
      }
    }
  }
  if (!r && !nrr) {
    ci->error = strdup("no data");
  }

  dns_rewind(&p, NULL);
  p.dnsp_qtyp = ci->query_rtype == DNS_T_ANY ? 0 : ci->query_rtype;
  p.dnsp_qcls = ci->query_ctype == DNS_C_ANY ? 0 : ci->query_ctype;
  while(dns_nextrr(&p, &rr) && ci->nrr < MAX_RR)
    decode_rr(ci, &p, &rr, &result_str[ci->nrr]);
  if(ci->sort)
    qsort(result_str, ci->nrr, sizeof(*result_str), cstring_cmp);
  /* calculate the length and allocate on the stack */
  len = 0;
  for(i=0; i<ci->nrr; i++) len += strlen(result_str[i]) + 2;
  result_combined = alloca(len);
  result_combined[0] = '\0';
  /* string it together */
  len = 0;
  for(i=0; i<ci->nrr; i++) {
    int slen;
    if(i) { memcpy(result_combined + len, ", ", 2); len += 2; }
    slen = strlen(result_str[i]);
    memcpy(result_combined + len, result_str[i], slen);
    len += slen;
    result_combined[len] = '\0';
    free(result_str[i]); /* free as we go */
  }
  noit_stats_set_metric(&ci->current, "answer", METRIC_STRING, result_combined);

 cleanup:
  if(result) free(result);
  if(ci->timeout_event) {
    eventer_t e = eventer_remove(ci->timeout_event);
    ci->timeout_event = NULL;
    if(e) eventer_free(e);
  }
  ci->check->flags &= ~NP_RUNNING;
  dns_check_log_results(ci);
  __deactivate_ci(ci);
}
Ejemplo n.º 9
0
static struct ref *do_fetch_pack(struct fetch_pack_args *args,
				 int fd[2],
				 const struct ref *orig_ref,
				 struct ref **sought, int nr_sought,
				 struct shallow_info *si,
				 char **pack_lockfile)
{
	struct ref *ref = copy_ref_list(orig_ref);
	unsigned char sha1[20];
	const char *agent_feature;
	int agent_len;

	sort_ref_list(&ref, ref_compare_name);
	qsort(sought, nr_sought, sizeof(*sought), cmp_ref_by_name);

	if ((args->depth > 0 || is_repository_shallow()) && !server_supports("shallow"))
		die("Server does not support shallow clients");
	if (server_supports("multi_ack_detailed")) {
		if (args->verbose)
			fprintf(stderr, "Server supports multi_ack_detailed\n");
		multi_ack = 2;
		if (server_supports("no-done")) {
			if (args->verbose)
				fprintf(stderr, "Server supports no-done\n");
			if (args->stateless_rpc)
				no_done = 1;
		}
	}
	else if (server_supports("multi_ack")) {
		if (args->verbose)
			fprintf(stderr, "Server supports multi_ack\n");
		multi_ack = 1;
	}
	if (server_supports("side-band-64k")) {
		if (args->verbose)
			fprintf(stderr, "Server supports side-band-64k\n");
		use_sideband = 2;
	}
	else if (server_supports("side-band")) {
		if (args->verbose)
			fprintf(stderr, "Server supports side-band\n");
		use_sideband = 1;
	}
	if (server_supports("allow-tip-sha1-in-want")) {
		if (args->verbose)
			fprintf(stderr, "Server supports allow-tip-sha1-in-want\n");
		allow_unadvertised_object_request |= ALLOW_TIP_SHA1;
	}
	if (server_supports("allow-reachable-sha1-in-want")) {
		if (args->verbose)
			fprintf(stderr, "Server supports allow-reachable-sha1-in-want\n");
		allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
	}
	if (!server_supports("thin-pack"))
		args->use_thin_pack = 0;
	if (!server_supports("no-progress"))
		args->no_progress = 0;
	if (!server_supports("include-tag"))
		args->include_tag = 0;
	if (server_supports("ofs-delta")) {
		if (args->verbose)
			fprintf(stderr, "Server supports ofs-delta\n");
	} else
		prefer_ofs_delta = 0;

	if ((agent_feature = server_feature_value("agent", &agent_len))) {
		agent_supported = 1;
		if (args->verbose && agent_len)
			fprintf(stderr, "Server version is %.*s\n",
				agent_len, agent_feature);
	}

	if (everything_local(args, &ref, sought, nr_sought)) {
		packet_flush(fd[1]);
		goto all_done;
	}
	if (find_common(args, fd, sha1, ref) < 0)
		if (!args->keep_pack)
			/* When cloning, it is not unusual to have
			 * no common commit.
			 */
			warning("no common commits");

	if (args->stateless_rpc)
		packet_flush(fd[1]);
	if (args->depth > 0)
		setup_alternate_shallow(&shallow_lock, &alternate_shallow_file,
					NULL);
	else if (si->nr_ours || si->nr_theirs)
		alternate_shallow_file = setup_temporary_shallow(si->shallow);
	else
		alternate_shallow_file = NULL;
	if (get_pack(args, fd, pack_lockfile))
		die("git fetch-pack: fetch failed.");

 all_done:
	return ref;
}
void sort_signal_list_alphabetically(signal_list_t *list)
{
	qsort(list->pins, list->count,  sizeof(npin_t *), compare_npin_t_names);
}
Ejemplo n.º 11
0
void
_bfd_elf_strtab_finalize (struct elf_strtab_hash *tab)
{
  struct elf_strtab_hash_entry **array, **a, *e;
  bfd_size_type size, amt;

  /* GCC 2.91.66 (egcs-1.1.2) on i386 miscompiles this function when i is
     a 64-bit bfd_size_type: a 64-bit target or --enable-64-bit-bfd.
     Besides, indexing with a long long wouldn't give anything but extra
     cycles.  */
  size_t i;

  /* Sort the strings by suffix and length.  */
  amt = tab->size * sizeof (struct elf_strtab_hash_entry *);
  array = (struct elf_strtab_hash_entry **) bfd_malloc (amt);
  if (array == NULL)
    goto alloc_failure;

  for (i = 1, a = array; i < tab->size; ++i)
    {
      e = tab->array[i];
      if (e->refcount)
	{
	  *a++ = e;
	  /* Adjust the length to not include the zero terminator.  */
	  e->len -= 1;
	}
      else
	e->len = 0;
    }

  size = a - array;
  if (size != 0)
    {
      qsort (array, size, sizeof (struct elf_strtab_hash_entry *), strrevcmp);

      /* Loop over the sorted array and merge suffixes.  Start from the
	 end because we want eg.

	 s1 -> "d"
	 s2 -> "bcd"
	 s3 -> "abcd"

	 to end up as

	 s3 -> "abcd"
	 s2 _____^
	 s1 _______^

	 ie. we don't want s1 pointing into the old s2.  */
      e = *--a;
      e->len += 1;
      while (--a >= array)
	{
	  struct elf_strtab_hash_entry *cmp = *a;

	  cmp->len += 1;
	  if (is_suffix (e, cmp))
	    {
	      cmp->u.suffix = e;
	      cmp->len = -cmp->len;
	    }
	  else
	    e = cmp;
	}
    }

alloc_failure:
  if (array)
    free (array);

  /* Assign positions to the strings we want to keep.  */
  size = 1;
  for (i = 1; i < tab->size; ++i)
    {
      e = tab->array[i];
      if (e->refcount && e->len > 0)
	{
	  e->u.index = size;
	  size += e->len;
	}
    }

  tab->sec_size = size;

  /* Adjust the rest.  */
  for (i = 1; i < tab->size; ++i)
    {
      e = tab->array[i];
      if (e->refcount && e->len < 0)
	e->u.index = e->u.suffix->u.index + (e->u.suffix->len + e->len);
    }
}
Ejemplo n.º 12
0
/*
 *	compute_tsvector_stats() -- compute statistics for a tsvector column
 *
 *	This functions computes statistics that are useful for determining @@
 *	operations' selectivity, along with the fraction of non-null rows and
 *	average width.
 *
 *	Instead of finding the most common values, as we do for most datatypes,
 *	we're looking for the most common lexemes. This is more useful, because
 *	there most probably won't be any two rows with the same tsvector and thus
 *	the notion of a MCV is a bit bogus with this datatype. With a list of the
 *	most common lexemes we can do a better job at figuring out @@ selectivity.
 *
 *	For the same reasons we assume that tsvector columns are unique when
 *	determining the number of distinct values.
 *
 *	The algorithm used is Lossy Counting, as proposed in the paper "Approximate
 *	frequency counts over data streams" by G. S. Manku and R. Motwani, in
 *	Proceedings of the 28th International Conference on Very Large Data Bases,
 *	Hong Kong, China, August 2002, section 4.2. The paper is available at
 *	http://www.vldb.org/conf/2002/S10P03.pdf
 *
 *	The Lossy Counting (aka LC) algorithm goes like this:
 *	Let s be the threshold frequency for an item (the minimum frequency we
 *	are interested in) and epsilon the error margin for the frequency. Let D
 *	be a set of triples (e, f, delta), where e is an element value, f is that
 *	element's frequency (actually, its current occurrence count) and delta is
 *	the maximum error in f. We start with D empty and process the elements in
 *	batches of size w. (The batch size is also known as "bucket size" and is
 *	equal to 1/epsilon.) Let the current batch number be b_current, starting
 *	with 1. For each element e we either increment its f count, if it's
 *	already in D, or insert a new triple into D with values (e, 1, b_current
 *	- 1). After processing each batch we prune D, by removing from it all
 *	elements with f + delta <= b_current.  After the algorithm finishes we
 *	suppress all elements from D that do not satisfy f >= (s - epsilon) * N,
 *	where N is the total number of elements in the input.  We emit the
 *	remaining elements with estimated frequency f/N.  The LC paper proves
 *	that this algorithm finds all elements with true frequency at least s,
 *	and that no frequency is overestimated or is underestimated by more than
 *	epsilon.  Furthermore, given reasonable assumptions about the input
 *	distribution, the required table size is no more than about 7 times w.
 *
 *	We set s to be the estimated frequency of the K'th word in a natural
 *	language's frequency table, where K is the target number of entries in
 *	the MCELEM array plus an arbitrary constant, meant to reflect the fact
 *	that the most common words in any language would usually be stopwords
 *	so we will not actually see them in the input.	We assume that the
 *	distribution of word frequencies (including the stopwords) follows Zipf's
 *	law with an exponent of 1.
 *
 *	Assuming Zipfian distribution, the frequency of the K'th word is equal
 *	to 1/(K * H(W)) where H(n) is 1/2 + 1/3 + ... + 1/n and W is the number of
 *	words in the language.	Putting W as one million, we get roughly 0.07/K.
 *	Assuming top 10 words are stopwords gives s = 0.07/(K + 10).  We set
 *	epsilon = s/10, which gives bucket width w = (K + 10)/0.007 and
 *	maximum expected hashtable size of about 1000 * (K + 10).
 *
 *	Note: in the above discussion, s, epsilon, and f/N are in terms of a
 *	lexeme's frequency as a fraction of all lexemes seen in the input.
 *	However, what we actually want to store in the finished pg_statistic
 *	entry is each lexeme's frequency as a fraction of all rows that it occurs
 *	in.  Assuming that the input tsvectors are correctly constructed, no
 *	lexeme occurs more than once per tsvector, so the final count f is a
 *	correct estimate of the number of input tsvectors it occurs in, and we
 *	need only change the divisor from N to nonnull_cnt to get the number we
 *	want.
 */
static void
compute_tsvector_stats(VacAttrStats *stats,
					   AnalyzeAttrFetchFunc fetchfunc,
					   int samplerows,
					   double totalrows)
{
	int			num_mcelem;
	int			null_cnt = 0;
	double		total_width = 0;

	/* This is D from the LC algorithm. */
	HTAB	   *lexemes_tab;
	HASHCTL		hash_ctl;
	HASH_SEQ_STATUS scan_status;

	/* This is the current bucket number from the LC algorithm */
	int			b_current;

	/* This is 'w' from the LC algorithm */
	int			bucket_width;
	int			vector_no,
				lexeme_no;
	LexemeHashKey hash_key;
	TrackItem  *item;

	/*
	 * We want statistics_target * 10 lexemes in the MCELEM array.	This
	 * multiplier is pretty arbitrary, but is meant to reflect the fact that
	 * the number of individual lexeme values tracked in pg_statistic ought to
	 * be more than the number of values for a simple scalar column.
	 */
	num_mcelem = stats->attr->attstattarget * 10;

	/*
	 * We set bucket width equal to (num_mcelem + 10) / 0.007 as per the
	 * comment above.
	 */
	bucket_width = (num_mcelem + 10) * 1000 / 7;

	/*
	 * Create the hashtable. It will be in local memory, so we don't need to
	 * worry about overflowing the initial size. Also we don't need to pay any
	 * attention to locking and memory management.
	 */
	MemSet(&hash_ctl, 0, sizeof(hash_ctl));
	hash_ctl.keysize = sizeof(LexemeHashKey);
	hash_ctl.entrysize = sizeof(TrackItem);
	hash_ctl.hash = lexeme_hash;
	hash_ctl.match = lexeme_match;
	hash_ctl.hcxt = CurrentMemoryContext;
	lexemes_tab = hash_create("Analyzed lexemes table",
							  bucket_width * 7,
							  &hash_ctl,
					HASH_ELEM | HASH_FUNCTION | HASH_COMPARE | HASH_CONTEXT);

	/* Initialize counters. */
	b_current = 1;
	lexeme_no = 0;

	/* Loop over the tsvectors. */
	for (vector_no = 0; vector_no < samplerows; vector_no++)
	{
		Datum		value;
		bool		isnull;
		TSVector	vector;
		WordEntry  *curentryptr;
		char	   *lexemesptr;
		int			j;

		vacuum_delay_point();

		value = fetchfunc(stats, vector_no, &isnull);

		/*
		 * Check for null/nonnull.
		 */
		if (isnull)
		{
			null_cnt++;
			continue;
		}

		/*
		 * Add up widths for average-width calculation.  Since it's a
		 * tsvector, we know it's varlena.  As in the regular
		 * compute_minimal_stats function, we use the toasted width for this
		 * calculation.
		 */
		total_width += VARSIZE_ANY(DatumGetPointer(value));

		/*
		 * Now detoast the tsvector if needed.
		 */
		vector = DatumGetTSVector(value);

		/*
		 * We loop through the lexemes in the tsvector and add them to our
		 * tracking hashtable.	Note: the hashtable entries will point into
		 * the (detoasted) tsvector value, therefore we cannot free that
		 * storage until we're done.
		 */
		lexemesptr = STRPTR(vector);
		curentryptr = ARRPTR(vector);
		for (j = 0; j < vector->size; j++)
		{
			bool		found;

			/* Construct a hash key */
			hash_key.lexeme = lexemesptr + curentryptr->pos;
			hash_key.length = curentryptr->len;

			/* Lookup current lexeme in hashtable, adding it if new */
			item = (TrackItem *) hash_search(lexemes_tab,
											 (const void *) &hash_key,
											 HASH_ENTER, &found);

			if (found)
			{
				/* The lexeme is already on the tracking list */
				item->frequency++;
			}
			else
			{
				/* Initialize new tracking list element */
				item->frequency = 1;
				item->delta = b_current - 1;
			}

			/* lexeme_no is the number of elements processed (ie N) */
			lexeme_no++;

			/* We prune the D structure after processing each bucket */
			if (lexeme_no % bucket_width == 0)
			{
				prune_lexemes_hashtable(lexemes_tab, b_current);
				b_current++;
			}

			/* Advance to the next WordEntry in the tsvector */
			curentryptr++;
		}
	}

	/* We can only compute real stats if we found some non-null values. */
	if (null_cnt < samplerows)
	{
		int			nonnull_cnt = samplerows - null_cnt;
		int			i;
		TrackItem **sort_table;
		int			track_len;
		int			cutoff_freq;
		int			minfreq,
					maxfreq;

		stats->stats_valid = true;
		/* Do the simple null-frac and average width stats */
		stats->stanullfrac = (double) null_cnt / (double) samplerows;
		stats->stawidth = total_width / (double) nonnull_cnt;

		/* Assume it's a unique column (see notes above) */
		stats->stadistinct = -1.0;

		/*
		 * Construct an array of the interesting hashtable items, that is,
		 * those meeting the cutoff frequency (s - epsilon)*N.	Also identify
		 * the minimum and maximum frequencies among these items.
		 *
		 * Since epsilon = s/10 and bucket_width = 1/epsilon, the cutoff
		 * frequency is 9*N / bucket_width.
		 */
		cutoff_freq = 9 * lexeme_no / bucket_width;

		i = hash_get_num_entries(lexemes_tab);	/* surely enough space */
		sort_table = (TrackItem **) palloc(sizeof(TrackItem *) * i);

		hash_seq_init(&scan_status, lexemes_tab);
		track_len = 0;
		minfreq = lexeme_no;
		maxfreq = 0;
		while ((item = (TrackItem *) hash_seq_search(&scan_status)) != NULL)
		{
			if (item->frequency > cutoff_freq)
			{
				sort_table[track_len++] = item;
				minfreq = Min(minfreq, item->frequency);
				maxfreq = Max(maxfreq, item->frequency);
			}
		}
		Assert(track_len <= i);

		/* emit some statistics for debug purposes */
		elog(DEBUG3, "tsvector_stats: target # mces = %d, bucket width = %d, "
			 "# lexemes = %d, hashtable size = %d, usable entries = %d",
			 num_mcelem, bucket_width, lexeme_no, i, track_len);

		/*
		 * If we obtained more lexemes than we really want, get rid of those
		 * with least frequencies.	The easiest way is to qsort the array into
		 * descending frequency order and truncate the array.
		 */
		if (num_mcelem < track_len)
		{
			qsort(sort_table, track_len, sizeof(TrackItem *),
				  trackitem_compare_frequencies_desc);
			/* reset minfreq to the smallest frequency we're keeping */
			minfreq = sort_table[num_mcelem - 1]->frequency;
		}
		else
			num_mcelem = track_len;

		/* Generate MCELEM slot entry */
		if (num_mcelem > 0)
		{
			MemoryContext old_context;
			Datum	   *mcelem_values;
			float4	   *mcelem_freqs;

			/*
			 * We want to store statistics sorted on the lexeme value using
			 * first length, then byte-for-byte comparison. The reason for
			 * doing length comparison first is that we don't care about the
			 * ordering so long as it's consistent, and comparing lengths
			 * first gives us a chance to avoid a strncmp() call.
			 *
			 * This is different from what we do with scalar statistics --
			 * they get sorted on frequencies. The rationale is that we
			 * usually search through most common elements looking for a
			 * specific value, so we can grab its frequency.  When values are
			 * presorted we can employ binary search for that.	See
			 * ts_selfuncs.c for a real usage scenario.
			 */
			qsort(sort_table, num_mcelem, sizeof(TrackItem *),
				  trackitem_compare_lexemes);

			/* Must copy the target values into anl_context */
			old_context = MemoryContextSwitchTo(stats->anl_context);

			/*
			 * We sorted statistics on the lexeme value, but we want to be
			 * able to find out the minimal and maximal frequency without
			 * going through all the values.  We keep those two extra
			 * frequencies in two extra cells in mcelem_freqs.
			 */
			mcelem_values = (Datum *) palloc(num_mcelem * sizeof(Datum));
			mcelem_freqs = (float4 *) palloc((num_mcelem + 2) * sizeof(float4));

			/*
			 * See comments above about use of nonnull_cnt as the divisor for
			 * the final frequency estimates.
			 */
			for (i = 0; i < num_mcelem; i++)
			{
				TrackItem  *item = sort_table[i];

				mcelem_values[i] =
					PointerGetDatum(cstring_to_text_with_len(item->key.lexeme,
														  item->key.length));
				mcelem_freqs[i] = (double) item->frequency / (double) nonnull_cnt;
			}
			mcelem_freqs[i++] = (double) minfreq / (double) nonnull_cnt;
			mcelem_freqs[i] = (double) maxfreq / (double) nonnull_cnt;
			MemoryContextSwitchTo(old_context);

			stats->stakind[0] = STATISTIC_KIND_MCELEM;
			stats->staop[0] = TextEqualOperator;
			stats->stanumbers[0] = mcelem_freqs;
			/* See above comment about two extra frequency fields */
			stats->numnumbers[0] = num_mcelem + 2;
			stats->stavalues[0] = mcelem_values;
			stats->numvalues[0] = num_mcelem;
			/* We are storing text values */
			stats->statypid[0] = TEXTOID;
			stats->statyplen[0] = -1;	/* typlen, -1 for varlena */
			stats->statypbyval[0] = false;
			stats->statypalign[0] = 'i';
		}
	}
	else
	{
		/* We found only nulls; assume the column is entirely null */
		stats->stats_valid = true;
		stats->stanullfrac = 1.0;
		stats->stawidth = 0;	/* "unknown" */
		stats->stadistinct = 0.0;		/* "unknown" */
	}

	/*
	 * We don't need to bother cleaning up any of our temporary palloc's. The
	 * hashtable should also go away, as it used a child memory context.
	 */
}
Ejemplo n.º 13
0
//{{{uint64_t uint32_t_ll_non_leading_serialize_to_wah(void *deserialized,
uint64_t uint32_t_ll_non_leading_serialize_to_wah(void *deserialized,
                                                  void **serialized)
{
    if (deserialized == NULL) {
        *serialized = NULL;
        return 0;
    }

    struct uint32_t_ll_bpt_non_leading_data *d =  
            (struct uint32_t_ll_bpt_non_leading_data *)deserialized;

    uint32_t SA_len, SE_len, serialized_len;

    if (d->SA != NULL)
        SA_len = d->SA->len;
    else
        SA_len = 0;

    if (d->SE != NULL)
        SE_len = d->SE->len;
    else
        SE_len = 0;

    struct wah_bpt_non_leading_data *wah_d =  
        (struct wah_bpt_non_leading_data *)
        calloc(1,
               sizeof(struct wah_bpt_non_leading_data));

    wah_d->SA = NULL;
    wah_d->SE = NULL;

    if (d->SA != NULL) {
        uint32_t *B = (uint32_t *)calloc(SA_len,sizeof(uint32_t));
        uint32_t i;
        struct uint32_t_ll_node *curr;

        curr = d->SA->head;
        i = 0;
        while (curr != NULL) {
            B[i++] = curr->val;
            curr = curr->next;
        }

        if (i != d->SA->len)
            errx(1, "Incorrect length in SA:leading_data.");

        qsort(B, d->SA->len, sizeof(uint32_t), uint32_t_cmp);
        uint8_t *w = uints_to_wah(B, d->SA->len);

        wah_d->SA = w;
        free(B);
    }

    if (d->SE != NULL) {
        uint32_t *B = (uint32_t *)calloc(SE_len,sizeof(uint32_t));
        uint32_t i;
        struct uint32_t_ll_node *curr;

        curr = d->SE->head;
        i = 0;
        while (curr != NULL) {
            B[i++] = curr->val;
            curr = curr->next;
        }

        if (i != d->SE->len)
            errx(1, "Incorrect length in SE:leading_data.");

        qsort(B, d->SE->len, sizeof(uint32_t), uint32_t_cmp);
        uint8_t *w = uints_to_wah(B, d->SE->len);

        wah_d->SE = w;
        free(B);
    }

    uint64_t w_size = wah_non_leading_serialize(wah_d, serialized);

    if (wah_d->SA != NULL)
        free(wah_d->SA);
    if (wah_d->SE != NULL)
        free(wah_d->SE);
    free(wah_d);

    return w_size;
}
Ejemplo n.º 14
0
static PSFINFO *LoadPSF(char *path, int level, int type,const char *pathDir) // Type==1 for just info load.
{
    FILE *fp;
    u8 *in,*out=0;
	u8 head[4];
    u32 reserved;
    u32 complen;
    u32 crc32;
    uLongf outlen;
	PSFINFO *psfi=NULL;
	PSFINFO *tmpi=NULL;

    u32 offset, plength;
	
	sexypsf_missing_psflib=0;

#ifdef DEBUG
	fprintf(stderr, "Searching (%s)\n", path);
#endif

	fp=fopen(path,"rb");
    if(!fp) return 0;

	fread(head,1,4,fp);
	if(memcmp(head,"PSF\x01",4)) return(0);

	psfi=malloc(sizeof(PSFINFO));
	if( psfi == NULL ) return(0);
	memset(psfi,0,sizeof(PSFINFO));
    psfi->stop=~0;
    psfi->fade=0;

    fread(&reserved,1,4,fp);
    fread(&complen,1,4,fp);
	complen=BFLIP32(complen);

    fread(&crc32,1,4,fp);
	crc32=BFLIP32(crc32);

#ifdef DEBUG
	fprintf(stderr, "CRC32 = 0x%x\n", crc32);
#endif

    fseek(fp,reserved,SEEK_CUR);

    if(type)
		fseek(fp,complen,SEEK_CUR);
    else {
        in=malloc(complen);
		if( !in ) return(0);
        out=malloc(1024*1024*2+0x800);
		if( !out ) {
			free(in);
			return(0);
		}
        fread(in,1,complen,fp);
        outlen=1024*1024*2;
        uncompress(out,&outlen,in,complen);
        free(in);

		psxRegs->pc = out[0x10] | out[0x11]<<8 | out[0x12]<<16 | out[0x13]<<24;
		psxRegs->GPR.n.gp = out[0x14] | out[0x15]<<8 | out[0x16]<<16 | out[0x17]<<24;
		psxRegs->GPR.n.sp = out[0x30] | out[0x31]<<8 | out[0x32]<<16 | out[0x33]<<24;

        if (psxRegs->GPR.n.sp == 0) psxRegs->GPR.n.sp = 0x801fff00;

#ifdef DEBUG
		fprintf(stderr, "%d Level: PC %x GP %x SP %x\n",
			level, psxRegs->pc, psxRegs->GPR.n.gp, psxRegs->GPR.n.sp);
#endif

 		if(level) {
			offset = out[0x18] | out[0x19]<<8 | out[0x1a]<<16 | out[0x1b]<<24;
			offset &= 0x3fffffff;	// kill any MIPS cache segment indicators
			plength = out[0x1c] | out[0x1d]<<8 | out[0x1e]<<16 | out[0x1f]<<24;

#ifdef DEBUG
			fprintf(stderr, "%d Level: offset %x plength: %d [%d]\n", level, offset, plength, plength);
#endif

			LoadPSXMem(offset,plength,(char*)out+0x800);
			free(out);

			// I don't really know how does this
			// work, but apparently it breaks the
			// loop that makes ff6 and ct to take
			// several seconds to start ...
			if( crc32 == 0xEFDE8EEE || crc32 == 0x545D9F65 )
				psxMemWrite32(0x5A66C, 0);

			// Same as above for Popolocrois
			if( crc32 == 0xF535726 )
				psxMemWrite32(0x5A990, 0);
		}
	}

    {
		u8 tagdata[5];
        if(fread(tagdata,1,5,fp)==5) {
			if(!memcmp(tagdata,"[TAG]",5)) {
				char linebuf[1024];
				while(fgets(linebuf,1024,fp)>0) {
					int x;
					char *key=0,*value=0;

					if(!GetKeyVal(linebuf,&key,&value)) {
						if(key) free(key);
						if(value) free(value);
						continue;
					}

					AddKV(&psfi->tags,key,value);

					if(!level) {
       					static char *yoinks[8]={"title","artist","game","year","genre",
                                  "copyright","psfby","comment"};
						char **yoinks2[8]={&psfi->title,&psfi->artist,&psfi->game,&psfi->year,&psfi->genre,
                                    &psfi->copyright,&psfi->psfby,&psfi->comment};
						for(x=0;x<8;x++)
							if(!strcasecmp(key,yoinks[x]))
								*yoinks2[x]=value;
						if(!strcasecmp(key,"length"))
							psfi->stop=TimeToMS(value);
						else if(!strcasecmp(key,"fade"))
							psfi->fade=TimeToMS(value);
					}

					if(!strcasecmp(key,"_lib") && !type) {
						char *tmpfn;
						/* Load file name "value" from the directory specified in
						   the full path(directory + file name) "path"
						*/
                        //printf("yo %s\nya %s\nyu%s\n",path,value,pathDir);
						tmpfn=GetFileWithBase(path,value,pathDir);
                        //printf("ru: %s\n",tmpfn);
						if(!(tmpi=LoadPSF(tmpfn,level+1,0,pathDir))) {
							//free(key);
							//free(value);
							sexypsf_missing_psflib=1;
							strcpy(sexypsf_psflib_str,value);
							free(tmpfn);
 							if(!level) free(out);
							fclose(fp);
							FreeTags(psfi->tags);
							free(psfi);
							return(0);
						}
						FreeTags(tmpi->tags);
						free(tmpi);
						free(tmpfn);
					}
				}
			}
		}
	}

    fclose(fp);

	/* Now, if we're at level 0(main PSF), load the main executable, and any libN stuff */
    if(!level && !type) {
		offset = out[0x18] | out[0x19]<<8 | out[0x1a]<<16 | out[0x1b]<<24;
		offset &= 0x3fffffff;	// kill any MIPS cache segment indicators
		plength = out[0x1c] | out[0x1d]<<8 | out[0x1e]<<16 | out[0x1f]<<24;

		// TODO - investigate: should i just make
		//        plength = outlen-0x800?

		// Philosoma has an illegal "plength".  *sigh*
		if (plength > (outlen-0x800))
		{
			plength = outlen-0x800;
		}

		if( psfi->game ) {
			// Suikoden Tenmei has an illegal "plength". *sigh sigh*
			if( !strncmp(psfi->game, "Suikoden: Tenmei no Chikai", 26) ) {
				plength = outlen-0x800;
			}

			// Einhänder has an illegal "plength". *sigh sigh sigh*
			if( !strncmp(psfi->game, "Einhänder", 9) ) {
				plength = outlen-0x800;
			}
		}

#ifdef DEBUG
		fprintf(stderr, "%d Level: offset %x plength: %d [%d]\n", level, offset, plength, outlen-2048);
#endif

		LoadPSXMem(offset,plength,(char*)out+0x800);
		free(out);
    }

	if(!type)	/* Load libN */{
		LIBNCACHE *cache;
		PSFTAG *tag;
		unsigned int libncount=0;
		unsigned int cur=0;

		tag=psfi->tags;
		while(tag) {
			if(!strncasecmp(tag->key,"_lib",4) && tag->key[4])
			libncount++;
			tag=tag->next;
		}

		if(libncount) {
			cache=malloc(sizeof(LIBNCACHE)*libncount);

			tag=psfi->tags;
			while(tag) {
				if(!strncasecmp(tag->key,"_lib",4) && tag->key[4]) {
					cache[cur].num=atoi(&tag->key[4]);
					cache[cur].value=tag->value;
					cur++;
				}
				tag=tag->next;
			}
			qsort(cache, libncount, sizeof(LIBNCACHE), ccomp);
			for(cur=0;cur<libncount;cur++) {
				u32 ba[3];
				char *tmpfn;

 				if(cache[cur].num < 2) continue;

				ba[0]=psxRegs->pc;
				ba[1]=psxRegs->GPR.n.gp;
				ba[2]=psxRegs->GPR.n.sp;

				/* Load file name "value" from the directory specified in
				the full path(directory + file name) "path"
				*/
				tmpfn=GetFileWithBase(path,cache[cur].value,pathDir);
				if(!(tmpi=LoadPSF(tmpfn,level+1,0,pathDir))) {
					//free(key);
					//free(value);
					//free(tmpfn);
					//fclose(fp);
			        //return(0);
				} else {
                    FreeTags(tmpi->tags);
                    free(tmpi);
                }
                free(tmpfn);


				psxRegs->pc=ba[0];
       			psxRegs->GPR.n.gp=ba[1];
				psxRegs->GPR.n.sp=ba[2];
			}
			free(cache);
		}	// if(libncount)

	}	// if(!type)

	return(psfi);
}
Ejemplo n.º 15
0
static void cvalue_list_sort_abs(cvalue_list *l)
{
    qsort(l->data, l->len, sizeof(l->data[0]), cvalue_sort_abs_comp);
}
Ejemplo n.º 16
0
/* This is used by cow_dir too. */
int merge_dir_lists(region_t r,
		    struct filesys_obj *dir1,
		    struct filesys_obj *dir2,
		    seqt_t *result,
		    int *err)
{
  int count1, count2;
  seqt_t got;
  seqf_t buf;
  seqf_t *array1, *array2;
  int i;

  region_t r2 = region_make();

  count1 = dir1->vtable->list(dir1, r2, &got, err);
  if(count1 < 0) { region_free(r2); return -1; }
  array1 = region_alloc(r2, count1 * sizeof(seqf_t));
  buf = flatten(r, got);
  for(i = 0; i < count1; i++) {
    int inode, type;
    seqf_t name;
    int ok = 1;
    m_int(&ok, &buf, &inode);
    m_int(&ok, &buf, &type);
    m_lenblock(&ok, &buf, &name);
    if(ok) {
      array1[i] = name;
    }
    else { region_free(r2); *err = EIO; return -1; }
  }

  count2 = dir2->vtable->list(dir2, r2, &got, err);
  if(count2 < 0) { region_free(r2); return -1; }
  array2 = region_alloc(r2, count2 * sizeof(seqf_t));
  buf = flatten(r, got);
  for(i = 0; i < count2; i++) {
    int inode, type;
    seqf_t name;
    int ok = 1;
    m_int(&ok, &buf, &inode);
    m_int(&ok, &buf, &type);
    m_lenblock(&ok, &buf, &name);
    if(ok) {
      array2[i] = name;
    }
    else { region_free(r2); *err = EIO; return -1; }
  }

  /* Sort the sequences to make it easy to merge them. */
  qsort(array1, count1, sizeof(seqf_t), sort_seqf_compare);
  qsort(array2, count2, sizeof(seqf_t), sort_seqf_compare);

  /* Merge the two sequences. */
  {
    cbuf_t buf = cbuf_make(r, 100);
    int count = 0;
    int i1 = 0, i2 = 0;
    while(1) {
      int comp;
      if(i1 < count1 && i2 < count2) {
	comp = seqf_compare(array1[i1], array2[i2]);
      }
      else if(i1 < count1) comp = -1;
      else if(i2 < count2) comp = 1;
      else break;

      cbuf_put_int(buf, 0); /* d_ino */
      cbuf_put_int(buf, 0); /* d_type */
      if(comp < 0) {
	cbuf_put_int(buf, array1[i1].size);
	cbuf_put_seqf(buf, array1[i1]);
	i1++;
      }
      else if(comp > 0) {
	cbuf_put_int(buf, array2[i2].size);
	cbuf_put_seqf(buf, array2[i2]);
	i2++;
      }
      else {
	cbuf_put_int(buf, array1[i1].size);
	cbuf_put_seqf(buf, array1[i1]);
	i1++;
	i2++;
      }
      count++;
    }
    *result = seqt_of_cbuf(buf);
    region_free(r2);
    return count;
  }
}
Ejemplo n.º 17
0
void content_playlist_qsort(content_playlist_t *playlist, content_playlist_sort_fun_t *fn)
{
   qsort(playlist->entries, playlist->size, sizeof(content_playlist_entry_t),
         (int (*)(const void *, const void *))fn);
}
Ejemplo n.º 18
0
void sa_sort(Array array) {
	sa_assert_element_size(array);
	qsort(array->a, array->n, sizeof(String), string_compare);
}
Ejemplo n.º 19
0
static vorbis_look_floor *floor1_look(vorbis_dsp_state *vd,
                                      vorbis_info_floor *in){

  int *sortpointer[VIF_POSIT+2];
  vorbis_info_floor1 *info=(vorbis_info_floor1 *)in;
  vorbis_look_floor1 *look=_ogg_calloc(1,sizeof(*look));
  int i,j,n=0;

  look->vi=info;
  look->n=info->postlist[1];

  /* we drop each position value in-between already decoded values,
     and use linear interpolation to predict each new value past the
     edges.  The positions are read in the order of the position
     list... we precompute the bounding positions in the lookup.  Of
     course, the neighbors can change (if a position is declined), but
     this is an initial mapping */

  for(i=0;i<info->partitions;i++)n+=info->class_dim[info->partitionclass[i]];
  n+=2;
  look->posts=n;

  /* also store a sorted position index */
  for(i=0;i<n;i++)sortpointer[i]=info->postlist+i;
  qsort(sortpointer,n,sizeof(*sortpointer),icomp);

  /* points from sort order back to range number */
  for(i=0;i<n;i++)look->forward_index[i]=sortpointer[i]-info->postlist;
  /* points from range order to sorted position */
  for(i=0;i<n;i++)look->reverse_index[look->forward_index[i]]=i;
  /* we actually need the post values too */
  for(i=0;i<n;i++)look->sorted_index[i]=info->postlist[look->forward_index[i]];

  /* quantize values to multiplier spec */
  switch(info->mult){
  case 1: /* 1024 -> 256 */
    look->quant_q=256;
    break;
  case 2: /* 1024 -> 128 */
    look->quant_q=128;
    break;
  case 3: /* 1024 -> 86 */
    look->quant_q=86;
    break;
  case 4: /* 1024 -> 64 */
    look->quant_q=64;
    break;
  }

  /* discover our neighbors for decode where we don't use fit flags
     (that would push the neighbors outward) */
  for(i=0;i<n-2;i++){
    int lo=0;
    int hi=1;
    int lx=0;
    int hx=look->n;
    int currentx=info->postlist[i+2];
    for(j=0;j<i+2;j++){
      int x=info->postlist[j];
      if(x>lx && x<currentx){
        lo=j;
        lx=x;
      }
      if(x<hx && x>currentx){
        hi=j;
        hx=x;
      }
    }
    look->loneighbor[i]=lo;
    look->hineighbor[i]=hi;
  }

  return(look);
}
Ejemplo n.º 20
0
void sa_sort_dec_ignore_case(Array array) {
	sa_assert_element_size(array);
	qsort(array->a, array->n, sizeof(String), string_compare_dec_ignore_case);
}
Ejemplo n.º 21
0
PHPAPI int php_scandir(const char *dirname, struct dirent **namelist[], int (*selector) (const struct dirent *entry), int (*compare) (const struct dirent **a, const struct dirent **b))
{
	DIR *dirp = NULL;
	struct dirent **vector = NULL;
	int vector_size = 0;
	int nfiles = 0;
	char entry[sizeof(struct dirent)+MAXPATHLEN];
	struct dirent *dp = (struct dirent *)&entry;

	if (namelist == NULL) {
		return -1;
	}

	if (!(dirp = opendir(dirname))) {
		return -1;
	}

	while (!php_readdir_r(dirp, (struct dirent *)entry, &dp) && dp) {
		int dsize = 0;
		struct dirent *newdp = NULL;

		if (selector && (*selector)(dp) == 0) {
			continue;
		}

		if (nfiles == vector_size) {
			struct dirent **newv;
			if (vector_size == 0) {
				vector_size = 10;
			} else { 
				vector_size *= 2;
			}

			newv = (struct dirent **) realloc (vector, vector_size * sizeof (struct dirent *));
			if (!newv) {
				return -1;
			}
			vector = newv;
		}

		dsize = sizeof (struct dirent) + ((strlen(dp->d_name) + 1) * sizeof(char));
		newdp = (struct dirent *) malloc(dsize);

		if (newdp == NULL) {
			goto fail;
		}

		vector[nfiles++] = (struct dirent *) memcpy(newdp, dp, dsize);
	}

	closedir(dirp);

	*namelist = vector;

	if (compare) {
		qsort (*namelist, nfiles, sizeof(struct dirent *), compare);
	}

	return nfiles;

fail:
	while (nfiles-- > 0) {
		free(vector[nfiles]);
	}
	free(vector);
	return -1;	
}
Ejemplo n.º 22
0
void GDALdllImageFilledPolygon(int nRasterXSize, int nRasterYSize, 
                               int nPartCount, int *panPartSize,
                               double *padfX, double *padfY,
                               double *dfVariant,
                               llScanlineFunc pfnScanlineFunc, void *pCBData )
{
/*************************************************************************
2nd Method (method=1):
=====================
No known bug
*************************************************************************/

    int i;
    int y;
    int miny, maxy,minx,maxx;
    double dminy, dmaxy;
    double dx1, dy1;
    double dx2, dy2;
    double dy;
    double intersect;

    int ind1, ind2;
    int ints, n, part;
    int *polyInts;

    int horizontal_x1, horizontal_x2;

    if (!nPartCount) {
        return;
    }

    n = 0;
    for( part = 0; part < nPartCount; part++ )
        n += panPartSize[part];

    polyInts = (int *) malloc(sizeof(int) * n);

    dminy = padfY[0];
    dmaxy = padfY[0];
    for (i=1; (i < n); i++) {

        if (padfY[i] < dminy) {
            dminy = padfY[i];
        }
        if (padfY[i] > dmaxy) {
            dmaxy = padfY[i];
        }
    }
    miny = (int) dminy;
    maxy = (int) dmaxy;
    
    
    if( miny < 0 )
        miny = 0;
    if( maxy >= nRasterYSize )
        maxy = nRasterYSize-1;
   
    
    minx = 0;
    maxx = nRasterXSize - 1;

    /* Fix in 1.3: count a vertex only once */
    for (y=miny; y <= maxy; y++) {
        int	partoffset = 0;

        dy = y +0.5; /* center height of line*/
         

        part = 0;
        ints = 0;

        /*Initialize polyInts, otherwise it can sometimes causes a seg fault */
        memset(polyInts, -1, sizeof(int) * n);

        for (i=0; (i < n); i++) {
        
            
            if( i == partoffset + panPartSize[part] ) {
                partoffset += panPartSize[part];
                part++;
            }

            if( i == partoffset ) {
                ind1 = partoffset + panPartSize[part] - 1;
                ind2 = partoffset;
            } else {
                ind1 = i-1;
                ind2 = i;
            }
	    

            dy1 = padfY[ind1];
            dy2 = padfY[ind2];
            

            if( (dy1 < dy && dy2 < dy) || (dy1 > dy && dy2 > dy) )
                continue;

            if (dy1 < dy2) {
                dx1 = padfX[ind1];
                dx2 = padfX[ind2];
            } else if (dy1 > dy2) {
                dy2 = padfY[ind1];
                dy1 = padfY[ind2];
                dx2 = padfX[ind1];
                dx1 = padfX[ind2];
            } else /* if (fabs(dy1-dy2)< 1.e-6) */
            {
                
                /*AE: DO NOT skip bottom horizontal segments 
                  -Fill them separately- 
                  They are not taken into account twice.*/
                if (padfX[ind1] > padfX[ind2])
                {
                    horizontal_x1 = (int) floor(padfX[ind2]+0.5);
                    horizontal_x2 = (int) floor(padfX[ind1]+0.5);
		
                    if  ( (horizontal_x1 >  maxx) ||  (horizontal_x2 <= minx) )
                        continue;

                    /*fill the horizontal segment (separately from the rest)*/
                    pfnScanlineFunc( pCBData, y, horizontal_x1, horizontal_x2 - 1, (dfVariant == NULL)?0:dfVariant[0] );
                    continue;
                }
                else /*skip top horizontal segments (they are already filled in the regular loop)*/
                    continue;

            }

            if(( dy < dy2 ) && (dy >= dy1))
            {
                
                intersect = (dy-dy1) * (dx2-dx1) / (dy2-dy1) + dx1;

                polyInts[ints++] = (int) floor(intersect+0.5);
            }
        }

        /* 
         * It would be more efficient to do this inline, to avoid 
         * a function call for each comparison.
         * NOTE - mloskot: make llCompareInt a functor and use std
         * algorithm and it will be optimized and expanded
         * automatically in compile-time, with modularity preserved.
         */
        qsort(polyInts, ints, sizeof(int), llCompareInt);


        for (i=0; (i < (ints)); i+=2)
        {
            if( polyInts[i] <= maxx && polyInts[i+1] > minx )
            {
                pfnScanlineFunc( pCBData, y, polyInts[i], polyInts[i+1] - 1, (dfVariant == NULL)?0:dfVariant[0] );
            }
        }
    }

    free( polyInts );
}
Ejemplo n.º 23
0
/* print a sparse array distribution */
  void
print_sdist(struct stat_stat_t *stat,	/* stat variable */
    FILE *fd)			/* output stream */
{
  unsigned int i, bcount;
  md_addr_t imax, imin;
  double btotal, bsum, bvar, bavg, bsqsum;
  struct bucket_t *bucket;
  int pf = stat->variant.for_sdist.pf;

  /* count and sum entries */
  bcount = 0; btotal = 0.0; bvar = 0.0; bsqsum = 0.0;
  imax = 0; imin = UINT_MAX;
  for (i=0; i<HTAB_SZ; i++)
  {
    for (bucket = stat->variant.for_sdist.sarr[i];
        bucket != NULL;
        bucket = bucket->next)
    {
      bcount++;
      btotal += bucket->count;
      /* on-line variance computation, tres cool, no!?! */
      bsqsum += ((double)bucket->count * (double)bucket->count);
      bavg = btotal / (double)bcount;
      bvar = (bsqsum - ((double)bcount * bavg * bavg)) / 
        (double)(((bcount - 1) > 0) ? (bcount - 1) : 1);
      if (bucket->index < imin)
        imin = bucket->index;
      if (bucket->index > imax)
        imax = bucket->index;
    }
  }

  /* print header */

  fprintf(fd, "\n");
  fprintf(fd, "%-28s # %s\n", stat->name, stat->desc);
  fprintf(fd, "%s.count = %u\n", stat->name, bcount);
  fprintf(fd, "%s.total = %.0f\n", stat->name, btotal);
  if (bcount > 0)
  {
    myfprintf(fd, "%s.imin = 0x%p\n", stat->name, imin);
    myfprintf(fd, "%s.imax = 0x%p\n", stat->name, imax);
  }
  else
  {
    fprintf(fd, "%s.imin = %d\n", stat->name, -1);
    fprintf(fd, "%s.imax = %d\n", stat->name, -1);
  }
  fprintf(fd, "%s.average = %8.4f\n", stat->name, btotal/bcount);
  fprintf(fd, "%s.std_dev = %8.4f\n", stat->name, sqrt(bvar));
  fprintf(fd, "%s.overflows = 0\n", stat->name);

  fprintf(fd, "# pdf == prob dist fn, cdf == cumulative dist fn\n");
  fprintf(fd, "# %14s ", "index");
  if (pf & PF_COUNT)
    fprintf(fd, "%10s ", "count");
  if (pf & PF_PDF)
    fprintf(fd, "%6s ", "pdf");
  if (pf & PF_CDF)
    fprintf(fd, "%6s ", "cdf");
  fprintf(fd, "\n");

  fprintf(fd, "%s.start_dist\n", stat->name);

  if (bcount > 0)
  {
    unsigned int bindex;
    struct bucket_t **barr;

    /* collect all buckets */
    barr = (struct bucket_t **)calloc(bcount, sizeof(struct bucket_t *));
    if (!barr)
      fatal("out of virtual memory");
    for (bindex=0,i=0; i<HTAB_SZ; i++)
    {
      for (bucket = stat->variant.for_sdist.sarr[i];
          bucket != NULL;
          bucket = bucket->next)
      {
        barr[bindex++] = bucket;
      }
    }

    /* sort the array by index */
    qsort(barr, bcount, sizeof(struct bucket_t *), (int (*)(const void*, const void*))compare_fn);

    /* print the array */
    bsum = 0.0;
    for (i=0; i<bcount; i++)
    {
      bsum += (double)barr[i]->count;
      if (stat->variant.for_sdist.print_fn)
      {
        stat->variant.for_sdist.print_fn(stat,
            barr[i]->index,
            barr[i]->count,
            bsum,
            btotal);
      }
      else
      {
        if (stat->format == NULL)
        {
          myfprintf(fd, "0x%p ", barr[i]->index);
          if (pf & PF_COUNT)
            fprintf(fd, "%10u ", barr[i]->count);
          if (pf & PF_PDF)
            fprintf(fd, "%6.2f ",
                (double)barr[i]->count/MAX(btotal, 1.0) * 100.0);
          if (pf & PF_CDF)
            fprintf(fd, "%6.2f ", bsum/MAX(btotal, 1.0) * 100.0);
        }
        else
        {
          if (pf == (PF_COUNT|PF_PDF|PF_CDF))
          {
            myfprintf(fd, stat->format,
                barr[i]->index, barr[i]->count,
                (double)barr[i]->count/MAX(btotal, 1.0)*100.0,
                bsum/MAX(btotal, 1.0) * 100.0);
          }
          else if (pf == (PF_COUNT|PF_PDF))
          {
            myfprintf(fd, stat->format,
                barr[i]->index, barr[i]->count,
                (double)barr[i]->count/MAX(btotal, 1.0)*100.0);
          }
          else if (pf == PF_COUNT)
          {
            myfprintf(fd, stat->format,
                barr[i]->index, barr[i]->count);
          }
          else
            fatal("distribution format not yet implemented");
        }
        fprintf(fd, "\n");
      }
    }

    /* all done, release bucket pointer array */
    free(barr);
  }

//  fprintf(fd, "%s.end_dist\n", stat->name);
}
Ejemplo n.º 24
0
int RCCE_init(int *argc, char ***argv)
{
    int ue;
    void *nothing = NULL;

    assert(*argc >= 3);

    setup_routes(*argc, *argv);

    // save pointer to executable name for later insertion into the argument list
    char *executable_name = (*argv)[0];

    RCCE_NP        = atoi(*(++(*argv)));
    RC_REFCLOCKGHZ = atof(*(++(*argv)));

    if(RC_REFCLOCKGHZ == 0) {
        printf("Barrelfish RCCE extension: Computing reference clock GHz automatically...\n");
        uint64_t tscperms;
        errval_t err = sys_debug_get_tsc_per_ms(&tscperms);
        assert(err_is_ok(err));
        RC_REFCLOCKGHZ = ((double)tscperms) / 1000000.0;
        printf("Reference clock computed to be %.2g\n", RC_REFCLOCKGHZ);
    }

    // put the participating core ids (unsorted) into an array
    for (ue=0; ue<RCCE_NP; ue++) {
        RC_COREID[ue] = atoi(*(++(*argv)));
    }

    // make sure executable name is as expected
    (*argv)[0] = executable_name;

    RC_MY_COREID = MYCOREID();

    // adjust apparent number of command line arguments, so it will appear to main
    // program that number of UEs, clock frequency, and core ID list were not on
    // command line
    *argc -= RCCE_NP+2;

    // sort array of participating phyical core IDs to determine their ranks
    qsort((char *)RC_COREID, RCCE_NP, sizeof(int), id_compare);

    // determine rank of calling core
    for (ue=0; ue<RCCE_NP; ue++) {
        if (RC_COREID[ue] == RC_MY_COREID) RCCE_IAM = ue;
    }

    // leave in one reassuring debug print
    printf("My rank is %d, physical core ID is %d\n", RCCE_IAM, RC_MY_COREID);
    if (RCCE_IAM<0) {
        return(RCCE_ERROR_CORE_NOT_IN_HOSTFILE);
    }

    // create global communicator (equivalent of MPI_COMM_WORLD); this will also allocate
    // the two synchronization flags associated with the global barrier
    RCCE_comm_split(RCCE_global_color, nothing, &RCCE_COMM_WORLD);

#ifdef MEASURE_TIME
    measure_start = RCCE_wtime();
    measure_rcce_time = 0.0;
#endif

#ifdef MEASURE_DATA
    memset(measure_rcce_data, 0, sizeof(measure_rcce_data));
#endif

    return (RCCE_SUCCESS);
}
Ejemplo n.º 25
0
void sinterGenericCommand(redisClient *c, robj **setkeys, unsigned long setnum, robj *dstkey) {
    robj **sets = zmalloc(sizeof(robj*)*setnum);
    setTypeIterator *si;
    robj *eleobj, *dstset = NULL;
    int64_t intobj;
    void *replylen = NULL;
    unsigned long j, cardinality = 0;
    int encoding;

    for (j = 0; j < setnum; j++) {
        robj *setobj = dstkey ?
            lookupKeyWrite(c->db,setkeys[j]) :
            lookupKeyRead(c->db,setkeys[j]);
        if (!setobj) {
            zfree(sets);
            if (dstkey) {
                if (dbDelete(c->db,dstkey)) {
                    signalModifiedKey(c->db,dstkey);
                    server.dirty++;
                }
                addReply(c,shared.czero);
            } else {
                addReply(c,shared.emptymultibulk);
            }
            return;
        }
        if (checkType(c,setobj,REDIS_SET)) {
            zfree(sets);
            return;
        }
        sets[j] = setobj;
    }
    /* Sort sets from the smallest to largest, this will improve our
     * algorithm's performance */
    qsort(sets,setnum,sizeof(robj*),qsortCompareSetsByCardinality);

    /* The first thing we should output is the total number of elements...
     * since this is a multi-bulk write, but at this stage we don't know
     * the intersection set size, so we use a trick, append an empty object
     * to the output list and save the pointer to later modify it with the
     * right length */
    if (!dstkey) {
        replylen = addDeferredMultiBulkLength(c);
    } else {
        /* If we have a target key where to store the resulting set
         * create this key with an empty set inside */
        dstset = createIntsetObject();
    }

    /* Iterate all the elements of the first (smallest) set, and test
     * the element against all the other sets, if at least one set does
     * not include the element it is discarded */
    si = setTypeInitIterator(sets[0]);
    while((encoding = setTypeNext(si,&eleobj,&intobj)) != -1) {
        for (j = 1; j < setnum; j++) {
            if (sets[j] == sets[0]) continue;
            if (encoding == REDIS_ENCODING_INTSET) {
                /* intset with intset is simple... and fast */
                if (sets[j]->encoding == REDIS_ENCODING_INTSET &&
                    !intsetFind((intset*)sets[j]->ptr,intobj))
                {
                    break;
                /* in order to compare an integer with an object we
                 * have to use the generic function, creating an object
                 * for this */
                } else if (sets[j]->encoding == REDIS_ENCODING_HT) {
                    eleobj = createStringObjectFromLongLong(intobj);
                    if (!setTypeIsMember(sets[j],eleobj)) {
                        decrRefCount(eleobj);
                        break;
                    }
                    decrRefCount(eleobj);
                }
            } else if (encoding == REDIS_ENCODING_HT) {
                /* Optimization... if the source object is integer
                 * encoded AND the target set is an intset, we can get
                 * a much faster path. */
                if (eleobj->encoding == REDIS_ENCODING_INT &&
                    sets[j]->encoding == REDIS_ENCODING_INTSET &&
                    !intsetFind((intset*)sets[j]->ptr,(long)eleobj->ptr))
                {
                    break;
                /* else... object to object check is easy as we use the
                 * type agnostic API here. */
                } else if (!setTypeIsMember(sets[j],eleobj)) {
                    break;
                }
            }
        }

        /* Only take action when all sets contain the member */
        if (j == setnum) {
            if (!dstkey) {
                if (encoding == REDIS_ENCODING_HT)
                    addReplyBulk(c,eleobj);
                else
                    addReplyBulkLongLong(c,intobj);
                cardinality++;
            } else {
                if (encoding == REDIS_ENCODING_INTSET) {
                    eleobj = createStringObjectFromLongLong(intobj);
                    setTypeAdd(dstset,eleobj);
                    decrRefCount(eleobj);
                } else {
                    setTypeAdd(dstset,eleobj);
                }
            }
        }
    }
    setTypeReleaseIterator(si);

    if (dstkey) {
        /* Store the resulting set into the target, if the intersection
         * is not an empty set. */
        int deleted = dbDelete(c->db,dstkey);
        if (setTypeSize(dstset) > 0) {
            dbAdd(c->db,dstkey,dstset);
            addReplyLongLong(c,setTypeSize(dstset));
            notifyKeyspaceEvent(REDIS_NOTIFY_SET,"sinterstore",
                dstkey,c->db->id);
        } else {
            decrRefCount(dstset);
            addReply(c,shared.czero);
            if (deleted)
                notifyKeyspaceEvent(REDIS_NOTIFY_GENERIC,"del",
                    dstkey,c->db->id);
        }
        signalModifiedKey(c->db,dstkey);
        server.dirty++;
    } else {
        setDeferredMultiBulkLength(c,replylen,cardinality);
    }
    zfree(sets);
}
Ejemplo n.º 26
0
int
ccnl_http_status(struct ccnl_relay_s *ccnl, struct ccnl_http_s *http)
{
    static char txt[64000];
    char *hdr =
	"HTTP/1.1 200 OK\n\r"
	"Content-Type: text/html; charset=utf-8\n\r"
        "Connection: close\n\r\n\r", *cp;
    int len = strlen(hdr), i, j, cnt;
    time_t t;
    struct utsname uts;
    struct ccnl_face_s *f;
    struct ccnl_forward_s *fwd;
    struct ccnl_interest_s *ipt;
    struct ccnl_buf_s *bpt;

    strcpy(txt, hdr);
    len += sprintf(txt+len,
		   "<html><head><title>ccn-lite-relay status</title>\n"
		   "<style type=\"text/css\">\n"
		   "body {font-family: sans-serif;}\n"
		   "</style>\n"
		   "</head><body>\n");
    len += sprintf(txt+len, "\n<table borders=0>\n<tr><td>"
		   "<a href=\"\">[refresh]</a>&nbsp;&nbsp;<td>"
		   "ccn-lite-relay Status Page &nbsp;&nbsp;");
    uname(&uts);
    len += sprintf(txt+len, "node <strong>%s (%d)</strong>\n",
		   uts.nodename, getpid());
    t = time(NULL);
    cp = ctime(&t);
    cp[strlen(cp)-1] = 0;
    len += sprintf(txt+len, "<tr><td><td><font size=-1>%s &nbsp;&nbsp;", cp);
    cp = ctime(&ccnl->startup_time);
    cp[strlen(cp)-1] = 0;
    len += sprintf(txt+len, " (started %s)</font>\n</table>\n", cp);

    len += sprintf(txt+len, "\n<p><table borders=0 width=100%% bgcolor=#e0e0ff>"
		   "<tr><td><em>Forwarding Table</em></table><ul>\n");
    for (fwd = ccnl->fib, cnt = 0; fwd; fwd = fwd->next, cnt++);
    if (cnt > 0) {
	struct ccnl_forward_s **fwda;
	fwda = (struct ccnl_forward_s**) ccnl_malloc(cnt * sizeof(fwd));
	for (fwd = ccnl->fib, i = 0; fwd; fwd = fwd->next, i++)
	    fwda[i] = fwd;
	qsort(fwda, cnt, sizeof(fwd), ccnl_cmpfib);
	for (i = 0; i < cnt; i++) {
	    char fname[10];
	    sprintf(fname, "f%d", fwda[i]->face->faceid);
	    len += sprintf(txt+len,
			   "<li>via %4s: <font face=courier>%s</font>\n",
			   fname, ccnl_prefix_to_path(fwda[i]->prefix));
	}
	ccnl_free(fwda);
    }
    len += sprintf(txt+len, "</ul>\n");

    len += sprintf(txt+len, "\n<p><table borders=0 width=100%% bgcolor=#e0e0ff>"
		   "<tr><td><em>Faces</em></table><ul>\n");
    for (f = ccnl->faces, cnt = 0; f; f = f->next, cnt++);
    if (cnt > 0) {
	struct ccnl_face_s **fa;
	fa = (struct ccnl_face_s**) ccnl_malloc(cnt * sizeof(f));
	for (f = ccnl->faces, i = 0; f; f = f->next, i++)
	    fa[i] = f;
	qsort(fa, cnt, sizeof(f), ccnl_cmpfaceid);
	for (i = 0; i < cnt; i++) {
	    len += sprintf(txt+len,
			   "<li><strong>f%d</strong> (via i%d) &nbsp;"
			   "peer=<font face=courier>%s</font> &nbsp;ttl=",
			   fa[i]->faceid, fa[i]->ifndx,
			   ccnl_addr2ascii(&(fa[i]->peer)));
	    if (fa[i]->flags & CCNL_FACE_FLAGS_STATIC)
		len += sprintf(txt+len, "static");
	    else
		len += sprintf(txt+len, "%.1fsec",
			fa[i]->last_used + CCNL_FACE_TIMEOUT - CCNL_NOW());
	    for (j = 0, bpt = fa[i]->outq; bpt; bpt = bpt->next, j++);
	    len += sprintf(txt+len, " &nbsp;qlen=%d\n", j);
	}
	ccnl_free(fa);
    }
    len += sprintf(txt+len, "</ul>\n");

    len += sprintf(txt+len, "\n<p><table borders=0 width=100%% bgcolor=#e0e0ff>"
		   "<tr><td><em>Interfaces</em></table><ul>\n");
    for (i = 0; i < ccnl->ifcount; i++) {
	len += sprintf(txt+len, "<li><strong>i%d</strong>&nbsp;&nbsp;"
		       "addr=<font face=courier>%s</font>&nbsp;&nbsp;"
		       "qlen=%d/%d\n",
		       i, ccnl_addr2ascii(&ccnl->ifs[i].addr),
		       ccnl->ifs[i].qlen, CCNL_MAX_IF_QLEN);
    }
    len += sprintf(txt+len, "</ul>\n");

    len += sprintf(txt+len, "\n<p><table borders=0 width=100%% bgcolor=#e0e0ff>"
		   "<tr><td><em>Misc stats</em></table><ul>\n");
    for (cnt = 0, bpt = ccnl->nonces; bpt; bpt = bpt->next, cnt++);
    len += sprintf(txt+len, "<li>Nonces: %d\n", cnt);
    for (cnt = 0, ipt = ccnl->pit; ipt; ipt = ipt->next, cnt++);
    len += sprintf(txt+len, "<li>Pending interests: %d\n", cnt);
    len += sprintf(txt+len, "<li>Content chunks: %d (max=%d)\n",
		   ccnl->contentcnt, ccnl->max_cache_entries);
    len += sprintf(txt+len, "</ul>\n");

    len += sprintf(txt+len, "\n<p><table borders=0 width=100%% bgcolor=#e0e0ff>"
		   "<tr><td><em>Config</em></table><table borders=0>\n");
    len += sprintf(txt+len, "<tr><td>content.timeout:"
		   "<td align=right> %d<td>\n", CCNL_CONTENT_TIMEOUT);
    len += sprintf(txt+len, "<tr><td>face.timeout:"
		   "<td align=right> %d<td>\n", CCNL_FACE_TIMEOUT);
    len += sprintf(txt+len, "<tr><td>interest.maxretransmit:"
		   "<td align=right> %d<td>\n", CCNL_MAX_INTEREST_RETRANSMIT);
    len += sprintf(txt+len, "<tr><td>interest.timeout:"
		   "<td align=right> %d<td>\n", CCNL_INTEREST_TIMEOUT);
    len += sprintf(txt+len, "<tr><td>nonces.max:"
		   "<td align=right> %d<td>\n", CCNL_MAX_NONCES);

    len += sprintf(txt+len, "<tr><td>compile.featureset:<td><td>");
#ifdef USE_DEBUG
    len +=  sprintf(txt+len, " DEBUG");
#endif
#ifdef USE_DEBUG_MALLOC
    len +=  sprintf(txt+len, " DEBUG_MALLOC");
#endif
#ifdef USE_FRAG
    len +=  sprintf(txt+len, " FRAGS");
#endif
#ifdef USE_ETHERNET
    len +=  sprintf(txt+len, " ETHERNET");
#endif
#ifdef USE_HTTP_STATUS
    len +=  sprintf(txt+len, " HTTP_STATUS");
#endif
#ifdef USE_MGMT
    len +=  sprintf(txt+len, " MGMT");
#endif
#ifdef USE_SCHEDULER
    len +=  sprintf(txt+len, " SCHEDULER");
#endif
#ifdef USE_UNIXSOCKET
    len +=  sprintf(txt+len, " UNIXSOCKET");
#endif
    len +=  sprintf(txt+len, "\n");
    len += sprintf(txt+len, "<tr><td>compile.time:"
		   "<td><td>%s %s\n", __DATE__, __TIME__);
    len += sprintf(txt+len, "<tr><td>compile.ccnl_core_version:"
		   "<td><td>%s\n", CCNL_VERSION);
    len += sprintf(txt+len, "</table>\n");

    len += sprintf(txt+len, "\n<p><hr></body></html>\n");

    http->out = (unsigned char*) txt;
    http->outoffs = 0;
    http->outlen = len;

    return 0;
}
Ejemplo n.º 27
0
void sortIndicesHost (int * cardinalities, int * indices, int size){
	int i;
	for(i=0;i<size;i++) indices[i] = i;
	keys = cardinalities;
	qsort(indices,size,sizeof(int),compare);
}
Ejemplo n.º 28
0
Archivo: help.c Proyecto: Ghadeerk/git
const char *help_unknown_cmd(const char *cmd)
{
	int i, n, best_similarity = 0;
	struct cmdnames main_cmds, other_cmds;

	memset(&main_cmds, 0, sizeof(main_cmds));
	memset(&other_cmds, 0, sizeof(other_cmds));
	memset(&aliases, 0, sizeof(aliases));

	git_config(git_unknown_cmd_config, NULL);

	load_command_list("git-", &main_cmds, &other_cmds);

	add_cmd_list(&main_cmds, &aliases);
	add_cmd_list(&main_cmds, &other_cmds);
	qsort(main_cmds.names, main_cmds.cnt,
	      sizeof(main_cmds.names), cmdname_compare);
	uniq(&main_cmds);

	/* This abuses cmdname->len for levenshtein distance */
	for (i = 0, n = 0; i < main_cmds.cnt; i++) {
		int cmp = 0; /* avoid compiler stupidity */
		const char *candidate = main_cmds.names[i]->name;

		/*
		 * An exact match means we have the command, but
		 * for some reason exec'ing it gave us ENOENT; probably
		 * it's a bad interpreter in the #! line.
		 */
		if (!strcmp(candidate, cmd))
			die(_(bad_interpreter_advice), cmd, cmd);

		/* Does the candidate appear in common_cmds list? */
		while (n < ARRAY_SIZE(common_cmds) &&
		       (cmp = strcmp(common_cmds[n].name, candidate)) < 0)
			n++;
		if ((n < ARRAY_SIZE(common_cmds)) && !cmp) {
			/* Yes, this is one of the common commands */
			n++; /* use the entry from common_cmds[] */
			if (!prefixcmp(candidate, cmd)) {
				/* Give prefix match a very good score */
				main_cmds.names[i]->len = 0;
				continue;
			}
		}

		main_cmds.names[i]->len =
			levenshtein(cmd, candidate, 0, 2, 1, 3) + 1;
	}

	qsort(main_cmds.names, main_cmds.cnt,
	      sizeof(*main_cmds.names), levenshtein_compare);

	if (!main_cmds.cnt)
		die(_("Uh oh. Your system reports no Git commands at all."));

	/* skip and count prefix matches */
	for (n = 0; n < main_cmds.cnt && !main_cmds.names[n]->len; n++)
		; /* still counting */

	if (main_cmds.cnt <= n) {
		/* prefix matches with everything? that is too ambiguous */
		best_similarity = SIMILARITY_FLOOR + 1;
	} else {
		/* count all the most similar ones */
		for (best_similarity = main_cmds.names[n++]->len;
		     (n < main_cmds.cnt &&
		      best_similarity == main_cmds.names[n]->len);
		     n++)
			; /* still counting */
	}
	if (autocorrect && n == 1 && SIMILAR_ENOUGH(best_similarity)) {
		const char *assumed = main_cmds.names[0]->name;
		main_cmds.names[0] = NULL;
		clean_cmdnames(&main_cmds);
		fprintf_ln(stderr,
			   _("WARNING: You called a Git command named '%s', "
			     "which does not exist.\n"
			     "Continuing under the assumption that you meant '%s'"),
			cmd, assumed);
		if (autocorrect > 0) {
			fprintf_ln(stderr, _("in %0.1f seconds automatically..."),
				(float)autocorrect/10.0);
			poll(NULL, 0, autocorrect * 100);
		}
		return assumed;
	}

	fprintf_ln(stderr, _("git: '%s' is not a git command. See 'git --help'."), cmd);

	if (SIMILAR_ENOUGH(best_similarity)) {
		fprintf_ln(stderr,
			   Q_("\nDid you mean this?",
			      "\nDid you mean one of these?",
			   n));

		for (i = 0; i < n; i++)
			fprintf(stderr, "\t%s\n", main_cmds.names[i]->name);
	}

	exit(1);
}
Ejemplo n.º 29
0
static void UpdateController(void)
{
	struct control_cache_t *cache;
	ioport_constructor last_ipt = NULL;
	BOOL flags[CONTROLLER_MAX];
	int nGames = GetNumGames();
	int b = 0;
	int p = 0;
	int i;

	cache = (control_cache_t *)malloc(sizeof (*cache) * nGames);
	if (cache == NULL)
		return;

	for (i = 0; i < nGames; i++)
	{
		cache[i].ipt = driver_list::driver(i).ipt;
		cache[i].num = i;
	}
	qsort(cache, nGames, sizeof (*cache), cmp_ipt);

	for (i = 0; i < nGames; i++)
	{
		struct DriversInfo *gameinfo = &drivers_info[cache[i].num];

		if (!cache[i].ipt)
			continue;

		if (cache[i].ipt != last_ipt)
		{
			const input_port_config *port;
			ioport_list portlist;
			astring errors;

			int w = CONTROLLER_JOY8WAY;
			BOOL lr = FALSE;
			BOOL ud = FALSE;
			BOOL dual = FALSE;

			last_ipt = cache[i].ipt;
			memset(flags, 0, sizeof flags);
			b = 0;
			p = 0;

			machine_config config(driver_list::driver(i), MameUIGlobal());
			device_iterator iter(config.root_device());
			for (device_t *device = iter.first(); device != NULL; device = iter.next())
				if (device->input_ports()!=NULL)
					input_port_list_init(*device, portlist, errors);

			for (port = portlist.first(); port != NULL; port = port->next())
			{
				const input_field_config *field;
				for (field = port->first_field(); field != NULL; field = field->next())
				{
				    int n;

				    if (p < field->player + 1)
					    p = field->player + 1;

				    n = field->type - IPT_BUTTON1 + 1;
				    if (n >= 1 && n <= MAX_NORMAL_BUTTONS && n > b)
				    {
					    b = n;
					    continue;
				    }

				    switch (field->type)
				    {
				    case IPT_JOYSTICKRIGHT_LEFT:
				    case IPT_JOYSTICKRIGHT_RIGHT:
				    case IPT_JOYSTICKLEFT_LEFT:
				    case IPT_JOYSTICKLEFT_RIGHT:
					    dual = TRUE;

				    case IPT_JOYSTICK_LEFT:
				    case IPT_JOYSTICK_RIGHT:
					    lr = TRUE;

					    if (field->way == 4)
						    w = CONTROLLER_JOY4WAY;
					    else if (field->way == 16)
						    w = CONTROLLER_JOY16WAY;
					    break;

				    case IPT_JOYSTICKRIGHT_UP:
				    case IPT_JOYSTICKRIGHT_DOWN:
				    case IPT_JOYSTICKLEFT_UP:
				    case IPT_JOYSTICKLEFT_DOWN:
					    dual = TRUE;

				    case IPT_JOYSTICK_UP:
				    case IPT_JOYSTICK_DOWN:
					    ud = TRUE;

					    if (field->way == 4)
						    w = CONTROLLER_JOY4WAY;
					    else if (field->way == 16)
						    w = CONTROLLER_JOY16WAY;
					    break;

				    case IPT_PADDLE:
					    flags[CONTROLLER_PADDLE] = TRUE;
					    break;

				    case IPT_DIAL:
					    flags[CONTROLLER_DIAL] = TRUE;
					    break;

				    case IPT_TRACKBALL_X:
				    case IPT_TRACKBALL_Y:
					    flags[CONTROLLER_TRACKBALL] = TRUE;
					    break;

				    case IPT_AD_STICK_X:
				    case IPT_AD_STICK_Y:
					    flags[CONTROLLER_ADSTICK] = TRUE;
					    break;

				    case IPT_LIGHTGUN_X:
				    case IPT_LIGHTGUN_Y:
					    flags[CONTROLLER_LIGHTGUN] = TRUE;
					    break;
				    case IPT_PEDAL:
					    flags[CONTROLLER_PEDAL] = TRUE;
					    break;
				    }
				}
			}
			//input_port_list_deinit(&portlist);

			if (lr || ud)
			{
				if (lr && !ud)
					w = CONTROLLER_JOY2WAY;
				else if (!lr && ud)
					w = CONTROLLER_VJOY2WAY;

				if (dual)
					w += CONTROLLER_DOUBLEJOY2WAY - CONTROLLER_JOY2WAY;

				flags[w] = TRUE;
			}
		}

		gameinfo->numPlayers = p;
		gameinfo->numButtons = b;

		memcpy(gameinfo->usesController, flags, sizeof gameinfo->usesController);
	}

	free(cache);
}
Ejemplo n.º 30
0
static void *
format_parse (const char *format, bool translated, char **invalid_reason)
{
  unsigned int directives;
  unsigned int numbered_arg_count;
  unsigned int allocated;
  struct numbered_arg *numbered;
  unsigned int unnumbered_arg_count;
  struct spec *result;

  directives = 0;
  numbered_arg_count = 0;
  allocated = 0;
  numbered = NULL;
  unnumbered_arg_count = 0;

  for (; *format != '\0';)
    if (*format++ == '%')
      {
	/* A directive.  */
	directives++;

	if (*format != '%')
	  {
	    /* A complex directive.  */
	    unsigned int number;
	    enum format_arg_type type;

	    number = ++unnumbered_arg_count;
	    if (isdigit (*format))
	      {
		const char *f = format;
		unsigned int m = 0;

		do
		  {
		    m = 10 * m + (*f - '0');
		    f++;
		  }
		while (isdigit (*f));

		if (*f == '$')
		  {
		    if (m == 0)
		      {
			*invalid_reason = INVALID_ARGNO_0 (directives);
			goto bad_format;
		      }
		    number = m;
		    format = ++f;
		    --unnumbered_arg_count;
		  }
	      }

	    /* Parse flags.  */
	    for (;;)
	      {
		if (*format == '0' || *format == '-' || *format == ' ')
		  format++;
		else if (*format == '\'')
		  {
		    format++;
		    if (*format == '\0')
		      {
			*invalid_reason = INVALID_UNTERMINATED_DIRECTIVE ();
			goto bad_format;
		      }
		    format++;
		  }
		else
		  break;
	      }

	    /* Parse width.  */
	    if (isdigit (*format))
	      {
		do
		  format++;
		while (isdigit (*format));
	      }

	    /* Parse precision.  */
	    if (*format == '.')
	      {
		format++;

		if (isdigit (*format))
		  {
		    do
		      format++;
		    while (isdigit (*format));
		  }
		else
		  --format;	/* will jump to bad_format */
	      }

	    /* Parse size.  */
	    if (*format == 'l')
	      format++;

	    switch (*format)
	      {
	      case 'b': case 'd': case 'u': case 'o': case 'x': case 'X':
		type = FAT_INTEGER;
		break;
	      case 'e': case 'f':
		type = FAT_FLOAT;
		break;
	      case 'c':
		type = FAT_CHARACTER;
		break;
	      case 's':
		type = FAT_STRING;
		break;
	      default:
		*invalid_reason =
		  (*format == '\0'
		   ? INVALID_UNTERMINATED_DIRECTIVE ()
		   : INVALID_CONVERSION_SPECIFIER (directives, *format));
		goto bad_format;
	      }

	    if (allocated == numbered_arg_count)
	      {
		allocated = 2 * allocated + 1;
		numbered = (struct numbered_arg *) xrealloc (numbered, allocated * sizeof (struct numbered_arg));
	      }
	    numbered[numbered_arg_count].number = number;
	    numbered[numbered_arg_count].type = type;
	    numbered_arg_count++;
	  }

	format++;
      }

  /* Sort the numbered argument array, and eliminate duplicates.  */
  if (numbered_arg_count > 1)
    {
      unsigned int i, j;
      bool err;

      qsort (numbered, numbered_arg_count,
	     sizeof (struct numbered_arg), numbered_arg_compare);

      /* Remove duplicates: Copy from i to j, keeping 0 <= j <= i.  */
      err = false;
      for (i = j = 0; i < numbered_arg_count; i++)
	if (j > 0 && numbered[i].number == numbered[j-1].number)
	  {
	    enum format_arg_type type1 = numbered[i].type;
	    enum format_arg_type type2 = numbered[j-1].type;
	    enum format_arg_type type_both;

	    if (type1 == type2)
	      type_both = type1;
	    else
	      {
		/* Incompatible types.  */
		type_both = type1;
		if (!err)
		  *invalid_reason =
		    INVALID_INCOMPATIBLE_ARG_TYPES (numbered[i].number);
		err = true;
	      }

	    numbered[j-1].type = type_both;
	  }
	else
	  {
	    if (j < i)
	      {
		numbered[j].number = numbered[i].number;
		numbered[j].type = numbered[i].type;
	      }
	    j++;
	  }
      numbered_arg_count = j;
      if (err)
	/* *invalid_reason has already been set above.  */
	goto bad_format;
    }

  result = (struct spec *) xmalloc (sizeof (struct spec));
  result->directives = directives;
  result->numbered_arg_count = numbered_arg_count;
  result->allocated = allocated;
  result->numbered = numbered;
  return result;

 bad_format:
  if (numbered != NULL)
    free (numbered);
  return NULL;
}