Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[glibutil] Added gutil_objv module. JB#33786
  • Loading branch information
monich committed May 28, 2023
1 parent 91f2bff commit 70ac525
Show file tree
Hide file tree
Showing 7 changed files with 615 additions and 0 deletions.
1 change: 1 addition & 0 deletions Makefile
Expand Up @@ -69,6 +69,7 @@ SRC = \
gutil_ring.c \
gutil_strv.c \
gutil_timenotify.c \
gutil_objv.c \
gutil_version.c \
gutil_weakref.c

Expand Down
118 changes: 118 additions & 0 deletions include/gutil_objv.h
@@ -0,0 +1,118 @@
/*
* Copyright (C) 2023 Slava Monich <slava@monich.com>
*
* You may use this file under the terms of BSD license as follows:
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the names of the copyright holders nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef GUTIL_OBJV_H
#define GUTIL_OBJV_H

#include "gutil_types.h"

#include <glib-object.h>

/*
* Operations on NULL-terminated array of references to GObjects.
*
* Since 1.0.70
*/

G_BEGIN_DECLS

void
gutil_objv_free(
GObject** objv);

GObject**
gutil_objv_copy(
GObject* const* objv)
G_GNUC_WARN_UNUSED_RESULT;

GObject**
gutil_objv_add(
GObject** objv,
GObject* obj)
G_GNUC_WARN_UNUSED_RESULT;

GObject**
gutil_objv_remove(
GObject** objv,
GObject* obj,
gboolean all)
G_GNUC_WARN_UNUSED_RESULT;

GObject**
gutil_objv_remove_at(
GObject** objv,
gsize pos)
G_GNUC_WARN_UNUSED_RESULT;

GObject*
gutil_objv_at(
GObject* const* objv,
gsize pos);

gboolean
gutil_objv_equal(
GObject* const* objv1,
GObject* const* objv2);

GObject*
gutil_objv_first(
GObject* const* objv);

GObject*
gutil_objv_last(
GObject* const* objv);

gssize
gutil_objv_find(
GObject* const* objv,
GObject* obj);

gssize
gutil_objv_find_last(
GObject* const* objv,
GObject* obj);

gboolean
gutil_objv_contains(
GObject* const* objv,
GObject* obj);

G_END_DECLS

#endif /* GUTIL_OBJV_H */

/*
* Local Variables:
* mode: C
* c-basic-offset: 4
* indent-tabs-mode: nil
* End:
*/
257 changes: 257 additions & 0 deletions src/gutil_objv.c
@@ -0,0 +1,257 @@
/*
* Copyright (C) 2023 Slava Monich <slava@monich.com>
*
* You may use this file under the terms of BSD license as follows:
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the names of the copyright holders nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/

#include "gutil_objv.h"
#include "gutil_misc.h"

void
gutil_objv_free(
GObject** objv)
{
if (objv) {
GObject** ptr = objv;
while (*ptr) g_object_unref(*ptr++);
g_free(objv);
}
}

GObject**
gutil_objv_copy(
GObject* const* objv)
{
if (objv) {
GObject* const* ptr = objv;
gsize n = 0;
/* Count the services and bump references at the same time */
while (*ptr) {
g_object_ref(*ptr++);
n++;
}
return gutil_memdup(objv, sizeof(GObject*) * (n + 1));
}
return NULL;

}

GObject**
gutil_objv_add(
GObject** objv,
GObject* obj)
{
if (obj) {
gsize len = gutil_ptrv_length(objv);

objv = g_renew(GObject*, objv, len + 2);
g_object_ref(objv[len++] = obj);
objv[len] = NULL;
}
return objv;
}

static
gssize
gutil_objv_find_last_impl(
GObject* const* objv,
GObject* obj,
gsize i /* exclisive */)
{
while (i > 0) {
if (objv[--i] == obj) {
return i;
}
}
return -1;
}

static
GObject**
gutil_objv_remove_impl(
GObject** objv,
gsize pos,
gsize len)
{
g_object_unref(objv[pos]);
memmove(objv + pos, objv + pos + 1, sizeof(GObject*) * (len - pos));
return g_realloc(objv, sizeof(GObject*) * len);
}

GObject**
gutil_objv_remove(
GObject** objv,
GObject* obj,
gboolean all)
{
if (objv && obj) {
const gssize pos = gutil_objv_find(objv, obj);

if (pos >= 0) {
gsize len = gutil_ptrv_length(objv);

objv = gutil_objv_remove_impl(objv, pos, len);
if (all) {
gssize i, l;

len--;
l = len - pos;
while ((i = gutil_objv_find_last_impl(objv + pos,
obj, l)) >= 0) {
objv = gutil_objv_remove_impl(objv, pos + i, len--);
l = i;
}
}
}
}
return objv;
}

GObject**
gutil_objv_remove_at(
GObject** objv,
gsize pos)
{
if (objv) {
const gsize len = gutil_ptrv_length(objv);

if (pos < len) {
objv = gutil_objv_remove_impl(objv, pos, len);
}
}
return objv;
}

GObject*
gutil_objv_at(
GObject* const* objv,
gsize pos)
{
if (objv) {
guint i = 0;

while (objv[i] && i < pos) i++;
if (i == pos) {
/* We also end up here if i == len but that's OK */
return objv[pos];
}
}
return NULL;

}

gboolean
gutil_objv_equal(
GObject* const* v1,
GObject* const* v2)
{
if (v1 == v2) {
return TRUE;
} else if (!v1) {
return !v2[0];
} else if (!v2) {
return !v1[0];
} else {
gsize len = 0;

while (v1[len] && v1[len] == v2[len]) len++;
return !v1[len] && !v2[len];
}
}

GObject*
gutil_objv_first(
GObject* const* objv)
{
return objv ? objv[0] : NULL;
}

GObject*
gutil_objv_last(
GObject* const* objv)
{
if (objv && objv[0]) {
GObject* const* ptr = objv;

while (ptr[1]) ptr++;
return *ptr;
}
return NULL;
}

gssize
gutil_objv_find(
GObject* const* objv,
GObject* obj)
{
if (objv && obj) {
GObject* const* ptr;

for (ptr = objv; *ptr; ptr++) {
if (*ptr == obj) {
return ptr - objv;
}
}
}
return -1;
}

gssize
gutil_objv_find_last(
GObject* const* objv,
GObject* obj)
{
return (objv && obj) ?
gutil_objv_find_last_impl(objv, obj, gutil_ptrv_length(objv)) :
-1;
}

gboolean
gutil_objv_contains(
GObject* const* objv,
GObject* obj)
{
if (objv && obj) {
GObject* const* ptr;

for (ptr = objv; *ptr; ptr++) {
if (*ptr == obj) {
return TRUE;
}
}
}
return FALSE;
}

/*
* Local Variables:
* mode: C
* c-basic-offset: 4
* indent-tabs-mode: nil
* End:
*/
1 change: 1 addition & 0 deletions test/Makefile
Expand Up @@ -11,6 +11,7 @@ all:
@$(MAKE) -C test_ints $*
@$(MAKE) -C test_log $*
@$(MAKE) -C test_misc $*
@$(MAKE) -C test_objv $*
@$(MAKE) -C test_ring $*
@$(MAKE) -C test_strv $*
@$(MAKE) -C test_weakref $*

0 comments on commit 70ac525

Please sign in to comment.