Refactor all 'vector' functions into common code All the operations on the 'git_index_entry' array and the 'git_tree_entry' array have been refactored into common code in the src/vector.c file. The new vector methods support: - insertion: O(1) (avg) - deletion: O(n) - searching: O(logn) - sorting: O(logn) - r. access: O(1) Signed-off-by: Vicent Marti <tanoku@gmail.com>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905
diff --git a/src/index.c b/src/index.c
index bb8cddd..488a60a 100644
--- a/src/index.c
+++ b/src/index.c
@@ -99,6 +99,23 @@ static int read_tree(git_index *index, const char *buffer, size_t buffer_size);
static git_index_tree *read_tree_internal(const char **, const char *, git_index_tree *);
+int index_srch(const void *key, const void *array_member)
+{
+ const char *filename = (const char *)key;
+ const git_index_entry *entry = *(const git_index_entry **)(array_member);
+
+ return strcmp(filename, entry->path);
+}
+
+int index_cmp(const void *a, const void *b)
+{
+ const git_index_entry *entry_a = *(const git_index_entry **)(a);
+ const git_index_entry *entry_b = *(const git_index_entry **)(b);
+
+ return strcmp(entry_a->path, entry_b->path);
+}
+
+
static int index_initialize(git_index **index_out, git_repository *owner, const char *index_path)
{
git_index *index;
@@ -119,6 +136,8 @@ static int index_initialize(git_index **index_out, git_repository *owner, const
index->repository = owner;
+ git_vector_init(&index->entries, 32, index_cmp, index_srch);
+
/* Check if index file is stored on disk already */
if (gitfo_exists(index->index_file_path) == 0)
index->on_disk = 1;
@@ -146,10 +165,14 @@ void git_index_clear(git_index *index)
assert(index);
- for (i = 0; i < index->entry_count; ++i)
- free(index->entries[i].path);
+ for (i = 0; i < index->entries.length; ++i) {
+ git_index_entry *e;
+ e = git_vector_get(&index->entries, i);
+ free(e->path);
+ free(e);
+ }
- index->entry_count = 0;
+ git_vector_clear(&index->entries);
index->last_modified = 0;
index->sorted = 1;
@@ -163,8 +186,8 @@ void git_index_free(git_index *index)
return;
git_index_clear(index);
- free(index->entries);
- index->entries = NULL;
+ git_vector_free(&index->entries);
+
free(index->index_file_path);
free(index);
}
@@ -241,13 +264,14 @@ int git_index_write(git_index *index)
unsigned int git_index_entrycount(git_index *index)
{
assert(index);
- return index->entry_count;
+ return index->entries.length;
}
git_index_entry *git_index_get(git_index *index, int n)
{
assert(index);
- return (n >= 0 && (unsigned int)n < index->entry_count) ? &index->entries[n] : NULL;
+ git_index__sort(index);
+ return git_vector_get(&index->entries, (unsigned int)n);
}
int git_index_add(git_index *index, const char *rel_path, int stage)
@@ -297,31 +321,15 @@ int git_index_add(git_index *index, const char *rel_path, int stage)
void git_index__sort(git_index *index)
{
- git_index_entry pivot;
- int i, j;
-
- if (index->sorted)
- return;
-
- for (i = 1; i < (int)index->entry_count; ++i) {
-
- memcpy(&pivot, &index->entries[i], sizeof(git_index_entry));
- j = i - 1;
-
- while (j >= 0 && strcmp(pivot.path, index->entries[j].path) < 0) {
- memcpy(&index->entries[j + 1], &index->entries[j], sizeof(git_index_entry));
- j = j - 1;
- }
-
- memcpy(&index->entries[j + 1], &pivot, sizeof(git_index_entry));
+ if (index->sorted == 0) {
+ git_vector_sort(&index->entries);
+ index->sorted = 1;
}
-
- index->sorted = 1;
}
int git_index_insert(git_index *index, const git_index_entry *source_entry)
{
- git_index_entry *offset;
+ git_index_entry *entry;
size_t path_length;
int position;
@@ -330,107 +338,63 @@ int git_index_insert(git_index *index, const git_index_entry *source_entry)
if (source_entry->path == NULL)
return GIT_EMISSINGOBJDATA;
- position = git_index_find(index, source_entry->path);
-
- if (position == GIT_ENOTFOUND) {
-
- /* Resize the entries array */
- if (index->entry_count + 1 > index->entries_size) {
- git_index_entry *new_entries;
- size_t new_size;
-
- new_size = (unsigned int)(index->entries_size * 1.5f);
- if (new_size < 8)
- new_size = 8;
-
- if ((new_entries = git__malloc(new_size * sizeof(git_index_entry))) == NULL)
- return GIT_ENOMEM;
-
- memcpy(new_entries, index->entries, index->entry_count * sizeof(git_index_entry));
- free(index->entries);
-
- index->entries_size = new_size;
- index->entries = new_entries;
- }
-
- offset = &index->entries[index->entry_count];
- index->entry_count++;
- index->sorted = 0;
-
- } else {
- offset = &index->entries[position];
- free(offset->path);
- }
+ entry = git__malloc(sizeof(git_index_entry));
+ if (entry == NULL)
+ return GIT_ENOMEM;
- memcpy(offset, source_entry, sizeof(git_index_entry));
+ memcpy(entry, source_entry, sizeof(git_index_entry));
/* duplicate the path string so we own it */
- offset->path = git__strdup(offset->path);
- if (offset->path == NULL)
+ entry->path = git__strdup(entry->path);
+ if (entry->path == NULL)
return GIT_ENOMEM;
/* make sure that the path length flag is correct */
- path_length = strlen(offset->path);
+ path_length = strlen(entry->path);
- offset->flags &= ~GIT_IDXENTRY_NAMEMASK;
+ entry->flags &= ~GIT_IDXENTRY_NAMEMASK;
if (path_length < GIT_IDXENTRY_NAMEMASK)
- offset->flags |= path_length & GIT_IDXENTRY_NAMEMASK;
+ entry->flags |= path_length & GIT_IDXENTRY_NAMEMASK;
else
- offset->flags |= GIT_IDXENTRY_NAMEMASK;;
+ entry->flags |= GIT_IDXENTRY_NAMEMASK;;
- /* TODO: force the extended index entry flag? */
- assert(offset->path);
-
- return GIT_SUCCESS;
-}
+ /* look if an entry with this path already exists */
+ position = git_index_find(index, source_entry->path);
-int git_index_remove(git_index *index, int position)
-{
- git_index_entry *offset;
- size_t copy_size;
+ /* if no entry exists, add the entry at the end;
+ * the index is no longer sorted */
+ if (position == GIT_ENOTFOUND) {
+ if (git_vector_insert(&index->entries, entry) < 0)
+ return GIT_ENOMEM;
- assert(index);
+ index->sorted = 0;
- if (position < 0 || (unsigned int)position > index->entry_count)
- return GIT_ENOTFOUND;
+ /* if a previous entry exists, replace it */
+ } else {
+ git_index_entry **entry_array = (git_index_entry **)index->entries.contents;
- offset = &index->entries[position];
- index->entry_count--;
- copy_size = (index->entry_count - position) * sizeof(git_index_entry);
+ free(entry_array[position]->path);
+ free(entry_array[position]);
- memcpy(offset, offset + sizeof(git_index_entry), copy_size);
+ entry_array[position] = entry;
+ }
return GIT_SUCCESS;
}
-int git_index_find(git_index *index, const char *path)
+int git_index_remove(git_index *index, int position)
{
- int low = 0, high = index->entry_count;
-
- if (!index->sorted)
- git_index__sort(index);
-
- while (low < high) {
- int mid = (low + high) >> 1;
- int cmp = strcmp(path, index->entries[mid].path);
-
- if (cmp < 0)
- high = mid;
-
- else if (cmp == 0) {
-
- while (mid > 0 && strcmp(path, index->entries[mid - 1].path) == 0)
- mid--;
-
- return mid;
-
- } else
- low = mid + 1;
- }
+ assert(index);
+ git_index__sort(index);
+ return git_vector_remove(&index->entries, (unsigned int)position);
+}
- return GIT_ENOTFOUND; /* NOT FOUND */
+int git_index_find(git_index *index, const char *path)
+{
+ git_index__sort(index);
+ return git_vector_search(&index->entries, path);
}
void git_index_tree__free(git_index_tree *tree)
@@ -659,29 +623,30 @@ int git_index__parse(git_index *index, const char *buffer, size_t buffer_size)
seek_forward(INDEX_HEADER_SIZE);
- index->entry_count = header.entry_count;
-
- /* If there is already a entires array, reuse it if it can hold all the
- * entries. If not, free and reallocate */
- if (index->entry_count > index->entries_size) {
- free(index->entries);
- index->entries_size = (uint32_t)(index->entry_count * 1.3f);
- index->entries = git__malloc(index->entries_size * sizeof(git_index_entry));
- }
+ git_vector_clear(&index->entries);
/* Parse all the entries */
- for (i = 0; i < index->entry_count && buffer_size > INDEX_FOOTER_SIZE; ++i) {
+ for (i = 0; i < header.entry_count && buffer_size > INDEX_FOOTER_SIZE; ++i) {
size_t entry_size;
- entry_size = read_entry(&index->entries[i], buffer, buffer_size);
+ git_index_entry *entry;
+
+ entry = git__malloc(sizeof(git_index_entry));
+ if (entry == NULL)
+ return GIT_ENOMEM;
+
+ entry_size = read_entry(entry, buffer, buffer_size);
/* 0 bytes read means an object corruption */
if (entry_size == 0)
return GIT_EOBJCORRUPTED;
+ if (git_vector_insert(&index->entries, entry) < 0)
+ return GIT_ENOMEM;
+
seek_forward(entry_size);
}
- if (i != index->entry_count)
+ if (i != header.entry_count)
return GIT_EOBJCORRUPTED;
/* There's still space for some extensions! */
@@ -746,13 +711,13 @@ int git_index__write(git_index *index, git_filelock *file)
WRITE_BYTES(INDEX_HEADER_SIG, 4);
WRITE_WORD(INDEX_VERSION_NUMBER);
- WRITE_WORD(index->entry_count);
+ WRITE_WORD(index->entries.length);
- for (i = 0; i < index->entry_count; ++i) {
+ for (i = 0; i < index->entries.length; ++i) {
git_index_entry *entry;
size_t path_length, padding;
- entry = &index->entries[i];
+ entry = git_vector_get(&index->entries, i);
path_length = strlen(entry->path);
WRITE_WORD(entry->ctime.seconds);
diff --git a/src/index.h b/src/index.h
index 6584049..f548c7b 100644
--- a/src/index.h
+++ b/src/index.h
@@ -3,6 +3,7 @@
#include "fileops.h"
#include "filelock.h"
+#include "vector.h"
#include "git/odb.h"
#include "git/index.h"
@@ -24,11 +25,8 @@ struct git_index {
char *index_file_path;
time_t last_modified;
+ git_vector entries;
- git_index_entry *entries;
- unsigned int entries_size;
-
- unsigned int entry_count;
unsigned int sorted:1,
on_disk:1;
diff --git a/src/tree.c b/src/tree.c
index 9dd407d..2eeba72 100644
--- a/src/tree.c
+++ b/src/tree.c
@@ -29,27 +29,7 @@
#include "tree.h"
#include "git/repository.h"
-static int resize_tree_array(git_tree *tree)
-{
- git_tree_entry **new_entries;
-
- tree->array_size *= 2;
- if (tree->array_size == 0)
- tree->array_size = 8;
-
- new_entries = git__malloc(tree->array_size * sizeof(git_tree_entry *));
- if (new_entries == NULL)
- return GIT_ENOMEM;
-
- memcpy(new_entries, tree->entries, tree->entry_count * sizeof(git_tree_entry *));
-
- free(tree->entries);
- tree->entries = new_entries;
-
- return GIT_SUCCESS;
-}
-
-int entry_cmp(const void *key, const void *array_member)
+int entry_search_cmp(const void *key, const void *array_member)
{
const char *filename = (const char *)key;
const git_tree_entry *entry = *(const git_tree_entry **)(array_member);
@@ -65,24 +45,22 @@ int entry_sort_cmp(const void *a, const void *b)
return strcmp(entry_a->filename, entry_b->filename);
}
-static void entry_resort(git_tree *tree)
-{
- qsort(tree->entries, tree->entry_count, sizeof(git_tree_entry *), entry_sort_cmp);
-}
-
static void free_tree_entries(git_tree *tree)
{
- size_t i;
+ unsigned int i;
if (tree == NULL)
return;
- for (i = 0; i < tree->entry_count; ++i) {
- free(tree->entries[i]->filename);
- free(tree->entries[i]);
+ for (i = 0; i < tree->entries.length; ++i) {
+ git_tree_entry *e;
+ e = git_vector_get(&tree->entries, i);
+
+ free(e->filename);
+ free(e);
}
- free(tree->entries);
+ git_vector_free(&tree->entries);
}
@@ -112,7 +90,7 @@ void git_tree_entry_set_name(git_tree_entry *entry, const char *name)
free(entry->filename);
entry->filename = git__strdup(name);
- entry_resort(entry->owner);
+ git_vector_sort(&entry->owner->entries);
entry->owner->object.modified = 1;
}
@@ -149,28 +127,27 @@ int git_tree_entry_2object(git_object **object_out, git_tree_entry *entry)
git_tree_entry *git_tree_entry_byname(git_tree *tree, const char *filename)
{
- git_tree_entry **found;
+ int idx;
assert(tree && filename);
- found = bsearch(filename, tree->entries, tree->entry_count, sizeof(git_tree_entry *), entry_cmp);
- return found ? *found : NULL;
+ idx = git_vector_search(&tree->entries, filename);
+ if (idx == GIT_ENOTFOUND)
+ return NULL;
+
+ return git_vector_get(&tree->entries, idx);
}
git_tree_entry *git_tree_entry_byindex(git_tree *tree, int idx)
{
assert(tree);
-
- if (tree->entries == NULL)
- return NULL;
-
- return (idx >= 0 && idx < (int)tree->entry_count) ? tree->entries[idx] : NULL;
+ return git_vector_get(&tree->entries, (unsigned int)idx);
}
size_t git_tree_entrycount(git_tree *tree)
{
assert(tree);
- return tree->entry_count;
+ return tree->entries.length;
}
int git_tree_add_entry(git_tree *tree, const git_oid *id, const char *filename, int attributes)
@@ -179,10 +156,6 @@ int git_tree_add_entry(git_tree *tree, const git_oid *id, const char *filename,
assert(tree && id && filename);
- if (tree->entry_count >= tree->array_size)
- if (resize_tree_array(tree) < 0)
- return GIT_ENOMEM;
-
if ((entry = git__malloc(sizeof(git_tree_entry))) == NULL)
return GIT_ENOMEM;
@@ -193,8 +166,10 @@ int git_tree_add_entry(git_tree *tree, const git_oid *id, const char *filename,
entry->attr = attributes;
entry->owner = tree;
- tree->entries[tree->entry_count++] = entry;
- entry_resort(tree);
+ if (git_vector_insert(&tree->entries, entry) < 0)
+ return GIT_ENOMEM;
+
+ git_vector_sort(&tree->entries);
tree->object.modified = 1;
return GIT_SUCCESS;
@@ -206,32 +181,28 @@ int git_tree_remove_entry_byindex(git_tree *tree, int idx)
assert(tree);
- if (idx < 0 || idx >= (int)tree->entry_count)
+ remove_ptr = git_vector_get(&tree->entries, (unsigned int)idx);
+ if (remove_ptr == NULL)
return GIT_ENOTFOUND;
- remove_ptr = tree->entries[idx];
- tree->entries[idx] = tree->entries[--tree->entry_count];
-
free(remove_ptr->filename);
free(remove_ptr);
- entry_resort(tree);
tree->object.modified = 1;
- return GIT_SUCCESS;
+
+ return git_vector_remove(&tree->entries, (unsigned int)idx);
}
int git_tree_remove_entry_byname(git_tree *tree, const char *filename)
{
- git_tree_entry **entry_ptr;
int idx;
assert(tree && filename);
- entry_ptr = bsearch(filename, tree->entries, tree->entry_count, sizeof(git_tree_entry *), entry_cmp);
- if (entry_ptr == NULL)
+ idx = git_vector_search(&tree->entries, filename);
+ if (idx == GIT_ENOTFOUND)
return GIT_ENOTFOUND;
- idx = (int)(entry_ptr - tree->entries);
return git_tree_remove_entry_byindex(tree, idx);
}
@@ -242,14 +213,15 @@ int git_tree__writeback(git_tree *tree, git_odb_source *src)
assert(tree && src);
- if (tree->entries == NULL)
+ if (tree->entries.length == 0)
return GIT_EMISSINGOBJDATA;
- entry_resort(tree);
+ git_vector_sort(&tree->entries);
- for (i = 0; i < tree->entry_count; ++i) {
+ for (i = 0; i < tree->entries.length; ++i) {
git_tree_entry *entry;
- entry = tree->entries[i];
+
+ entry = git_vector_get(&tree->entries, i);
sprintf(filemode, "%06o ", entry->attr);
@@ -265,31 +237,26 @@ int git_tree__writeback(git_tree *tree, git_odb_source *src)
static int tree_parse_buffer(git_tree *tree, char *buffer, char *buffer_end)
{
static const size_t avg_entry_size = 40;
+ unsigned int expected_size;
int error = 0;
- free_tree_entries(tree);
-
- tree->entry_count = 0;
- tree->array_size = (tree->object.source.raw.len / avg_entry_size) + 1;
- tree->entries = git__malloc(tree->array_size * sizeof(git_tree_entry *));
+ expected_size = (tree->object.source.raw.len / avg_entry_size) + 1;
- if (tree->entries == NULL)
+ free_tree_entries(tree);
+ if (git_vector_init(&tree->entries, expected_size, entry_sort_cmp, entry_search_cmp) < 0)
return GIT_ENOMEM;
while (buffer < buffer_end) {
git_tree_entry *entry;
- if (tree->entry_count >= tree->array_size)
- if (resize_tree_array(tree) < 0)
- return GIT_ENOMEM;
-
entry = git__malloc(sizeof(git_tree_entry));
if (entry == NULL) {
error = GIT_ENOMEM;
break;
}
- tree->entries[tree->entry_count++] = entry;
+ if (git_vector_insert(&tree->entries, entry) < 0)
+ return GIT_ENOMEM;
entry->owner = tree;
entry->attr = strtol(buffer, &buffer, 8);
diff --git a/src/tree.h b/src/tree.h
index db1c19f..84d0222 100644
--- a/src/tree.h
+++ b/src/tree.h
@@ -3,6 +3,7 @@
#include <git/tree.h>
#include "repository.h"
+#include "vector.h"
struct git_tree_entry {
unsigned int attr;
@@ -14,10 +15,7 @@ struct git_tree_entry {
struct git_tree {
git_object object;
-
- git_tree_entry **entries;
- size_t entry_count;
- size_t array_size;
+ git_vector entries;
};
void git_tree__free(git_tree *tree);
diff --git a/src/vector.c b/src/vector.c
new file mode 100644
index 0000000..3f76df2
--- /dev/null
+++ b/src/vector.c
@@ -0,0 +1,146 @@
+/*
+ * This file is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License, version 2,
+ * as published by the Free Software Foundation.
+ *
+ * In addition to the permissions in the GNU General Public License,
+ * the authors give you unlimited permission to link the compiled
+ * version of this file into combinations with other programs,
+ * and to distribute those combinations without any restriction
+ * coming from the use of this file. (The General Public License
+ * restrictions do apply in other respects; for example, they cover
+ * modification of the file, and distribution when not linked into
+ * a combined executable.)
+ *
+ * This file is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; see the file COPYING. If not, write to
+ * the Free Software Foundation, 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
+ */
+
+#include "common.h"
+#include "repository.h"
+#include "vector.h"
+
+static const double resize_factor = 1.75;
+static const int minimum_size = 8;
+
+static int resize_vector(git_vector *v)
+{
+ void **new_contents;
+
+ v->_alloc_size *= resize_factor;
+ if (v->_alloc_size == 0)
+ v->_alloc_size = minimum_size;
+
+ new_contents = git__malloc(v->_alloc_size * sizeof(void *));
+ if (new_contents == NULL)
+ return GIT_ENOMEM;
+
+ memcpy(new_contents, v->contents, v->length * sizeof(void *));
+
+ free(v->contents);
+ v->contents = new_contents;
+
+ return GIT_SUCCESS;
+}
+
+
+void git_vector_free(git_vector *v)
+{
+ assert(v);
+ free(v->contents);
+}
+
+int git_vector_init(git_vector *v, unsigned int initial_size, git_vector_cmp cmp, git_vector_srch srch)
+{
+ assert(v);
+
+ memset(v, 0x0, sizeof(git_vector));
+
+ if (initial_size == 0)
+ initial_size = minimum_size;
+
+ v->_alloc_size = initial_size;
+ v->_cmp = cmp;
+ v->_srch = srch;
+
+ v->length = 0;
+
+ v->contents = git__malloc(v->_alloc_size * sizeof(void *));
+ if (v->contents == NULL)
+ return GIT_ENOMEM;
+
+ return GIT_SUCCESS;
+}
+
+int git_vector_insert(git_vector *v, void *element)
+{
+ assert(v);
+
+ if (v->length >= v->_alloc_size) {
+ if (resize_vector(v) < 0)
+ return GIT_ENOMEM;
+ }
+
+ v->contents[v->length++] = element;
+
+ return GIT_SUCCESS;
+}
+
+void *git_vector_get(git_vector *v, unsigned int position)
+{
+ assert(v);
+ return (position < v->length) ? v->contents[position] : NULL;
+}
+
+void git_vector_sort(git_vector *v)
+{
+ assert(v);
+
+ if (v->_cmp != NULL)
+ qsort(v->contents, v->length, sizeof(void *), v->_cmp);
+}
+
+int git_vector_search(git_vector *v, const void *key)
+{
+ void **find;
+
+ if (v->_srch == NULL)
+ return GIT_ENOTFOUND;
+
+ find = bsearch(key, v->contents, v->length, sizeof(void *), v->_srch);
+ if (find == NULL)
+ return GIT_ENOTFOUND;
+
+ return (int)(find - v->contents);
+}
+
+int git_vector_remove(git_vector *v, unsigned int idx)
+{
+ unsigned int i;
+
+ assert(v);
+
+ if (idx >= v->length || v->length == 0)
+ return GIT_ENOTFOUND;
+
+ for (i = idx; i < v->length; ++i)
+ v->contents[i] = v->contents[i + 1];
+
+ v->length--;
+ return GIT_SUCCESS;
+}
+
+void git_vector_clear(git_vector *v)
+{
+ assert(v);
+ v->length = 0;
+}
+
+
diff --git a/src/vector.h b/src/vector.h
new file mode 100644
index 0000000..8239aba
--- /dev/null
+++ b/src/vector.h
@@ -0,0 +1,32 @@
+#ifndef INCLUDE_vector_h__
+#define INCLUDE_vector_h__
+
+#include "git/common.h"
+
+
+typedef int (*git_vector_cmp)(const void *, const void *);
+typedef int (*git_vector_srch)(const void *, const void *);
+
+typedef struct git_vector {
+ unsigned int _alloc_size;
+ git_vector_cmp _cmp;
+ git_vector_srch _srch;
+
+ void **contents;
+ unsigned int length;
+} git_vector;
+
+
+int git_vector_init(git_vector *v, unsigned int initial_size, git_vector_cmp cmp, git_vector_srch srch);
+void git_vector_free(git_vector *v);
+void git_vector_clear(git_vector *v);
+
+int git_vector_search(git_vector *v, const void *key);
+void git_vector_sort(git_vector *v);
+
+void *git_vector_get(git_vector *v, unsigned int position);
+
+int git_vector_insert(git_vector *v, void *element);
+int git_vector_remove(git_vector *v, unsigned int idx);
+
+#endif
diff --git a/tests/t0601-read.c b/tests/t0601-read.c
index 6156745..6ddb5fb 100644
--- a/tests/t0601-read.c
+++ b/tests/t0601-read.c
@@ -35,7 +35,7 @@ BEGIN_TEST(index_loadempty_test)
must_pass(git_index_read(index));
must_be_true(index->on_disk == 0);
- must_be_true(index->entry_count == 0);
+ must_be_true(git_index_entrycount(index) == 0);
must_be_true(index->sorted);
git_index_free(index);
@@ -44,6 +44,7 @@ END_TEST
BEGIN_TEST(index_load_test)
git_index *index;
unsigned int i;
+ git_index_entry **entries;
must_pass(git_index_open_bare(&index, TEST_INDEX_PATH));
must_be_true(index->on_disk);
@@ -51,11 +52,13 @@ BEGIN_TEST(index_load_test)
must_pass(git_index_read(index));
must_be_true(index->on_disk);
- must_be_true(index->entry_count == TEST_INDEX_ENTRY_COUNT);
+ must_be_true(git_index_entrycount(index) == TEST_INDEX_ENTRY_COUNT);
must_be_true(index->sorted);
+ entries = (git_index_entry **)index->entries.contents;
+
for (i = 0; i < ARRAY_SIZE(TEST_ENTRIES); ++i) {
- git_index_entry *e = &index->entries[TEST_ENTRIES[i].index];
+ git_index_entry *e = entries[TEST_ENTRIES[i].index];
must_be_true(strcmp(e->path, TEST_ENTRIES[i].path) == 0);
must_be_true(e->mtime.seconds == TEST_ENTRIES[i].mtime);
@@ -74,7 +77,7 @@ BEGIN_TEST(index2_load_test)
must_pass(git_index_read(index));
must_be_true(index->on_disk);
- must_be_true(index->entry_count == TEST_INDEX2_ENTRY_COUNT);
+ must_be_true(git_index_entrycount(index) == TEST_INDEX2_ENTRY_COUNT);
must_be_true(index->sorted);
must_be_true(index->tree != NULL);
diff --git a/tests/t0603-sort.c b/tests/t0603-sort.c
index d47c93c..35f7f87 100644
--- a/tests/t0603-sort.c
+++ b/tests/t0603-sort.c
@@ -7,26 +7,32 @@
#define TEST_INDEX_PATH "../t0600-objects/index"
+/*
void print_entries(git_index *index)
{
unsigned int i;
- for (i = 0; i < index->entry_count; ++i)
+ for (i = 0; i < index->entries.length; ++i)
printf("%d: %s\n", i, index->entries[i].path);
}
+*/
void randomize_entries(git_index *index)
{
unsigned int i, j;
- git_index_entry tmp;
+ git_index_entry *tmp;
+ git_index_entry **entries;
+
+ entries = (git_index_entry **)index->entries.contents;
srand((unsigned int)time(NULL));
- for (i = 0; i < index->entry_count; ++i) {
- j = rand() % index->entry_count;
- memcpy(&tmp, &index->entries[j], sizeof(git_index_entry));
- memcpy(&index->entries[j], &index->entries[i], sizeof(git_index_entry));
- memcpy(&index->entries[i], &tmp, sizeof(git_index_entry));
+ for (i = 0; i < index->entries.length; ++i) {
+ j = rand() % index->entries.length;
+
+ tmp = entries[j];
+ entries[j] = entries[i];
+ entries[i] = tmp;
}
index->sorted = 0;
@@ -35,6 +41,9 @@ void randomize_entries(git_index *index)
BEGIN_TEST(index_sort_test)
git_index *index;
unsigned int i;
+ git_index_entry **entries;
+
+ entries = (git_index_entry **)index->entries.contents;
must_pass(git_index_open_bare(&index, TEST_INDEX_PATH));
must_pass(git_index_read(index));
@@ -44,9 +53,8 @@ BEGIN_TEST(index_sort_test)
git_index__sort(index);
must_be_true(index->sorted);
- for (i = 1; i < index->entry_count; ++i)
- must_be_true(strcmp(index->entries[i - 1].path,
- index->entries[i].path) < 0);
+ for (i = 1; i < index->entries.length; ++i)
+ must_be_true(strcmp(entries[i - 1]->path, entries[i]->path) < 0);
git_index_free(index);
END_TEST