Objectively 1.0.0
Ultra-lightweight object oriented framework for GNU C.
Macros | Functions
IndexPath.c File Reference
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include "Hash.h"
#include "IndexPath.h"
#include "MutableString.h"

Go to the source code of this file.

Macros

#define _Class   _IndexPath
 

Functions

Class_IndexPath (void)
 
static Objectcopy (const Object *self)
 
static void dealloc (Object *self)
 
static Stringdescription (const Object *self)
 
static int hash (const Object *self)
 
static size_t indexAtPosition (const IndexPath *self, size_t position)
 
static void initialize (Class *clazz)
 
static IndexPathinitWithIndex (IndexPath *self, size_t index)
 
static IndexPathinitWithIndexes (IndexPath *self, size_t *indexes, size_t length)
 
static _Bool isEqual (const Object *self, const Object *other)
 

Macro Definition Documentation

◆ _Class

#define _Class   _IndexPath

Definition at line 32 of file IndexPath.c.

Function Documentation

◆ _IndexPath()

Class * _IndexPath ( void  )

Definition at line 180 of file IndexPath.c.

180 {
181 static Class *clazz;
182 static Once once;
183
184 do_once(&once, {
185 clazz = _initialize(&(const ClassDef) {
186 .name = "IndexPath",
187 .superclass = _Object(),
188 .instanceSize = sizeof(IndexPath),
189 .interfaceOffset = offsetof(IndexPath, interface),
190 .interfaceSize = sizeof(IndexPathInterface),
192 });
193 });
194
195 return clazz;
196}
Class * _initialize(const ClassDef *def)
Initializes the given Class.
Definition: Class.c:91
static void initialize(Class *clazz)
Definition: IndexPath.c:163
long Once
The Once type.
Definition: Once.h:37
#define do_once(once, block)
Executes the given block at most one time.
Definition: Once.h:43
ClassDefs are passed to _initialize via an archetype to initialize a Class.
Definition: Class.h:41
The runtime representation of a Class.
Definition: Class.h:95
Index paths represent the path to an element or node within a tree or graph structure.
Definition: IndexPath.h:40
Class * _Object(void)
The Object archetype.
Definition: Object.c:136

◆ copy()

static Object * copy ( const Object self)
static
See also
Object::copy(const Object *)

Definition at line 39 of file IndexPath.c.

39 {
40
41 IndexPath *this = (IndexPath *) self;
42 IndexPath *that = $(alloc(IndexPath), initWithIndexes, this->indexes, this->length);
43
44 return (Object *) that;
45}
#define alloc(type)
Allocate and initialize and instance of type.
Definition: Class.h:159
IndexPath * initWithIndexes(IndexPath *self, size_t *indexes, size_t length)
Initializes this IndexPath with the specified indexes and length.
Definition: IndexPath.c:141
Object is the root Class of The Objectively Class hierarchy.
Definition: Object.h:46

◆ dealloc()

static void dealloc ( Object self)
static
See also
Object::dealloc(Object *)

Definition at line 50 of file IndexPath.c.

50 {
51
52 IndexPath *this = (IndexPath *) self;
53
54 free(this->indexes);
55
56 super(Object, self, dealloc);
57}
#define super(type, obj, method,...)
void dealloc(Object *self)
Frees all resources held by this Object.
Definition: Array.c:50

◆ description()

static String * description ( const Object self)
static
See also
Object::description(const Object *)

Definition at line 62 of file IndexPath.c.

62 {
63
64 const IndexPath *this = (IndexPath *) self;
65 MutableString *desc = mstr("[");
66
67 for (size_t i = 0; i < this->length; i++) {
68 $(desc, appendFormat, "%d", this->indexes[i]);
69 if (i < this->length - 1) {
70 $(desc, appendCharacters, ", ");
71 }
72 }
73
74 $(desc, appendCharacters, "]");
75 return (String *) desc;
76}
Mutable UTF-8 strings.
Definition: MutableString.h:40
void appendFormat(MutableString *self, const char *fmt,...)
Appends the specified formatted string.
Definition: MutableString.c:89
void appendCharacters(MutableString *self, const char *chars)
Appends the specified UTF-8 encoded C string.
Definition: MutableString.c:54
Immutable UTF-8 strings.
Definition: String.h:69
OBJECTIVELY_EXPORT MutableString * mstr(const char *fmt,...)
A convenience function for instantiating MutableStrings.

