~matttbe/ubuntu/raring/geis/lp1077376

« back to all changes in this revision

Viewing changes to libs/geis-util/geis_bag.h

  • Committer: Package Import Robot
  • Author(s): Chase Douglas
  • Date: 2012-07-30 08:51:42 UTC
  • Revision ID: package-import@ubuntu.com-20120730085142-jrc33ygjvt0ob1wl
Tags: upstream-2.2.11
ImportĀ upstreamĀ versionĀ 2.2.11

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * @file geis_bag.h
 
3
 * @brief generic bag container interface
 
4
 */
 
5
 
 
6
/*
 
7
 * Copyright 2011 Canonical Ltd.
 
8
 *
 
9
 * This program is free software: you can redistribute it and/or modify it 
 
10
 * under the terms of the GNU General Public License version 3, as published 
 
11
 * by the Free Software Foundation.
 
12
 *
 
13
 * This program is distributed in the hope that it will be useful, but 
 
14
 * WITHOUT ANY WARRANTY; without even the implied warranties of 
 
15
 * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR 
 
16
 * PURPOSE.  See the GNU General Public License for more details.
 
17
 *
 
18
 * You should have received a copy of the GNU General Public License along 
 
19
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 
20
 */
 
21
#ifndef GEIS_UTIL_BAG_H
 
22
#define GEIS_UTIL_BAG_H
 
23
 
 
24
#include "geis/geis.h"
 
25
 
 
26
 
 
27
/**
 
28
 * @struct GeisBag
 
29
 * @brief A handle for a generic value-storage bag.
 
30
 * @ingroup geis_util
 
31
 *
 
32
 * A bag is a container that holds zero or more copies of a data structure of
 
33
 * homogeneous type.  It is implemented as a contiguous store of data that grows
 
34
 * when needed using a predetermined growth factor to give amortized constant
 
35
 * insertion and removal rates.
 
36
 *
 
37
 * Uses of this structure should be wrapped by more type-specific functions.
 
38
 *
 
39
 * @typedef GeisBag
 
40
 * @brief opaque pointer to a GeisBag object.
 
41
 */
 
42
typedef struct GeisBag *GeisBag;
 
43
 
 
44
struct GeisBag
 
45
{
 
46
  GeisSize   store_size;           /**< The size of data_store in datum_size
 
47
                                     *  units. */
 
48
  GeisFloat  store_growth_factor;  /**< The storage growth factor. */
 
49
  GeisSize   datum_size;           /**< The number of bytes in each datum. */
 
50
  GeisSize   data_count;           /**< The number of data in data_store. */
 
51
  void      *data_store;           /**< The the data store. */
 
52
};
 
53
 
 
54
 
 
55
static const GeisSize  geis_bag_default_init_alloc    = 2;
 
56
static const GeisFloat geis_bag_default_growth_factor = 1.5f;
 
57
 
 
58
 
 
59
/**
 
60
 * Creates a new, empty bag.
 
61
 * @memberof GeisBag
 
62
 *
 
63
 * @param[in] data_size      The size (in bytes) of the stored datum.
 
64
 * @param[in] init_alloc     The initial number of data to allocate space for.
 
65
 * @param[in] growth_factor  The growth factor for extending the bag.
 
66
 *
 
67
 * Constructs a generic homogenous container that is designed to hold data with
 
68
 * a size of @p data_size (in bytes).  Space for @p init_alloc data is initially
 
69
 * allocated, and subsequent expansions will be done by a factor of
 
70
 * @p growth_factor (taking the ceiling of the current allocation times the @p
 
71
 * growth_factor).
 
72
 *
 
73
 * @returns a new, empty GeisBag on success or a NULL on failure.
 
74
 */
 
75
GeisBag
 
76
geis_bag_new(GeisSize data_size, GeisSize init_alloc, GeisFloat growth_factor);
 
77
 
 
78
/**
 
79
 * Destroys a bag.
 
80
 * @memberof GeisBag
 
81
 *
 
82
 * @param[in]  bag  The bag to destroy.
 
83
 *
 
84
 * Frees all resources used by the bag itself:  does not destroy any store data.
 
85
 */
 
86
void
 
87
geis_bag_delete(GeisBag bag);
 
88
 
 
89
/**
 
90
 * Gets the number of data contained in the bag.
 
91
 * @memberof GeisBag
 
92
 *
 
93
 * @param[in]  bag    The bag.
 
94
 *
 
95
 * @returns the count of the data contained in the bag.
 
96
 */
 
97
GeisSize
 
98
geis_bag_count(GeisBag bag);
 
99
 
 
100
/**
 
101
 * Appends a new datam to the store.
 
102
 * @memberof GeisBag
 
103
 *
 
104
 * @param[in]  bag    The bag to append to.
 
105
 * @param[in]  datum  The datum to append.
 
106
 *
 
107
 * @returns GEIS_STATUS_UNKNOWN_ERROR if @p datum failed to be appended (for
 
108
 * example, memory allocation failure during bag expansion), GEIS_STATUS_SUCCESS
 
109
 * otherwise.
 
110
 */
 
111
GeisStatus
 
112
geis_bag_append(GeisBag bag, void *datum);
 
113
 
 
114
/**
 
115
 * Gets a pointer to a datum by index.
 
116
 * @memberof GeisBag
 
117
 *
 
118
 * @param[in]  bag    The bag.
 
119
 * @param[in]  index  Indicates the datum to retrieve.
 
120
 *
 
121
 * @returns NULL if @p index is out of range, a pointer to the indictaed datum
 
122
 * otherwise.
 
123
 */
 
124
void*
 
125
geis_bag_at(GeisBag bag, GeisSize index);
 
126
 
 
127
/**
 
128
 * Removes an indicated datum from the bag.
 
129
 * @memberof GeisBag
 
130
 *
 
131
 * @param[in]  bag    The bag.
 
132
 * @param[in]  index  Indicates the datum to remove.
 
133
 *
 
134
 * This function removes the datum and if necessary fills in the 'hole' in the
 
135
 * store by moving other data around.  It will invalidate any pointers into the
 
136
 * bag:  avoid using 'em.  Does not call any deleter functions, so plan
 
137
 * accordingly.
 
138
 *
 
139
 * @returns GEIS_STATUS_BAD_ARGUMENT if @p index is out of range,
 
140
 * GEIS_STATUS_SUCCESS otherwise.
 
141
 */
 
142
GeisStatus
 
143
geis_bag_remove(GeisBag bag, GeisSize index);
 
144
 
 
145
#endif /* GEIS_UTIL_BAG_H */