35static int compare(
const void *a,
const void *b) {
37 const size_t sa = *(
size_t *) a;
38 const size_t sb = *(
size_t *) b;
40 return sa < sb ? -1 : sa > sb ? 1 : 0;
46static size_t compact(
size_t *indexes,
size_t count) {
51 qsort(indexes, count,
sizeof(
size_t),
compare);
53 for (
size_t i = 1; i < count; i++) {
54 if (indexes[i] != indexes[size]) {
55 indexes[++size] = indexes[i];
65#define _Class _IndexSet
67#define INDEX_SET_CHUNK_SIZE 8
102 for (
size_t i = 0; i < this->count; i++) {
104 if (i < this->count - 1) {
122 for (
size_t i = 0; i < this->count; i++) {
143 if (this->count == that->
count) {
144 return memcmp(this->indexes, that->
indexes, this->count *
sizeof(
size_t)) == 0;
151#pragma mark - IndexSet
159 for (
size_t i = 0; i < self->
count; i++) {
160 if (self->
indexes[i] == index) {
186 self->capacity = self->
count;
192 memcpy(self->
indexes, indexes,
sizeof(
size_t) * self->
count);
199#pragma mark - IndexSet mutation
210 for (i = 0; i < this->count; i++) {
211 if (this->indexes[i] == index) {
214 if (this->indexes[i] > index) {
219 if (this->count == self->capacity) {
222 this->indexes = realloc(this->indexes, self->capacity *
sizeof(
size_t));
223 assert(this->indexes);
226 for (
size_t j = this->count; j > i; j--) {
227 this->indexes[j] = this->indexes[j - 1];
230 this->indexes[i] = index;
240 for (
size_t i = 0; i < count; i++) {
275 self->capacity = capacity;
277 self->
indexes = malloc(self->capacity *
sizeof(
size_t));
293 this->indexes = NULL;
306 for (
size_t i = 0; i < this->count; i++) {
307 if (this->indexes[i] == index) {
309 for (
size_t j = i; j < this->count; j++) {
310 this->indexes[j] = this->indexes[j + 1];
323 for (
size_t i = 0; i < count; i++) {
339#pragma mark - Class lifecycle
379 .interfaceOffset = offsetof(
IndexSet, interface),
380 .interfaceSize =
sizeof(IndexSetInterface),
Class * _initialize(const ClassDef *def)
Initializes the given Class.
#define alloc(type)
Allocate and initialize and instance of type.
#define super(type, obj, method,...)
int HashForInteger(int hash, const long integer)
Accumulates the hash value of integer into hash.
Utilities for calculating hash values.
#define HASH_SEED
The hash seed value.
static bool containsIndex(const IndexSet *self, size_t index)
static IndexSet * init(IndexSet *self)
static void removeIndexesInRange(IndexSet *self, const Range range)
static void removeAllIndexes(IndexSet *self)
static bool isEqual(const Object *self, const Object *other)
static IndexSet * initWithIndexes(IndexSet *self, size_t *indexes, size_t count)
static int compare(const void *a, const void *b)
qsort comparator for indexes.
static IndexSet * initWithCapacity(IndexSet *self, size_t capacity)
static String * description(const Object *self)
static size_t compact(size_t *indexes, size_t count)
Sorts and compacts the given array to contain only unique values.
static void dealloc(Object *self)
static void removeIndexes(IndexSet *self, size_t *indexes, size_t count)
static void removeIndex(IndexSet *self, size_t index)
static IndexSet * initWithIndex(IndexSet *self, size_t index)
static Object * copy(const Object *self)
static void initialize(Class *clazz)
static void addIndex(IndexSet *self, size_t index)
static void addIndexesInRange(IndexSet *self, const Range range)
#define INDEX_SET_CHUNK_SIZE
static int hash(const Object *self)
static void addIndexes(IndexSet *self, size_t *indexes, size_t count)
Collections of unique index values.
static bool isKindOfClass(const Object *self, const Class *clazz)
String * str(const char *fmt,...)
static void appendCharacters(String *self, const char *chars)
static void appendFormat(String *self, const char *fmt,...)
#define do_once(once, block)
Executes the given block at most one time.
ClassDefs are passed to _initialize via an archetype to initialize a Class.
The runtime representation of a Class.
ident interface
The interface of the Class.
Collections of unique index values.
IndexSet * initWithCapacity(IndexSet *self, size_t capacity)
Initializes this IndexSet with the specified capacity.
IndexSet * initWithIndex(IndexSet *self, size_t index)
Initializes this IndexSet with the specified index.
size_t * indexes
The indexes.
void removeAllIndexes(IndexSet *self)
Removes all indexes from this IndexSet.
IndexSet * initWithIndexes(IndexSet *self, size_t *indexes, size_t count)
Initializes this IndexSet with the specified indexes and count.
size_t count
The count of indexes.
Object is the root Class of The Objectively Class hierarchy.
int hash(const Object *self)
void dealloc(Object *self)
Frees all resources held by this Object.
A location and length into contiguous collections.
ssize_t location
The location.