Example #1
0
int main (int argc, char **argv) {
  long int *a0 = NULL, *a1 = NULL, *a2 = NULL;
  long int n = -1;

  clock_t start, finish;
  double elapsed_time;

  printf("Enter the problem size: ");
  scanf("%ld", &n); // Read the size of the list from the command line.

  if (n < 0) {
    printf("No problem size specified. Say \"bye-bye\" to the nice program!\n");
    return (16);
  }

  //**********************************
  //* Initialization. 
  //*
  a0 = (long int*) malloc(n*sizeof(long int));
  a1 = (long int*) malloc(n*sizeof(long int));
  a2 = (long int*) malloc(n*sizeof(long int));

  srand(42);
  for (long int i = 0; i < n; i++) {
    a0[i] = rand() % 99;  // Generate pseudorandom long ints between 0 and 99.
  }

  // Use the standard library qsort() to check our work.
  memcpy(a1, a0, n*sizeof(long int)); // Copy the array a0 to a1.

  start = clock();
  qsort(a1, n, sizeof(long int), &long_int_compare); 
  finish = clock();
  elapsed_time = (double) (finish-start) / CLOCKS_PER_SEC;
  printf("------------------------------\n");
  printf("Library quicksort:\n");
  printf("  Elapsed time: %f\n", elapsed_time);
  printf("------------------------------\n");
  
  // Apply insertion sort.
  memcpy(a2, a0, n*sizeof(long int));

  start = clock();
  iterative_mergesort(a2, n);
  finish = clock();
  elapsed_time = (double) (finish-start) / CLOCKS_PER_SEC;
  printf("recursive_mergesort:\n");
  printf("  Elapsed time: %f\n", elapsed_time);
  printf("  Correctly sorted? %s\n", are_identical(a1, a2, n) ? "yes" : "no");

  // Clean up: free allocated memory.
  free(a0); free(a1); free(a2);

  return 0;
}
Example #2
0
File: main.cpp Project: CCJY/coliru
int main()
{
    std::vector<uint8_t> test = { 'a', 'b', 'c' };
    
    
    // test non-owning range
    ByteRange alias(test.data(), test.data() + test.size(), ByteRange::Refer{});    
    auto alias_copy = alias;
    VERIFY(!alias.owns() && !alias_copy.owns());
    VERIFY(are_equal(alias, alias_copy) && are_identical(alias, alias_copy));
    
    
    // test owning range
    ByteRange owner(test.data(), test.data() + test.size(), ByteRange::Copy{});
    auto owner_copy = owner;
    VERIFY(owner.owns() && owner_copy.owns());
    VERIFY(are_equal(owner, owner_copy) && !are_identical(owner, owner_copy));
    
    
    // test cross-type assignments
    VERIFY((alias = owner, alias.owns()));
    VERIFY((owner = alias_copy, !owner.owns()));
}
Example #3
0
int
main(int argc, char **argv)
{
    domainname      d1, d2;
    char           *data1, *data2, *data3, *data4;
    if (argc < 3) {
        printf("Usage: %s domainname domainname\n", argv[0]);
        return 1;
    }
    data1 = (char *) malloc(MAX_LENGTH);
    data2 = (char *) malloc(MAX_LENGTH);
    data3 = (char *) malloc(MAX_LENGTH);
    data4 = (char *) malloc(MAX_LENGTH);
    d1 = text_to_domain(argv[1]);
    if (!VALID(d1)) {
        printf("Invalid domain name \"%s\"\n", argv[1]);
        return 1;
    }
    d2 = text_to_domain(argv[2]);
    if (!VALID(d2)) {
        printf("Invalid domain name \"%s\"\n", argv[2]);
        return 1;
    }
    printf
        ("The canonical version of \"%s\" is \"%s\"; its TLD is \"%s\".\n\t**s registered domain is \"%s\", it has %i labels\n",
         orig_domain_name(data1, d1), domain_name(data2, d1), tld(data3, d1),
         registered_domain_name(data4, d1), (int) nlabels(d1));
    if (are_equal(d1, d2)) {
        printf("%s and %s are equivalent domain names\n", argv[1], argv[2]);
    } else {
        printf("%s and %s are NOT equivalent domain names\n", argv[1], argv[2]);
    }
    if (are_identical(d1, d2)) {
        printf("%s and %s are absolutely identical\n", argv[1], argv[2]);
    } else {
        printf("%s and %s are NOT absolutely identical\n", argv[1], argv[2]);
    }
    free(data1);
    free(data2);
    free(data3);
    free(data4);
    return 0;
}
Example #4
0
/*
 * Push the current filename, link and line number onto the history list.
 */
