Objectively
Ultra-lightweight object oriented framework for GNU C.
Loading...
Searching...
No Matches
IndexPath.c
Go to the documentation of this file.
1/*
2 * Objectively: Ultra-lightweight object oriented framework for GNU C.
3 * Copyright (C) 2014 Jay Dolan <jay@jaydolan.com>
4 *
5 * This software is provided 'as-is', without any express or implied
6 * warranty. In no event will the authors be held liable for any damages
7 * arising from the use of this software.
8 *
9 * Permission is granted to anyone to use this software for any purpose,
10 * including commercial applications, and to alter it and redistribute it
11 * freely, subject to the following restrictions:
12 *
13 * 1. The origin of this software must not be misrepresented; you must not
14 * claim that you wrote the original software. If you use this software
15 * in a product, an acknowledgment in the product documentation would be
16 * appreciated but is not required.
17 *
18 * 2. Altered source versions must be plainly marked as such, and must not be
19 * misrepresented as being the original software.
20 *
21 * 3. This notice may not be removed or altered from any source distribution.
22 */
23
24#include <assert.h>
25#include <stdlib.h>
26#include <string.h>
27
28#include "Hash.h"
29#include "IndexPath.h"
30#include "String.h"
31
32#define _Class _IndexPath
33
34#pragma mark - Object
35
39static Object *copy(const Object *self) {
40
41 IndexPath *this = (IndexPath *) self;
42 IndexPath *that = $(alloc(IndexPath), initWithIndexes, this->indexes, this->length);
43
44 return (Object *) that;
45}
46
50static void dealloc(Object *self) {
51
52 IndexPath *this = (IndexPath *) self;
53
54 free(this->indexes);
55
56 super(Object, self, dealloc);
57}
58
62static String *description(const Object *self) {
63
64 const IndexPath *this = (IndexPath *) self;
65 String *desc = str("[");
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}
77
81static int hash(const Object *self) {
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}
93
97static bool isEqual(const Object *self, const Object *other) {
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}
115
116#pragma mark - IndexPath
117
122static size_t indexAtPosition(const IndexPath *self, size_t position) {
123
124 assert(position < self->length);
125
126 return self->indexes[position];
127}
128
133static IndexPath *initWithIndex(IndexPath *self, size_t index) {
134 return $(self, initWithIndexes, &index, 1);
135}
136
141static IndexPath *initWithIndexes(IndexPath *self, size_t *indexes, size_t length) {
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}
157
158#pragma mark - Class lifecycle
159
163static void initialize(Class *clazz) {
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}
175
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}
197
198#undef _Class
199
static Array * init(Array *self)
Definition Array.c:420
Class * _initialize(const ClassDef *def)
Initializes the given Class.
Definition Class.c:86
#define alloc(type)
Allocate and initialize and instance of type.
Definition Class.h:176
#define super(type, obj, method,...)
int HashForInteger(int hash, const long integer)
Accumulates the hash value of integer into hash.
Definition Hash.c:66
Utilities for calculating hash values.
#define HASH_SEED
The hash seed value.
Definition Hash.h:37
Class * _IndexPath(void)
Definition IndexPath.c:180
static IndexPath * initWithIndexes(IndexPath *self, size_t *indexes, size_t length)
Definition IndexPath.c:141
static bool isEqual(const Object *self, const Object *other)
Definition IndexPath.c:97
static IndexPath * initWithIndex(IndexPath *self, size_t index)
Definition IndexPath.c:133
static size_t indexAtPosition(const IndexPath *self, size_t position)
Definition IndexPath.c:122
static String * description(const Object *self)
Definition IndexPath.c:62
static void dealloc(Object *self)
Definition IndexPath.c:50
static Object * copy(const Object *self)
Definition IndexPath.c:39
static void initialize(Class *clazz)
Definition IndexPath.c:163
static int hash(const Object *self)
Definition IndexPath.c:81
Index paths represent the path to an element or node within a tree or graph structure.
static bool isKindOfClass(const Object *self, const Class *clazz)
Definition Object.c:101
Class * _Object(void)
Definition Object.c:136
String * str(const char *fmt,...)
Definition String.c:1084
static void appendCharacters(String *self, const char *chars)
Definition String.c:506
static void appendFormat(String *self, const char *fmt,...)
Definition String.c:541
UTF-8 strings.
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
ident interface
The interface of the Class.
Definition Class.h:105
Index paths represent the path to an element or node within a tree or graph structure.
Definition IndexPath.h:40
IndexPath * initWithIndexes(IndexPath *self, size_t *indexes, size_t length)
Initializes this IndexPath with the specified indexes and length.
Definition IndexPath.c:141
size_t * indexes
The indexes.
Definition IndexPath.h:56
size_t length
The length of indexes.
Definition IndexPath.h:61
Object is the root Class of The Objectively Class hierarchy.
Definition Object.h:46
Object * copy(const Object *self)
Creates a shallow copy of this Object.
Definition Array.c:84
int hash(const Object *self)
Definition Array.c:129
void dealloc(Object *self)
Frees all resources held by this Object.
Definition Array.c:99
UTF-8 strings.
Definition String.h:69