◆ hash()

static int hash ( const Object self)
static
See also
Object::hash(const Object *)

Definition at line 81 of file IndexPath.c.

81 {
82
83 int hash = HASH_SEED;
84
85 const IndexPath *this = (IndexPath *) self;
86
87 for (size_t i = 0; i < this->length; i++) {
88 hash = HashForInteger(hash, this->indexes[i]);
89 }
90
91 return hash;
92}
int HashForInteger(int hash, const long integer)
Accumulates the hash value of integer into hash.
Definition: Hash.c:62
#define HASH_SEED
The hash seed value.
Definition: Hash.h:37
int hash(const Object *self)
Definition: Array.c:80

◆ indexAtPosition()

static size_t indexAtPosition ( const IndexPath self,
size_t  position 
)
static

Definition at line 122 of file IndexPath.c.

122 {
123
124 assert(position < self->length);
125
126 return self->indexes[position];
127}
size_t * indexes
The indexes.
Definition: IndexPath.h:56

◆ initialize()

static void initialize ( Class clazz)
static
See also
Class::initialize(Class *)

Definition at line 163 of file IndexPath.c.

163 {
164
165 ((ObjectInterface *) clazz->interface)->copy = copy;
166 ((ObjectInterface *) clazz->interface)->dealloc = dealloc;
167 ((ObjectInterface *) clazz->interface)->description = description;
168 ((ObjectInterface *) clazz->interface)->hash = hash;
169 ((ObjectInterface *) clazz->interface)->isEqual = isEqual;
170
171 ((IndexPathInterface *) clazz->interface)->indexAtPosition = indexAtPosition;
172 ((IndexPathInterface *) clazz->interface)->initWithIndex = initWithIndex;
173 ((IndexPathInterface *) clazz->interface)->initWithIndexes = initWithIndexes;
174}
ident interface
The interface of the Class.
Definition: Class.h:105
IndexPath * initWithIndex(IndexPath *self, size_t index)
Initializes this IndexPath with the specified index.
Definition: IndexPath.c:133
size_t indexAtPosition(const IndexPath *self, size_t position)
Definition: IndexPath.c:122
Object * copy(const Object *self)
Creates a shallow copy of this Object.
Definition: Array.c:40
String * description(const Object *self)
Definition: Array.c:66
_Bool isEqual(const Object *self, const Object *other)
Tests equality of the other Object.
Definition: Array.c:96

◆ initWithIndex()

static IndexPath * initWithIndex ( IndexPath self,
size_t  index 
)
static

Definition at line 133 of file IndexPath.c.

133 {
134 return $(self, initWithIndexes, &index, 1);
135}

◆ initWithIndexes()

static IndexPath * initWithIndexes ( IndexPath self,
size_t *  indexes,
size_t  length 
)
static

Definition at line 141 of file IndexPath.c.

141 {
142
143 self = (IndexPath *) super(Object, self, init);
144 if (self) {
145
146 self->length = length;
147 assert(self->length);
148
149 self->indexes = calloc(sizeof(size_t), length);
150 assert(self->indexes);
151
152 memcpy(self->indexes, indexes, sizeof(size_t) * length);
153 }
154
155 return self;
156}
Condition * init(Condition *self)
Initializes this Condition.
Definition: Condition.c:67
size_t length
The length of indexes.
Definition: IndexPath.h:61

◆ isEqual()

static _Bool isEqual ( const Object self,
const Object other 
)
static
See also
Object::isEqual(const Object *, const Object *)

Definition at line 97 of file IndexPath.c.

97 {
98
99 if (super(Object, self, isEqual, other)) {
100 return true;
101 }
102
103 if (other && $(other, isKindOfClass, _IndexPath())) {
104
105 const IndexPath *this = (IndexPath *) self;
106 const IndexPath *that = (IndexPath *) other;
107
108 if (this->length == that->length) {
109 return memcmp(this->indexes, that->indexes, this->length * sizeof(size_t)) == 0;
110 }
111 }
112
113 return false;
114}
Class * _IndexPath(void)
The IndexPath archetype.
Definition: IndexPath.c:180
_Bool isKindOfClass(const Object *self, const Class *clazz)
Tests for Class hierarchy membership.
Definition: Object.c:101