int LYpush(DocInfo *doc, int force_push)
{
    /*
     * Don't push NULL file names.
     */
    if (*doc->address == '\0')
	return 0;

    /*
     * Check whether this is a document we don't push unless forced.  - FM
     */
    if (!force_push) {
	/*
	 * Don't push the history, printer, or download lists.
	 */
	if (!LYwouldPush(doc->title, doc->address)) {
	    if (!LYforce_no_cache)
		LYoverride_no_cache = TRUE;
	    return 0;
	}
    }

    /*
     * If file is identical to one before it, don't push it.
     * But do not duplicate it if there is only one on the stack,
     * note that HDOC() starts from 0, so nhist should be > 0.
     */
    if (nhist >= 1 && are_identical(&(history[nhist - 1]), doc)) {
	if (HDOC(nhist - 1).internal_link == doc->internal_link) {
	    /* But it is nice to have the last position remembered!
	       - kw */
	    HDOC(nhist - 1).link = doc->link;
	    HDOC(nhist - 1).line = doc->line;
	    return 0;
	}
    }

    /*
     * If file is identical to the current document, just move the pointer.
     */
    if (nhist_extra >= 1 && are_identical(&(history[nhist]), doc)) {
	HDOC(nhist).link = doc->link;
	HDOC(nhist).line = doc->line;
	nhist_extra--;
	LYAllocHistory(nhist);
	nhist++;
	trace_history("LYpush: just move the cursor");
	return 1;
    }

    clean_extra_history();
#ifdef LY_FIND_LEAKS
    if (!already_registered_clean_all_history) {
	already_registered_clean_all_history = 1;
	atexit(clean_all_history);
    }
#endif

    /*
     * OK, push it...
     */
    LYAllocHistory(nhist);
    HDOC(nhist).link = doc->link;
    HDOC(nhist).line = doc->line;

    HDOC(nhist).title = NULL;
    LYformTitle(&(HDOC(nhist).title), doc->title);

    HDOC(nhist).address = NULL;
    StrAllocCopy(HDOC(nhist).address, doc->address);

    HDOC(nhist).post_data = NULL;
    BStrCopy(HDOC(nhist).post_data, doc->post_data);

    HDOC(nhist).post_content_type = NULL;
    StrAllocCopy(HDOC(nhist).post_content_type, doc->post_content_type);

    HDOC(nhist).bookmark = NULL;
    StrAllocCopy(HDOC(nhist).bookmark, doc->bookmark);

    HDOC(nhist).isHEAD = doc->isHEAD;
    HDOC(nhist).safe = doc->safe;

    HDOC(nhist).internal_link = FALSE;	/* by default */
    history[nhist].intern_seq_start = -1;	/* by default */
    if (doc->internal_link) {
	/* Now some tricky stuff: if the caller thinks that the doc
	   to push was the result of following an internal
	   (fragment) link, we check whether we believe it.
	   It is only accepted as valid if the immediately preceding
	   item on the history stack is actually the same document
	   except for fragment and location info.  I.e. the Parent
	   Anchors are the same.
	   Also of course this requires that this is not the first
	   history item. - kw */
	if (nhist > 0) {
	    DocAddress WWWDoc;
	    HTParentAnchor *thisparent, *thatparent = NULL;

	    WWWDoc.address = doc->address;
	    WWWDoc.post_data = doc->post_data;
	    WWWDoc.post_content_type = doc->post_content_type;
	    WWWDoc.bookmark = doc->bookmark;
	    WWWDoc.isHEAD = doc->isHEAD;
	    WWWDoc.safe = doc->safe;
	    thisparent =
		HTAnchor_findAddress(&WWWDoc);
	    /* Now find the ParentAnchor for the previous history
	     * item - kw
	     */
	    if (thisparent) {
		/* If the last-pushed item is a LYNXIMGMAP but THIS one
		 * isn't, compare the physical URLs instead. - kw
		 */
		if (isLYNXIMGMAP(HDOC(nhist - 1).address) &&
		    !isLYNXIMGMAP(doc->address)) {
		    WWWDoc.address = HDOC(nhist - 1).address + LEN_LYNXIMGMAP;
		    /*
		     * If THIS item is a LYNXIMGMAP but the last-pushed one
		     * isn't, fake it by using THIS item's address for
		     * thatparent... - kw
		     */
		} else if (isLYNXIMGMAP(doc->address) &&
			   !isLYNXIMGMAP(HDOC(nhist - 1).address)) {
		    char *temp = NULL;

		    StrAllocCopy(temp, STR_LYNXIMGMAP);
		    StrAllocCat(temp, doc->address + LEN_LYNXIMGMAP);
		    WWWDoc.address = temp;
		    WWWDoc.post_content_type = HDOC(nhist - 1).post_content_type;
		    WWWDoc.bookmark = HDOC(nhist - 1).bookmark;
		    WWWDoc.isHEAD = HDOC(nhist - 1).isHEAD;
		    WWWDoc.safe = HDOC(nhist - 1).safe;
		    thatparent =
			HTAnchor_findAddress(&WWWDoc);
		    FREE(temp);
		} else {
		    WWWDoc.address = HDOC(nhist - 1).address;
		}
		if (!thatparent) {	/* if not yet done */
		    WWWDoc.post_data = HDOC(nhist - 1).post_data;
		    WWWDoc.post_content_type = HDOC(nhist - 1).post_content_type;
		    WWWDoc.bookmark = HDOC(nhist - 1).bookmark;
		    WWWDoc.isHEAD = HDOC(nhist - 1).isHEAD;
		    WWWDoc.safe = HDOC(nhist - 1).safe;
		    thatparent =
			HTAnchor_findAddress(&WWWDoc);
		}
		/* In addition to equality of the ParentAnchors, require
		 * that IF we have a HTMainText (i.e., it wasn't just
		 * HTuncache'd by mainloop), THEN it has to be consistent
		 * with what we are trying to push.
		 *
		 * This may be overkill...  - kw
		 */
		if (thatparent == thisparent &&
		    (!HTMainText || HTMainAnchor == thisparent)
		    ) {
		    HDOC(nhist).internal_link = TRUE;
		    history[nhist].intern_seq_start =
			history[nhist - 1].intern_seq_start >= 0 ?
			history[nhist - 1].intern_seq_start : nhist - 1;
		    CTRACE((tfp, "\nLYpush: pushed as internal link, OK\n"));
		}
	    }
	}
	if (!HDOC(nhist).internal_link) {
	    CTRACE((tfp, "\nLYpush: push as internal link requested, %s\n",
		    "but didn't check out!"));
	}
    }
    CTRACE((tfp, "\nLYpush[%d]: address:%s\n        title:%s\n",
	    nhist, doc->address, doc->title));
    nhist++;
    return 1;
}