Objectively 1.0.0
Ultra-lightweight object oriented framework for GNU C.
Vector.h
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#pragma once
25
26#include <stdarg.h>
27
28#include <Objectively/Object.h>
29
35typedef struct Vector Vector;
36typedef struct VectorInterface VectorInterface;
37
44typedef void (*VectorEnumerator)(const Vector *vector, ident obj, ident data);
45
51struct Vector {
52
57
62 VectorInterface *interface;
63
67 size_t capacity;
68
72 size_t count;
73
77 size_t size;
78
83};
84
88#define VectorElement(vector, type, index) \
89 (((type *) vector->elements) + (index))
90
94struct VectorInterface {
95
99 ObjectInterface objectInterface;
100
108 void (*addElement)(Vector *self, const ident element);
109
118 void (*enumerateElements)(const Vector *self, VectorEnumerator enumerator, ident data);
119
128 void (*filterElements)(Vector *self, Predicate predicate, ident data);
129
138 ident (*findElement)(const Vector *self, Predicate predicate, ident data);
139
147 ssize_t (*indexOfElement)(const Vector *self, const ident element);
148
159 Vector *(*initWithElements)(Vector *self, size_t size, size_t count, ident elements);
160
169 Vector *(*initWithSize)(Vector *self, size_t size);
170
179 void (*insertElementAtIndex)(Vector *self, const ident element, size_t index);
180
190 ident (*reduce)(const Vector *self, Reducer reducer, ident accumulator, ident data);
191
198 void (*removeAllElements)(Vector *self);
199
207 void (*removeElementAtIndex)(Vector *self, size_t index);
208
216 void (*resize)(Vector *self, size_t capacity);
217
225 void (*sort)(Vector *self, Comparator comparator);
226
235 Vector *(*vectorWithSize)(size_t size);
236
247 Vector *(*vectorWithElements)(size_t size, size_t count, ident elements);
248};
249
static ident reduce(const Array *self, Reducer reducer, ident accumulator, ident data)
Definition: Array.c:405
#define obj
static void sort(MutableArray *self, Comparator comparator)
Definition: MutableArray.c:314
static MutableData * data(void)
Definition: MutableData.c:75
Object is the root Class of The Objectively Class hierarchy.
void * ident
The identity type, similar to Objective-C id.
Definition: Types.h:49
#define OBJECTIVELY_EXPORT
Definition: Types.h:36
_Bool(* Predicate)(const ident obj, ident data)
The Predicate function type for filtering Objects.
Definition: Types.h:111
Order(* Comparator)(const ident obj1, const ident obj2)
The Comparator function type for ordering Objects.
Definition: Types.h:82
ident(* Reducer)(const ident obj, ident accumulator, ident data)
The Reducer function type for reducing collections.
Definition: Types.h:127
static void insertElementAtIndex(Vector *self, const ident element, size_t index)
Definition: Vector.c:221
static void addElement(Vector *self, const ident element)
Definition: Vector.c:109
static void resize(Vector *self, size_t capacity)
Definition: Vector.c:276
static void removeElementAtIndex(Vector *self, size_t index)
Definition: Vector.c:261
static void removeAllElements(Vector *self)
Definition: Vector.c:253
static ssize_t indexOfElement(const Vector *self, const ident element)
Definition: Vector.c:177
static void filterElements(Vector *self, Predicate predicate, ident data)
Definition: Vector.c:145
static ident findElement(const Vector *self, Predicate predicate, ident data)
Definition: Vector.c:160
static void enumerateElements(const Vector *self, VectorEnumerator enumerator, ident data)
Definition: Vector.c:132
void(* VectorEnumerator)(const Vector *vector, ident obj, ident data)
The VectorEnumerator function type.
Definition: Vector.h:44
The runtime representation of a Class.
Definition: Class.h:95
MutableData * data(void)
Returns a new MutableData.
Definition: MutableData.c:75
Object is the root Class of The Objectively Class hierarchy.
Definition: Object.h:46
Vectors.
Definition: Vector.h:51
Object object
The superclass.
Definition: Vector.h:56
VectorInterface * interface
The interface.
Definition: Vector.h:62
Class * _Vector(void)
The Vector archetype.
Definition: Vector.c:387
size_t count
The count of elements.
Definition: Vector.h:72
size_t capacity
The capacity.
Definition: Vector.h:67
ident elements
The elements.
Definition: Vector.h:82
size_t size
The size of each element.
Definition: Vector.h:77