~ubuntu-branches/ubuntu/lucid/igraph/lucid

« back to all changes in this revision

Viewing changes to src/vector.pmt

  • Committer: Bazaar Package Importer
  • Author(s): Mathieu Malaterre
  • Date: 2009-11-16 18:12:42 UTC
  • Revision ID: james.westby@ubuntu.com-20091116181242-mzv9p5fz9uj57xd1
Tags: upstream-0.5.3
ImportĀ upstreamĀ versionĀ 0.5.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- mode: C -*-  */
 
2
/* 
 
3
   IGraph library.
 
4
   Copyright (C) 2003, 2004, 2005  Gabor Csardi <csardi@rmki.kfki.hu>
 
5
   MTA RMKI, Konkoly-Thege Miklos st. 29-33, Budapest 1121, Hungary
 
6
   
 
7
   This program is free software; you can redistribute it and/or modify
 
8
   it under the terms of the GNU General Public License as published by
 
9
   the Free Software Foundation; either version 2 of the License, or
 
10
   (at your option) any later version.
 
11
   
 
12
   This program is distributed in the hope that it will be useful,
 
13
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
   GNU General Public License for more details.
 
16
   
 
17
   You should have received a copy of the GNU General Public License
 
18
   along with this program; if not, write to the Free Software
 
19
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 
 
20
   02110-1301 USA
 
21
 
 
22
*/
 
23
 
 
24
#include "memory.h"
 
25
#include "error.h"
 
26
#include "random.h"
 
27
 
 
28
#include <assert.h>
 
29
#include <string.h>             /* memcpy & co. */
 
30
#include <stdlib.h>
 
31
#include <stdarg.h>             /* va_start & co */
 
32
#include <math.h>
 
33
 
 
34
/**
 
35
 * \ingroup vector
 
36
 * \section about_igraph_vector_t_objects About \type igraph_vector_t objects
 
37
 * 
 
38
 * <para>The \type igraph_vector_t data type is a simple and efficient
 
39
 * interface to arrays containing numbers. It is something
 
40
 * similar as (but much simpler than) the \type vector template
 
41
 * in the C++ standard library.</para>
 
42
 *
 
43
 * <para>Vectors are used extensively in \a igraph, all
 
44
 * functions which expect or return a list of numbers use
 
45
 * igraph_vector_t to achieve this.</para>
 
46
 *
 
47
 * <para>The \type igraph_vector_t type usually uses
 
48
 * O(n) space
 
49
 * to store n elements. Sometimes it
 
50
 * uses more, this is because vectors can shrink, but even if they
 
51
 * shrink, the current implementation does not free a single bit of
 
52
 * memory.</para>
 
53
 * 
 
54
 * <para>The elements in an \type igraph_vector_t
 
55
 * object are indexed from zero, we follow the usual C convention
 
56
 * here.</para>
 
57
 * 
 
58
 * <para>The elements of a vector always occupy a single block of
 
59
 * memory, the starting address of this memory block can be queried
 
60
 * with the \ref VECTOR macro. This way, vector objects can be used
 
61
 * with standard mathematical libraries, like the GNU Scientific
 
62
 * Library.</para>
 
63
 */
 
64
 
 
65
/**
 
66
 * \ingroup vector
 
67
 * \section igraph_vector_constructors_and_destructors Constructors and
 
68
 * Destructors
 
69
 * 
 
70
 * <para>\type igraph_vector_t objects have to be initialized before using
 
71
 * them, this is analogous to calling a constructor on them. There are a
 
72
 * number of \type igraph_vector_t constructors, for your
 
73
 * convenience. \ref igraph_vector_init() is the basic constructor, it
 
74
 * creates a vector of the given length, filled with zeros.
 
75
 * \ref igraph_vector_copy() creates a new identical copy
 
76
 * of an already existing and initialized vector. \ref
 
77
 * igraph_vector_init_copy() creates a vector by copying a regular C array. 
 
78
 * \ref igraph_vector_init_seq() creates a vector containing a regular
 
79
 * sequence with increment one.</para>
 
80
 * 
 
81
 * <para>\ref igraph_vector_view() is a special constructor, it allows you to
 
82
 * handle a regular C array as a \type vector without copying
 
83
 * its elements.
 
84
 * </para> 
 
85
 *
 
86
 * <para>If a \type igraph_vector_t object is not needed any more, it
 
87
 * should be destroyed to free its allocated memory by calling the
 
88
 * \type igraph_vector_t destructor, \ref igraph_vector_destroy().</para>
 
89
 * 
 
90
 * <para> Note that vectors created by \ref igraph_vector_view() are special,
 
91
 * you mustn't call \ref igraph_vector_destroy() on these.</para>
 
92
 */
 
93
 
 
94
/**
 
95
 * \ingroup vector
 
96
 * \function igraph_vector_init
 
97
 * \brief Initializes a vector object (constructor).
 
98
 * 
 
99
 * </para><para>
 
100
 * Every vector needs to be initialized before it can be used, and
 
101
 * there are a number of initialization functions or otherwise called
 
102
 * constructors. 
 
103
 * 
 
104
 * </para><para>
 
105
 * Every vector object initialized by this function should be
 
106
 * destroyed (ie. the memory allocated for it should be freed) when it
 
107
 * is not needed anymore, the \ref igraph_vector_destroy() function is
 
108
 * responsible for this.
 
109
 * \param v Pointer to a not yet initialized vector object.
 
110
 * \param size The size of the vector.
 
111
 * \return error code:
 
112
 *       \c IGRAPH_ENOMEM if there is not enough memory.
 
113
 * 
 
114
 * Time complexity: operating system dependent, the amount of 
 
115
 * \quote time \endquote required to allocate
 
116
 * O(n) elements,
 
117
 * n is the number of elements. 
 
118
 */
 
119
 
 
120
int FUNCTION(igraph_vector,init)      (TYPE(igraph_vector)* v, int long size) { 
 
121
        long int alloc_size= size > 0 ? size : 1;
 
122
        if (size < 0) { size=0; }
 
123
        v->stor_begin=igraph_Calloc(alloc_size, BASE);
 
124
        if (v->stor_begin==0) {
 
125
          IGRAPH_ERROR("cannot init vector", IGRAPH_ENOMEM);
 
126
        }
 
127
        v->stor_end=v->stor_begin + alloc_size;
 
128
        v->end=v->stor_begin+size;
 
129
 
 
130
        return 0;
 
131
}
 
132
 
 
133
/**
 
134
 * \ingroup vector
 
135
 * \function igraph_vector_view
 
136
 * \brief Handle a regular C array as a \type igraph_vector_t.
 
137
 * 
 
138
 * </para><para>
 
139
 * This is a special \type igraph_vector_t constructor. It allows to
 
140
 * handle a regular C array as a \type igraph_vector_t temporarily.
 
141
 * Be sure that you \em don't ever call the destructor (\ref
 
142
 * igraph_vector_destroy()) on objects created by this constructor.
 
143
 * \param v Pointer to an uninitialized \type igraph_vector_t object.
 
144
 * \param data Pointer, the C array.
 
145
 * \param length The length of the C array.
 
146
 * \return Pointer to the vector object, the same as the 
 
147
 *     \p v parameter, for convenience.
 
148
 * 
 
149
 * Time complexity: O(1)
 
150
 */ 
 
151
 
 
152
const TYPE(igraph_vector)*FUNCTION(igraph_vector,view) (const TYPE(igraph_vector) *v,
 
153
                                                        const BASE *data, 
 
154
                                                        long int length) {
 
155
  TYPE(igraph_vector) *v2=(TYPE(igraph_vector)*)v;
 
156
  v2->stor_begin=(BASE*)data;
 
157
  v2->stor_end=(BASE*)data+length;
 
158
  v2->end=v->stor_end;
 
159
  return v;
 
160
}
 
161
 
 
162
/**
 
163
 * \ingroup vector
 
164
 * \function igraph_vector_init_real
 
165
 * \brief Create an \type igraph_vector_t from the parameters.
 
166
 * 
 
167
 * </para><para>
 
168
 * Because of how C and the C library handles variable length argument
 
169
 * lists, it is required that you supply real constants to this
 
170
 * function. This means that
 
171
 * \verbatim igraph_vector_t v;
 
172
 * igraph_vector_init_real(&amp;v, 5, 1,2,3,4,5); \endverbatim
 
173
 * is an error at runtime and the results are undefined. This is
 
174
 * the proper way:
 
175
 * \verbatim igraph_vector_t v;
 
176
 * igraph_vector_init_real(&amp;v, 5, 1.0,2.0,3.0,4.0,5.0); \endverbatim
 
177
 * \param v Pointer to an uninitialized \type igraph_vector_t object.
 
178
 * \param no Positive integer, the number of \type igraph_real_t
 
179
 *    parameters to follow.
 
180
 * \param ... The elements of the vector.
 
181
 * \return Error code, this can be \c IGRAPH_ENOMEM
 
182
 *     if there isn't enough memory to allocate the vector.
 
183
 *
 
184
 * \sa \ref igraph_vector_init_real_end(), \ref igraph_vector_init_int() for similar
 
185
 * functions.
 
186
 *
 
187
 * Time complexity: depends on the time required to allocate memory,
 
188
 * but at least O(n), the number of
 
189
 * elements in the vector.
 
190
 */
 
191
 
 
192
int FUNCTION(igraph_vector,init_real)(TYPE(igraph_vector) *v, int no, ...) {
 
193
  int i=0;
 
194
  va_list ap;
 
195
  IGRAPH_CHECK(FUNCTION(igraph_vector,init)(v, no));
 
196
 
 
197
  va_start(ap, no);
 
198
  for (i=0; i<no; i++) {
 
199
    VECTOR(*v)[i]=(BASE) va_arg(ap, double);
 
200
  }
 
201
  va_end(ap);
 
202
  
 
203
  return 0;
 
204
}
 
205
 
 
206
/**
 
207
 * \ingroup vector
 
208
 * \function igraph_vector_init_real_end
 
209
 * \brief Create an \type igraph_vector_t from the parameters.
 
210
 * 
 
211
 * </para><para>
 
212
 * This constructor is similar to \ref igraph_vector_init_real(), the only
 
213
 * difference is that instead of giving the number of elements in the
 
214
 * vector, a special marker element follows the last real vector
 
215
 * element.
 
216
 * \param v Pointer to an uninitialized \type igraph_vector_t object.
 
217
 * \param endmark This element will signal the end of the vector. It
 
218
 *    will \em not be part of the vector.
 
219
 * \param ... The elements of the vector.
 
220
 * \return Error code, \c IGRAPH_ENOMEM if there
 
221
 *    isn't enough memory.
 
222
 * 
 
223
 * \sa \ref igraph_vector_init_real() and \ref igraph_vector_init_int_end() for
 
224
 * similar functions.
 
225
 * 
 
226
 * Time complexity: at least O(n) for 
 
227
 * n elements plus the time
 
228
 * complexity of the memory allocation.
 
229
 */
 
230
 
 
231
int FUNCTION(igraph_vector,init_real_end)(TYPE(igraph_vector) *v, 
 
232
                                          BASE endmark, ...) {
 
233
  int i=0, n=0;
 
234
  va_list ap;
 
235
 
 
236
  va_start(ap, endmark);
 
237
  while (1) {
 
238
    BASE num = va_arg(ap, double);
 
239
    if (num == endmark) {
 
240
      break;
 
241
    }
 
242
    n++;
 
243
  }
 
244
  va_end(ap);
 
245
 
 
246
  IGRAPH_CHECK(FUNCTION(igraph_vector,init)(v,n));
 
247
  IGRAPH_FINALLY(FUNCTION(igraph_vector,destroy), v);
 
248
  
 
249
  va_start(ap, endmark);
 
250
  for (i=0; i<n; i++) {
 
251
    VECTOR(*v)[i]=(BASE) va_arg(ap, double);
 
252
  }
 
253
  va_end(ap);
 
254
  
 
255
  IGRAPH_FINALLY_CLEAN(1);
 
256
  return 0;
 
257
}
 
258
 
 
259
/**
 
260
 * \ingroup vector
 
261
 * \function igraph_vector_init_int
 
262
 * \brief Create an \type igraph_vector_t containing the parameters.
 
263
 * 
 
264
 * </para><para>
 
265
 * This function is similar to \ref igraph_vector_init_real(), but it expects 
 
266
 * \type int parameters. It is important that all parameters
 
267
 * should be of this type, otherwise the result of the function call
 
268
 * is undefined.
 
269
 * \param v Pointer to an uninitialized \type igraph_vector_t object.
 
270
 * \param no The number of \type int parameters to follow.
 
271
 * \param ... The elements of the vector.
 
272
 * \return Error code, \c IGRAPH_ENOMEM if there is
 
273
 *    not enough memory.
 
274
 * \sa \ref igraph_vector_init_real() and igraph_vector_init_int_end(), these are
 
275
 *    similar functions.
 
276
 *
 
277
 * Time complexity: at least O(n) for 
 
278
 * n elements plus the time
 
279
 * complexity of the memory allocation.
 
280
 */
 
281
 
 
282
int FUNCTION(igraph_vector,init_int)(TYPE(igraph_vector) *v, int no, ...) {
 
283
  int i=0;
 
284
  va_list ap;
 
285
  IGRAPH_CHECK(FUNCTION(igraph_vector,init)(v, no));
 
286
 
 
287
  va_start(ap, no);
 
288
  for (i=0; i<no; i++) {
 
289
    VECTOR(*v)[i]=(BASE) va_arg(ap, int);
 
290
  }
 
291
  va_end(ap);
 
292
  
 
293
  return 0;
 
294
}
 
295
 
 
296
/**
 
297
 * \ingroup vector
 
298
 * \function igraph_vector_init_int_end
 
299
 * \brief Create an \type igraph_vector_t from the parameters.
 
300
 * 
 
301
 * </para><para>
 
302
 * This constructor is similar to \ref igraph_vector_init_int(), the only
 
303
 * difference is that instead of giving the number of elements in the
 
304
 * vector, a special marker element follows the last real vector
 
305
 * element.
 
306
 * \param v Pointer to an uninitialized \type igraph_vector_t object.
 
307
 * \param endmark This element will signal the end of the vector. It
 
308
 *    will \em not be part of the vector.
 
309
 * \param ... The elements of the vector.
 
310
 * \return Error code, \c IGRAPH_ENOMEM if there
 
311
 *    isn't enough memory.
 
312
 * 
 
313
 * \sa \ref igraph_vector_init_int() and \ref igraph_vector_init_real_end() for
 
314
 * similar functions.
 
315
 *
 
316
 * Time complexity: at least O(n) for 
 
317
 * n elements plus the time
 
318
 * complexity of the memory allocation.
 
319
 */
 
320
 
 
321
int FUNCTION(igraph_vector_init,int_end)(TYPE(igraph_vector) *v, int endmark, ...) {
 
322
  int i=0, n=0;
 
323
  va_list ap;
 
324
 
 
325
  va_start(ap, endmark);
 
326
  while (1) {
 
327
    int num = va_arg(ap, int);
 
328
    if (num == endmark) {
 
329
      break;
 
330
    }
 
331
    n++;
 
332
  }
 
333
  va_end(ap);
 
334
 
 
335
  IGRAPH_CHECK(FUNCTION(igraph_vector,init)(v, n));
 
336
  IGRAPH_FINALLY(FUNCTION(igraph_vector,destroy), v);
 
337
  
 
338
  va_start(ap, endmark);
 
339
  for (i=0; i<n; i++) {
 
340
    VECTOR(*v)[i]=(BASE) va_arg(ap, int);
 
341
  }
 
342
  va_end(ap);
 
343
  
 
344
  IGRAPH_FINALLY_CLEAN(1);
 
345
  return 0;
 
346
}
 
347
 
 
348
/**
 
349
 * \ingroup vector
 
350
 * \function igraph_vector_destroy
 
351
 * \brief Destroys a vector object.
 
352
 *
 
353
 * </para><para>
 
354
 * All vectors initialized by \ref igraph_vector_init() should be properly
 
355
 * destroyed by this function. A destroyed vector needs to be
 
356
 * reinitialized by \ref igraph_vector_init(), \ref igraph_vector_init_copy() or
 
357
 * another constructor.
 
358
 * \param v Pointer to the (previously initialized) vector object to
 
359
 *        destroy. 
 
360
 *
 
361
 * Time complexity: operating system dependent.
 
362
 */
 
363
 
 
364
void FUNCTION(igraph_vector,destroy)   (TYPE(igraph_vector)* v) {
 
365
  assert(v != 0);
 
366
  if (v->stor_begin != 0) {
 
367
    igraph_Free(v->stor_begin);
 
368
    v->stor_begin = NULL;
 
369
  }
 
370
}
 
371
 
 
372
/**
 
373
 * \ingroup vector
 
374
 * \function igraph_vector_reserve
 
375
 * \brief Reserves memory for a vector.
 
376
 * 
 
377
 * </para><para>
 
378
 * \a igraph vectors are flexible, they can grow and
 
379
 * shrink. Growing 
 
380
 * however occasionally needs the data in the vector to be copyed.
 
381
 * In order to avoid you can call this function to reserve space for
 
382
 * future growth of the vector. 
 
383
 * 
 
384
 * </para><para>
 
385
 * Note that this function does \em not change the size of the
 
386
 * vector. Let us see a small example to clarify things: if you
 
387
 * reserve space for 100 elements and the size of your
 
388
 * vector was (and still is) 60, then you can surely add additional 40
 
389
 * elements to your vector before it will be copied.
 
390
 * \param v The vector object.
 
391
 * \param size The new \em allocated size of the vector.
 
392
 * \return Error code:
 
393
 *         \c IGRPAH_ENOMEM if there is not enough memory.
 
394
 *
 
395
 * Time complexity: operating system dependent, should be around
 
396
 * O(n), n 
 
397
 * is the new allocated size of the vector.
 
398
 */
 
399
 
 
400
int FUNCTION(igraph_vector,reserve)   (TYPE(igraph_vector)* v, long int size) {
 
401
        long int actual_size=FUNCTION(igraph_vector,size)(v);
 
402
        BASE *tmp;
 
403
        assert(v != NULL);
 
404
        assert(v->stor_begin != NULL);
 
405
        if (size <= FUNCTION(igraph_vector,size)(v)) { return 0; }
 
406
 
 
407
        tmp=igraph_Realloc(v->stor_begin, size, BASE);
 
408
        if (tmp==0) {
 
409
          IGRAPH_ERROR("cannot reserve space for vector", IGRAPH_ENOMEM);
 
410
        }
 
411
        v->stor_begin=tmp;
 
412
        v->stor_end=v->stor_begin + size;
 
413
        v->end=v->stor_begin+actual_size;
 
414
        
 
415
        return 0;
 
416
}
 
417
 
 
418
/**
 
419
 * \ingroup vector
 
420
 * \function igraph_vector_empty
 
421
 * \brief Decides whether the size of the vector is zero.
 
422
 *
 
423
 * \param v The vector object.
 
424
 * \return Non-zero number if the size of the vector is not zero and
 
425
 *         zero otherwise.
 
426
 * 
 
427
 * Time complexity: O(1).
 
428
 */
 
429
 
 
430
igraph_bool_t FUNCTION(igraph_vector,empty)     (const TYPE(igraph_vector)* v) {
 
431
        assert(v != NULL);
 
432
        assert(v->stor_begin != NULL);
 
433
        return v->stor_begin == v->end;
 
434
}
 
435
 
 
436
/**
 
437
 * \ingroup vector
 
438
 * \function igraph_vector_size
 
439
 * \brief Gives the size (=length) of the vector.
 
440
 * 
 
441
 * \param v The vector object
 
442
 * \return The size of the vector.
 
443
 *
 
444
 * Time complexity: O(1). 
 
445
 */
 
446
 
 
447
long int FUNCTION(igraph_vector,size)      (const TYPE(igraph_vector)* v) {
 
448
        assert(v != NULL);
 
449
        assert(v->stor_begin != NULL);
 
450
        return v->end - v->stor_begin;
 
451
}
 
452
 
 
453
/**
 
454
 * \ingroup vector
 
455
 * \function igraph_vector_clear
 
456
 * \brief Removes all elements from a vector.
 
457
 * 
 
458
 * </para><para>
 
459
 * This function simply sets the size of the vector to zero, it does
 
460
 * not free any allocated memory. For that you have to call
 
461
 * \ref igraph_vector_destroy().
 
462
 * \param v The vector object.
 
463
 * 
 
464
 * Time complexity: O(1).
 
465
 */
 
466
 
 
467
void FUNCTION(igraph_vector,clear)     (TYPE(igraph_vector)* v) {
 
468
        assert(v != NULL);
 
469
        assert(v->stor_begin != NULL);
 
470
        v->end = v->stor_begin;
 
471
}
 
472
 
 
473
/**
 
474
 * \ingroup vector
 
475
 * \function igraph_vector_push_back
 
476
 * \brief Appends one element to a vector.
 
477
 * 
 
478
 * </para><para>
 
479
 * This function resizes the vector to be one element longer and
 
480
 * sets the very last element in the vector to \p e.
 
481
 * \param v The vector object.
 
482
 * \param e The element to append to the vector.
 
483
 * \return Error code:
 
484
 *         \c IGRAPH_ENOMEM: not enough memory.
 
485
 * 
 
486
 * Time complexity: operating system dependent. What is important that
 
487
 * a sequence of n
 
488
 * subsequent calls to this function has time complexity
 
489
 * O(n), even if there 
 
490
 * hadn't been any space reserved for the new elements by
 
491
 * \ref igraph_vector_reserve(). This is implemented by a trick similar to the C++
 
492
 * \type vector class: each time more memory is allocated for a
 
493
 * vector, the size of the additionally allocated memory is the same
 
494
 * as the vector's current length. (We assume here that the time
 
495
 * complexity of memory allocation is at most linear.)
 
496
 */
 
497
 
 
498
int FUNCTION(igraph_vector,push_back) (TYPE(igraph_vector)* v, BASE e) {
 
499
        assert(v != NULL);
 
500
        assert(v->stor_begin != NULL);
 
501
        
 
502
        /* full, allocate more storage */
 
503
        if (v->stor_end == v->end) {
 
504
                long int new_size = FUNCTION(igraph_vector,size)(v) * 2;
 
505
                if (new_size == 0) { new_size = 1; }
 
506
                IGRAPH_CHECK(FUNCTION(igraph_vector,reserve)(v, new_size));
 
507
        }
 
508
        
 
509
        *(v->end) = e;
 
510
        v->end += 1;
 
511
        
 
512
        return 0;
 
513
}
 
514
 
 
515
/**
 
516
 * \ingroup vector
 
517
 * \function igraph_vector_insert
 
518
 * \brief Inserts a single element into a vector.
 
519
 *
 
520
 * Note that this function does not do range checking. Insertion will shift the
 
521
 * elements from the position given to the end of the vector one position to the
 
522
 * right, and the new element will be inserted in the empty space created at
 
523
 * the given position. The size of the vector will increase by one.
 
524
 *
 
525
 * \param v The vector object.
 
526
 * \param pos The position where the new element is inserted.
 
527
 */
 
528
int FUNCTION(igraph_vector,insert)(TYPE(igraph_vector) *v, long int pos,
 
529
                                   BASE value) {
 
530
  long int size = FUNCTION(igraph_vector,size)(v);
 
531
  IGRAPH_CHECK(FUNCTION(igraph_vector,resize)(v, size+1));
 
532
  if (pos<size) {
 
533
    memmove(v->stor_begin+pos+1, v->stor_begin+pos, 
 
534
            sizeof(BASE)*(size-pos));
 
535
  }
 
536
  v->stor_begin[pos] = value;
 
537
  return 0;
 
538
}
 
539
 
 
540
/**
 
541
 * \ingroup vector
 
542
 * \section igraph_vector_accessing_elements Accessing elements
 
543
 * 
 
544
 * <para>The simplest way to access an element of a vector is to use the
 
545
 * \ref VECTOR macro. This macro can be used both for querying and setting
 
546
 * \type igraph_vector_t elements. If you need a function, \ref
 
547
 * igraph_vector_e() queries and \ref igraph_vector_set() sets an element of a
 
548
 * vector. \ref igraph_vector_e_ptr() returns the address of an element.</para>
 
549
 * 
 
550
 * <para>\ref igraph_vector_tail() returns the last element of a non-empty
 
551
 * vector. There is no <function>igraph_vector_head()</function> function
 
552
 * however, as it is easy to write <code>VECTOR(v)[0]</code>
 
553
 * instead.</para>
 
554
 */
 
555
 
 
556
/**
 
557
 * \ingroup vector
 
558
 * \function igraph_vector_e
 
559
 * \brief Access an element of a vector.
 
560
 * \param v The \type igraph_vector_t object.
 
561
 * \param pos The position of the element, the index of the first
 
562
 *    element is zero.
 
563
 * \return The desired element.
 
564
 * \sa \ref igraph_vector_e_ptr() and the \ref VECTOR macro.
 
565
 * 
 
566
 * Time complexity: O(1).
 
567
 */
 
568
 
 
569
BASE FUNCTION(igraph_vector,e)         (const TYPE(igraph_vector)* v, long int pos) {
 
570
        assert(v != NULL);
 
571
        assert(v->stor_begin != NULL);
 
572
        return * (v->stor_begin + pos);
 
573
}
 
574
 
 
575
/**
 
576
 * \ingroup vector
 
577
 * \function igraph_vector_e_ptr
 
578
 * \brief Get the address of an element of a vector
 
579
 * \param v The \type igraph_vector_t object.
 
580
 * \param pos The position of the element, the position of the first
 
581
 *   element is zero.
 
582
 * \return Pointer to the desired element.
 
583
 * \sa \ref igraph_vector_e() and the \ref VECTOR macro.
 
584
 * 
 
585
 * Time complexity: O(1).
 
586
 */
 
587
 
 
588
BASE* FUNCTION(igraph_vector,e_ptr)  (const TYPE(igraph_vector)* v, long int pos) {
 
589
  assert(v!=NULL);
 
590
  assert(v->stor_begin != NULL);
 
591
  return v->stor_begin+pos;
 
592
}
 
593
 
 
594
/**
 
595
 * \ingroup vector
 
596
 * \function igraph_vector_set
 
597
 * \brief Assignment to an element of a vector.
 
598
 * \param v The \type igraph_vector_t element.
 
599
 * \param pos Position of the element to set.
 
600
 * \param value New value of the element.
 
601
 * \sa \ref igraph_vector_e().
 
602
 */
 
603
 
 
604
void FUNCTION(igraph_vector,set)       (TYPE(igraph_vector)* v, 
 
605
                                        long int pos, BASE value) {
 
606
        assert(v != NULL);
 
607
        assert(v->stor_begin != NULL);  
 
608
        *(v->stor_begin + pos) = value;
 
609
}
 
610
 
 
611
/**
 
612
 * \ingroup vector
 
613
 * \function igraph_vector_null
 
614
 * \brief Sets each element in the vector to zero.
 
615
 * 
 
616
 * </para><para>
 
617
 * Note that \ref igraph_vector_init() sets the elements to zero as well, so
 
618
 * it makes no sense to call this function on a just initialized
 
619
 * vector. 
 
620
 * \param v The vector object.
 
621
 *
 
622
 * Time complexity: O(n), the size of
 
623
 * the vector. 
 
624
 */
 
625
 
 
626
void FUNCTION(igraph_vector,null)      (TYPE(igraph_vector)* v) {
 
627
        assert(v != NULL);
 
628
        assert(v->stor_begin != NULL);
 
629
        if (FUNCTION(igraph_vector,size)(v)>0) {
 
630
                memset(v->stor_begin, 0, 
 
631
                       sizeof(BASE)*FUNCTION(igraph_vector,size)(v));
 
632
        }
 
633
}
 
634
 
 
635
/**
 
636
 * \function igraph_vector_fill
 
637
 * \brief Fill a vector with a constant element
 
638
 * 
 
639
 * Sets each element of the vector to the supplied constant.
 
640
 * \param vector The vector to work on.
 
641
 * \param e The element to fill with.
 
642
 * 
 
643
 * Time complexity: O(n), the size of the vector.
 
644
 */
 
645
 
 
646
void FUNCTION(igraph_vector,fill)      (TYPE(igraph_vector)* v, BASE e) {
 
647
  BASE *ptr;
 
648
  assert(v != NULL);
 
649
  assert(v->stor_begin != NULL);
 
650
  for (ptr = v->stor_begin; ptr < v->end; ptr++) {
 
651
    *ptr = e;
 
652
  }  
 
653
}  
 
654
 
 
655
/**
 
656
 * \ingroup vector
 
657
 * \function igraph_vector_tail
 
658
 * \brief Returns the last element in a vector.
 
659
 *
 
660
 * </para><para>
 
661
 * It is an error to call this function on an empty vector, the result
 
662
 * is undefined.
 
663
 * \param v The vector object.
 
664
 * \return The last element.
 
665
 * 
 
666
 * Time complexity: O(1).
 
667
 */
 
668
 
 
669
BASE FUNCTION(igraph_vector,tail)(const TYPE(igraph_vector) *v) {
 
670
  assert(v!=NULL);
 
671
  assert(v->stor_begin != NULL);
 
672
  return *((v->end)-1);
 
673
}
 
674
 
 
675
/**
 
676
 * \ingroup vector
 
677
 * \function igraph_vector_pop_back
 
678
 * \brief Removes and returns the last element of a vector.
 
679
 *
 
680
 * </para><para>
 
681
 * It is an error to call this function with an empty vector.
 
682
 * \param v The vector object.
 
683
 * \return The removed last element.
 
684
 * 
 
685
 * Time complexity: O(1).
 
686
 */
 
687
 
 
688
BASE FUNCTION(igraph_vector,pop_back)(TYPE(igraph_vector)* v) {
 
689
  BASE tmp;
 
690
  assert(v!=NULL);
 
691
  assert(v->stor_begin != NULL);
 
692
  assert(v->end != v->stor_begin);
 
693
  tmp=FUNCTION(igraph_vector,e)(v, FUNCTION(igraph_vector,size)(v)-1);
 
694
  v->end -= 1;
 
695
  return tmp;
 
696
}
 
697
 
 
698
/**
 
699
 * \ingroup vector
 
700
 * \function igraph_vector_sort_cmp
 
701
 * \brief Internal comparision function of vector elements, used by 
 
702
 * \ref igraph_vector_sort().
 
703
 */
 
704
 
 
705
int FUNCTION(igraph_vector,sort_cmp)(const void *a, const void *b) {
 
706
  const BASE *da = (const BASE *) a;
 
707
  const BASE *db = (const BASE *) b;
 
708
 
 
709
  return (*da > *db) - (*da < *db);
 
710
}
 
711
 
 
712
/**
 
713
 * \ingroup vector
 
714
 * \function igraph_vector_sort
 
715
 * \brief Sorts the elements of the vector into ascending order.
 
716
 * 
 
717
 * </para><para>
 
718
 * This function uses the built-in sort function of the C library.
 
719
 * \param v Pointer to an initialized vector object.
 
720
 *
 
721
 * Time complexity: should be
 
722
 * O(nlogn) for
 
723
 * n 
 
724
 * elements.
 
725
 */
 
726
 
 
727
void FUNCTION(igraph_vector,sort)(TYPE(igraph_vector) *v) {
 
728
  assert(v != NULL);
 
729
  assert(v->stor_begin != NULL);
 
730
  qsort(v->stor_begin, FUNCTION(igraph_vector,size)(v), 
 
731
    sizeof(BASE), FUNCTION(igraph_vector,sort_cmp));
 
732
}
 
733
 
 
734
/**
 
735
 * \ingroup vector
 
736
 * \function igraph_vector_resize
 
737
 * \brief Resize the vector.
 
738
 *
 
739
 * </para><para>
 
740
 * Note that this function does not free any memory, just sets the
 
741
 * size of the vector to the given one. It can on the other hand 
 
742
 * allocate more memory if the new size is larger than the previous
 
743
 * one. In this case the newly appeared elements in the vector are
 
744
 * \em not set to zero, they are uninitialized.
 
745
 * \param v The vector object
 
746
 * \param newsize The new size of the vector.
 
747
 * \return Error code, 
 
748
 *         \c IGRAPH_ENOMEM if there is not enough
 
749
 *         memory. Note that this function \em never returns an error
 
750
 *         if the vector is made smaller.
 
751
 * \sa \ref igraph_vector_reserve() for allocating memory for future
 
752
 * extensions of a vector.
 
753
 * 
 
754
 * Time complexity: O(1) if the new
 
755
 * size is smaller, operating system dependent if it is larger. In the
 
756
 * latter case it is usually around
 
757
 * O(n),
 
758
 * n is the new size of the vector. 
 
759
 */
 
760
 
 
761
int FUNCTION(igraph_vector,resize)(TYPE(igraph_vector)* v, long int newsize) {
 
762
  assert(v != NULL);
 
763
  assert(v->stor_begin != NULL);
 
764
  IGRAPH_CHECK(FUNCTION(igraph_vector,reserve)(v, newsize));
 
765
  v->end = v->stor_begin+newsize;
 
766
  return 0;
 
767
}
 
768
 
 
769
/**
 
770
 * \ingroup vector
 
771
 * \function igraph_vector_max
 
772
 * \brief Gives the maximum element of the vector.
 
773
 *
 
774
 * </para><para>
 
775
 * If the size of the vector is zero, an arbitrary number is
 
776
 * returned.
 
777
 * \param v The vector object.
 
778
 * \return The maximum element.
 
779
 *
 
780
 * Time complexity: O(n),
 
781
 * n is the size of the vector. 
 
782
 */
 
783
 
 
784
BASE FUNCTION(igraph_vector,max)(const TYPE(igraph_vector)* v) {
 
785
  BASE max;
 
786
  BASE *ptr;
 
787
  assert(v != NULL);
 
788
  assert(v->stor_begin != NULL);
 
789
  max=*(v->stor_begin);
 
790
  ptr=v->stor_begin+1;
 
791
  while (ptr < v->end) {
 
792
    if ((*ptr) > max) {
 
793
      max=*ptr;
 
794
    }
 
795
    ptr++;
 
796
  }
 
797
  return max;
 
798
}
 
799
 
 
800
/**
 
801
 * \ingroup vector
 
802
 * \function igraph_vector_which_max
 
803
 * \brief Gives the position of the maximum element of the vector.
 
804
 *
 
805
 * </para><para>
 
806
 * If the size of the vector is zero, -1 is 
 
807
 * returned.
 
808
 * \param v The vector object.
 
809
 * \return The position of the first maximum element.
 
810
 *
 
811
 * Time complexity: O(n),
 
812
 * n is the size of the vector. 
 
813
 */
 
814
 
 
815
long int FUNCTION(igraph_vector,which_max)(const TYPE(igraph_vector)* v) {
 
816
  long int which=-1;
 
817
  if (!FUNCTION(igraph_vector,empty)(v)) {
 
818
    BASE max;
 
819
    BASE *ptr;
 
820
    long int pos;
 
821
    assert(v != NULL);
 
822
    assert(v->stor_begin != NULL);
 
823
    max=*(v->stor_begin); which=0;
 
824
    ptr=v->stor_begin+1; pos=1;
 
825
    while (ptr < v->end) {
 
826
      if ((*ptr) > max) {
 
827
        max=*ptr;
 
828
        which=pos;
 
829
      }
 
830
      ptr++; pos++;
 
831
    }
 
832
  }
 
833
  return which;
 
834
}
 
835
 
 
836
/**
 
837
 * \function igraph_vector_min
 
838
 * \brief Smallest element of a vector.
 
839
 * 
 
840
 * The vector must be non-empty.
 
841
 * \param v The input vector.
 
842
 * \return The smallest element of \p v.
 
843
 * 
 
844
 * Time complexity: O(n), the number of elements.
 
845
 */
 
846
 
 
847
BASE FUNCTION(igraph_vector,min)(const TYPE(igraph_vector)* v) {
 
848
  BASE min;
 
849
  BASE *ptr;
 
850
  assert(v != NULL);
 
851
  assert(v->stor_begin != NULL);
 
852
  min=*(v->stor_begin);
 
853
  ptr=v->stor_begin+1;
 
854
  while (ptr < v->end) {
 
855
    if ((*ptr) < min) {
 
856
      min=*ptr;
 
857
    }
 
858
    ptr++;
 
859
  }
 
860
  return min;
 
861
}
 
862
 
 
863
/**
 
864
 * \function igraph_vector_which_min
 
865
 * \brief Index of the smallest element.
 
866
 * 
 
867
 * The vector must be non-empty.
 
868
 * If the smallest element is not unique, then the index of the first
 
869
 * is returned.
 
870
 * \param v The input vector.
 
871
 * \return Index of the smallest element.
 
872
 * 
 
873
 * Time complexity: O(n), the number of elements.
 
874
 */
 
875
 
 
876
long int FUNCTION(igraph_vector,which_min)(const TYPE(igraph_vector)* v) {
 
877
  long int which=-1;
 
878
  if (!FUNCTION(igraph_vector,empty)(v)) {
 
879
    BASE min;
 
880
    BASE *ptr;
 
881
    long int pos;
 
882
    assert(v != NULL);
 
883
    assert(v->stor_begin != NULL);
 
884
    min=*(v->stor_begin); which=0;
 
885
    ptr=v->stor_begin+1; pos=1;
 
886
    while (ptr < v->end) {
 
887
      if ((*ptr) < min) {
 
888
        min=*ptr;
 
889
        which=pos;
 
890
      }
 
891
      ptr++; pos++;
 
892
    }
 
893
  }
 
894
  return which;
 
895
}
 
896
 
 
897
/**
 
898
 * \ingroup vector
 
899
 * \function igraph_vector_init_copy
 
900
 * \brief Initializes a vector from an ordinary C array (constructor).
 
901
 * 
 
902
 * \param v Pointer to an uninitialized vector object.
 
903
 * \param data A regular C array.
 
904
 * \param length The length of the C array.
 
905
 * \return Error code: 
 
906
 *         \c IGRAPH_ENOMEM if there is not enough memory.
 
907
 * 
 
908
 * Time complexity: operating system specific, usually
 
909
 * O(\p length).
 
910
 */
 
911
 
 
912
int FUNCTION(igraph_vector,init_copy)(TYPE(igraph_vector) *v, 
 
913
                                      BASE *data, long int length) {
 
914
  v->stor_begin=igraph_Calloc(length, BASE);
 
915
  if (v->stor_begin==0) {
 
916
    IGRAPH_ERROR("cannot init vector from array", IGRAPH_ENOMEM);
 
917
  }
 
918
  v->stor_end=v->stor_begin+length;
 
919
  v->end=v->stor_end;
 
920
  memcpy(v->stor_begin, data, length*sizeof(BASE));
 
921
  
 
922
  return 0;
 
923
}
 
924
 
 
925
/**
 
926
 * \ingroup vector
 
927
 * \function igraph_vector_copy_to
 
928
 * \brief Copies the contents of a vector to a C array.
 
929
 * 
 
930
 * </para><para>
 
931
 * The C array should have sufficient length.
 
932
 * \param v The vector object.
 
933
 * \param to The C array.
 
934
 * 
 
935
 * Time complexity: O(n),
 
936
 * n is the size of the vector.
 
937
 */
 
938
 
 
939
void FUNCTION(igraph_vector,copy_to)(const TYPE(igraph_vector) *v, BASE *to) {
 
940
                                     
 
941
  assert(v != NULL);
 
942
  assert(v->stor_begin != NULL);
 
943
  if (v->end != v->stor_begin) {
 
944
    memcpy(to, v->stor_begin, sizeof(BASE) * (v->end - v->stor_begin));
 
945
  }
 
946
}
 
947
 
 
948
/**
 
949
 * \ingroup vector
 
950
 * \function igraph_vector_copy
 
951
 * \brief Initializes a vector from another vector object (constructor).
 
952
 * 
 
953
 * </para><para>
 
954
 * The contents of the existing vector object will be copied to
 
955
 * the new one.
 
956
 * \param to Pointer to a not yet initialized vector object.
 
957
 * \param from The original vector object to copy.
 
958
 * \return Error code:
 
959
 *         \c IGRAPH_ENOMEM if there is not enough memory.
 
960
 * 
 
961
 * Time complexity: operating system dependent, usually
 
962
 * O(n),
 
963
 * n is the size of the vector. 
 
964
 */
 
965
 
 
966
int FUNCTION(igraph_vector,copy)(TYPE(igraph_vector) *to, 
 
967
                                 const TYPE(igraph_vector) *from) {
 
968
  assert(from != NULL);
 
969
  assert(from->stor_begin != NULL);
 
970
  to->stor_begin=igraph_Calloc(FUNCTION(igraph_vector,size)(from), BASE);
 
971
  if (to->stor_begin==0) {
 
972
    IGRAPH_ERROR("canot copy vector", IGRAPH_ENOMEM);
 
973
  }
 
974
  to->stor_end=to->stor_begin+FUNCTION(igraph_vector,size)(from);
 
975
  to->end=to->stor_end;
 
976
  memcpy(to->stor_begin, from->stor_begin, 
 
977
         FUNCTION(igraph_vector,size)(from)*sizeof(BASE));
 
978
  
 
979
  return 0;
 
980
}
 
981
 
 
982
/**
 
983
 * \ingroup vector
 
984
 * \function igraph_vector_sum
 
985
 * \brief Calculates the sum of the elements in the vector.
 
986
 *
 
987
 * </para><para>
 
988
 * For the empty vector 0.0 is returned.
 
989
 * \param v The vector object.
 
990
 * \return The sum of the elements.
 
991
 * 
 
992
 * Time complexity: O(n), the size of
 
993
 * the vector. 
 
994
 */
 
995
 
 
996
BASE FUNCTION(igraph_vector,sum)(const TYPE(igraph_vector) *v) {
 
997
  BASE res=0;
 
998
  BASE *p;
 
999
  assert(v != NULL);
 
1000
  assert(v->stor_begin != NULL);
 
1001
  for (p=v->stor_begin; p<v->end; p++) {
 
1002
    res += *p;
 
1003
  }
 
1004
  return res;
 
1005
}
 
1006
 
 
1007
/**
 
1008
 * \ingroup vector
 
1009
 * \function igraph_vector_prod
 
1010
 * \brief Calculates the product of the elements in the vector.
 
1011
 * 
 
1012
 * </para><para>
 
1013
 * For the empty vector one (1) is returned.
 
1014
 * \param v The vector object.
 
1015
 * \return The product of the elements.
 
1016
 * 
 
1017
 * Time complexity: O(n), the size of
 
1018
 * the vector. 
 
1019
 */
 
1020
 
 
1021
BASE FUNCTION(igraph_vector,prod)(const TYPE(igraph_vector) *v) {
 
1022
  BASE res=1;
 
1023
  BASE *p;
 
1024
  assert(v != NULL);
 
1025
  assert(v->stor_begin != NULL);
 
1026
  for (p=v->stor_begin; p<v->end; p++) {
 
1027
    res *= *p;
 
1028
  }
 
1029
  return res;
 
1030
}
 
1031
 
 
1032
/**
 
1033
 * \ingroup vector
 
1034
 * \function igraph_vector_init_seq
 
1035
 * \brief Initializes a vector with a sequence.
 
1036
 * 
 
1037
 * </para><para>
 
1038
 * The vector will contain the numbers \p from,
 
1039
 * \p from+1, ..., \p to.
 
1040
 * \param v Pointer to an uninitialized vector object.
 
1041
 * \param from The lower limit in the sequence (inclusive).
 
1042
 * \param to The upper limit in the sequence (inclusive).
 
1043
 * \return Error code:
 
1044
 *         \c IGRAPH_ENOMEM: out of memory.
 
1045
 *
 
1046
 * Time complexity: O(n), the number
 
1047
 * of elements in the vector. 
 
1048
 */
 
1049
 
 
1050
int FUNCTION(igraph_vector,init_seq)(TYPE(igraph_vector) *v, 
 
1051
                                     BASE from, BASE to) {
 
1052
  BASE *p;
 
1053
  IGRAPH_CHECK(FUNCTION(igraph_vector,init)(v, to-from+1));
 
1054
 
 
1055
  for (p=v->stor_begin; p<v->end; p++) {
 
1056
    *p = from++;
 
1057
  }
 
1058
  
 
1059
  return 0;
 
1060
}
 
1061
 
 
1062
/**
 
1063
 * \ingroup vector
 
1064
 * \function igraph_vector_remove_section
 
1065
 * \brief Deletes a section from a vector.
 
1066
 * 
 
1067
 * </para><para>
 
1068
 * Note that this function does not do range checking. The result is
 
1069
 * undefined if you supply invalid limits.
 
1070
 * \param v The vector object.
 
1071
 * \param from The position of the first element to remove.
 
1072
 * \param to The position of the first element \em not to remove.
 
1073
 *
 
1074
 * Time complexity: O(n-from),
 
1075
 * n is the number of elements in the
 
1076
 * vector. 
 
1077
 */
 
1078
 
 
1079
void FUNCTION(igraph_vector,remove_section)(TYPE(igraph_vector) *v, 
 
1080
                                            long int from, long int to) {
 
1081
  assert(v != NULL);
 
1082
  assert(v->stor_begin != NULL);
 
1083
  memmove(v->stor_begin+from, v->stor_begin+to,
 
1084
          sizeof(BASE)*(v->end-v->stor_begin-to));
 
1085
  v->end -= (to-from);
 
1086
}
 
1087
 
 
1088
/**
 
1089
 * \ingroup vector
 
1090
 * \function igraph_vector_remove
 
1091
 * \brief Removes a single element from a vector.
 
1092
 *
 
1093
 * Note that this function does not do range checking.
 
1094
 * \param v The vector object.
 
1095
 * \param elem The position of the element to remove.
 
1096
 * 
 
1097
 * Time complexity: O(n-elem),
 
1098
 * n is the number of elements in the
 
1099
 * vector. 
 
1100
 */
 
1101
 
 
1102
void FUNCTION(igraph_vector,remove)(TYPE(igraph_vector) *v, long int elem) {
 
1103
  assert(v != NULL);
 
1104
  assert(v->stor_begin != NULL);
 
1105
  FUNCTION(igraph_vector,remove_section)(v, elem, elem+1);
 
1106
}
 
1107
 
 
1108
/**
 
1109
 * \ingroup vector
 
1110
 * \function igraph_vector_move_interval
 
1111
 * \brief Copies a section of a vector.
 
1112
 *
 
1113
 * </para><para>
 
1114
 * The result of this function is undefined if the source and target
 
1115
 * intervals overlap.
 
1116
 * \param v The vector object.
 
1117
 * \param begin The position of the first element to move.
 
1118
 * \param end The position of the first element \em not to move.
 
1119
 * \param to The target position.
 
1120
 * \return Error code, the current implementation always returns with
 
1121
 *    success. 
 
1122
 *
 
1123
 * Time complexity: O(end-begin).
 
1124
 */
 
1125
 
 
1126
int FUNCTION(igraph_vector,move_interval)(TYPE(igraph_vector) *v, 
 
1127
                                          long int begin, long int end, 
 
1128
                                          long int to) {
 
1129
  assert(v != NULL);
 
1130
  assert(v->stor_begin != NULL);
 
1131
  memcpy(v->stor_begin+to, v->stor_begin+begin, 
 
1132
         sizeof(BASE)*(end-begin));
 
1133
 
 
1134
  return 0;
 
1135
}
 
1136
 
 
1137
int FUNCTION(igraph_vector,move_interval2)(TYPE(igraph_vector) *v, 
 
1138
                                          long int begin, long int end, 
 
1139
                                          long int to) {
 
1140
  assert(v != NULL);
 
1141
  assert(v->stor_begin != NULL);
 
1142
  memmove(v->stor_begin+to, v->stor_begin+begin, 
 
1143
         sizeof(BASE)*(end-begin));
 
1144
 
 
1145
  return 0;
 
1146
}
 
1147
 
 
1148
/**
 
1149
 * \ingroup vector
 
1150
 * \function igraph_vector_permdelete
 
1151
 * \brief Remove elements of a vector (for internal use).
 
1152
 */
 
1153
 
 
1154
void FUNCTION(igraph_vector,permdelete)(TYPE(igraph_vector) *v, 
 
1155
                                        const igraph_vector_t *index, long int nremove) {
 
1156
  long int i, n;
 
1157
  assert(v != NULL);
 
1158
  assert(v->stor_begin != NULL);
 
1159
  n = FUNCTION(igraph_vector,size)(v);
 
1160
  for (i=0; i<n; i++) {
 
1161
    if (VECTOR(*index)[i] != 0) {
 
1162
      VECTOR(*v)[ (long int)VECTOR(*index)[i]-1 ] = VECTOR(*v)[i];
 
1163
    }
 
1164
  }
 
1165
  v->end -= nremove;
 
1166
}
 
1167
 
 
1168
/**
 
1169
 * \ingroup vector
 
1170
 * \function igraph_vector_remove_negidx
 
1171
 * \brief Remove elements of a vector (for internal use).
 
1172
 */
 
1173
 
 
1174
void FUNCTION(igraph_vector,remove_negidx)(TYPE(igraph_vector) *v, 
 
1175
                                           const igraph_vector_t *neg, long int nremove) {
 
1176
  long int i, idx=0;
 
1177
  assert(v != NULL);
 
1178
  assert(v->stor_begin != NULL);
 
1179
  for (i=0; i<FUNCTION(igraph_vector,size)(v); i++) {
 
1180
    VECTOR(*v)[idx++] = VECTOR(*v)[i];
 
1181
  }
 
1182
  v->end -= nremove;
 
1183
}
 
1184
 
 
1185
/**
 
1186
 * \ingroup vector
 
1187
 * \function igraph_vector_isininterval
 
1188
 * \brief Checks if all elements of a vector are in the given
 
1189
 * interval.
 
1190
 * 
 
1191
 * \param v The vector object.
 
1192
 * \param low The lower limit of the interval (inclusive).
 
1193
 * \param high The higher limit of the interval (inclusive).
 
1194
 * \return True (positive integer) if all vector elements are in the
 
1195
 *   interval, false (zero) otherwise.
 
1196
 *
 
1197
 * Time complexity: O(n), the number
 
1198
 * of elements in the vector.
 
1199
 */
 
1200
 
 
1201
igraph_bool_t FUNCTION(igraph_vector,isininterval)(const TYPE(igraph_vector) *v, 
 
1202
                                                   BASE low, 
 
1203
                                                   BASE high) {
 
1204
  BASE *ptr;
 
1205
  assert(v != NULL);
 
1206
  assert(v->stor_begin != NULL);
 
1207
  for (ptr=v->stor_begin; ptr<v->end; ptr++) {
 
1208
    if (*ptr < low || *ptr >high) {
 
1209
      return 0;
 
1210
    }
 
1211
  }
 
1212
  return 1;
 
1213
}
 
1214
 
 
1215
/**
 
1216
 * \ingroup vector
 
1217
 * \function igraph_vector_any_smaller
 
1218
 * \brief Checks if any element of a vector is smaller than a limit.
 
1219
 * 
 
1220
 * \param v The \type igraph_vector_t object.
 
1221
 * \param limit The limit.
 
1222
 * \return True (positive integer) if the vector contains at least one
 
1223
 *   smaller element than \p limit, false (zero)
 
1224
 *   otherwise. 
 
1225
 * 
 
1226
 * Time complexity: O(n), the number
 
1227
 * of elements in the vector.
 
1228
 */
 
1229
 
 
1230
igraph_bool_t FUNCTION(igraph_vector,any_smaller)(const TYPE(igraph_vector) *v, 
 
1231
                                                  BASE limit) {
 
1232
  BASE *ptr;
 
1233
  assert(v != NULL);
 
1234
  assert(v->stor_begin != NULL);
 
1235
  for (ptr=v->stor_begin; ptr<v->end; ptr++) {
 
1236
    if (*ptr < limit) {
 
1237
      return 1;
 
1238
    }
 
1239
  }
 
1240
  return 0;
 
1241
}
 
1242
 
 
1243
/**
 
1244
 * \ingroup vector
 
1245
 * \function igraph_vector_is_equal
 
1246
 * \brief Decides whether two vectors contain exactly the same elements
 
1247
 * (in the same order).
 
1248
 * 
 
1249
 * \param lhs The first vector.
 
1250
 * \param rhs The second vector.
 
1251
 * \return Positive integer if the two vectors are equal element by
 
1252
 * element or zero if they are not.
 
1253
 * 
 
1254
 * Time complexity: O(n), the length
 
1255
 * of the vectors.
 
1256
 */
 
1257
 
 
1258
igraph_bool_t FUNCTION(igraph_vector,is_equal)(const TYPE(igraph_vector) *lhs, 
 
1259
                                               const TYPE(igraph_vector) *rhs) {
 
1260
  long int i, s;
 
1261
  assert(lhs != 0);
 
1262
  assert(rhs != 0);
 
1263
  assert(lhs->stor_begin != 0);
 
1264
  assert(rhs->stor_begin != 0);
 
1265
  
 
1266
  s=FUNCTION(igraph_vector,size)(lhs);
 
1267
  if (s != FUNCTION(igraph_vector,size)(rhs)) {
 
1268
    return 0;
 
1269
  } else {
 
1270
    for (i=0; i<s; i++) {
 
1271
      if (VECTOR(*lhs)[i] != VECTOR(*rhs)[i]) {
 
1272
        return 0;
 
1273
      }
 
1274
    }
 
1275
    return 1;
 
1276
  }
 
1277
}
 
1278
 
 
1279
/**
 
1280
 * \ingroup vector
 
1281
 * \function igraph_vector_binsearch
 
1282
 * \brief Finds an element by binary searching a sorted vector.
 
1283
 * 
 
1284
 * </para><para>
 
1285
 * It is assumed that the vector is sorted. If the specified element
 
1286
 * (\p what) is not in the vector, then the
 
1287
 * position of where it should be inserted (to keep the vector sorted)
 
1288
 * is returned.
 
1289
 * \param v The \type igraph_vector_t object.
 
1290
 * \param what The element to search for.
 
1291
 * \param pos Pointer to a \type long int. This is set to the
 
1292
 *   position of an instance of \p what in the
 
1293
 *   vector if it is present. If \p v does not
 
1294
 *   contain \p what then
 
1295
 *   \p pos is set to the position to which it
 
1296
 *   should be inserted (to keep the the vector sorted of course).
 
1297
 * \return Positive integer (true) if \p what is
 
1298
 *   found in the vector, zero (false) otherwise.
 
1299
 * 
 
1300
 * Time complexity: O(log(n)),
 
1301
 * n is the number of elements in
 
1302
 * \p v.
 
1303
 */
 
1304
 
 
1305
igraph_bool_t FUNCTION(igraph_vector,binsearch)(const TYPE(igraph_vector) *v, 
 
1306
                                                BASE what, long int *pos) {
 
1307
  long int left=0;
 
1308
  long int right=FUNCTION(igraph_vector,size)(v)-1;
 
1309
 
 
1310
  if (right < 0) {
 
1311
    if (pos != 0) *pos=0;
 
1312
        return 0;
 
1313
  }
 
1314
 
 
1315
  while (left < right-1) {
 
1316
    long int middle=(left+right)/2;
 
1317
    if (VECTOR(*v)[middle] > what) {
 
1318
      right=middle;
 
1319
    } else if (VECTOR(*v)[middle] < what) {
 
1320
      left=middle;
 
1321
    } else {
 
1322
      left=middle;
 
1323
      break;
 
1324
    }
 
1325
  }
 
1326
 
 
1327
  if (VECTOR(*v)[left] < what) {
 
1328
    if (VECTOR(*v)[right] < what) left=right+1;
 
1329
    else left=right;
 
1330
  }
 
1331
  
 
1332
  if (pos != 0) {
 
1333
    *pos=left;
 
1334
  }
 
1335
  
 
1336
  if (left < FUNCTION(igraph_vector,size)(v)) {
 
1337
    return VECTOR(*v)[left]==what;
 
1338
  } else { 
 
1339
    return 0;
 
1340
  }
 
1341
}
 
1342
 
 
1343
/**
 
1344
 * \ingroup vector
 
1345
 * \function igraph_vector_binsearch2
 
1346
 * \brief Binary search, without returning the index.
 
1347
 * 
 
1348
 * </para><para>
 
1349
 * It is assumed that the vector is sorted. If the specified element
 
1350
 * (\p what) is not in the vector, then the
 
1351
 * position of where it should be inserted (to keep the vector sorted)
 
1352
 * is returned.
 
1353
 * \param v The \type igraph_vector_t object.
 
1354
 * \param what The element to search for.
 
1355
 * \return Positive integer (true) if \p what is
 
1356
 *   found in the vector, zero (false) otherwise.
 
1357
 * 
 
1358
 * Time complexity: O(log(n)),
 
1359
 * n is the number of elements in
 
1360
 * \p v.
 
1361
 */
 
1362
 
 
1363
igraph_bool_t FUNCTION(igraph_vector,binsearch2)(const TYPE(igraph_vector) *v, 
 
1364
                                                 BASE what) {
 
1365
  long int left=0;
 
1366
  long int right=v->end - v->stor_begin-1;
 
1367
 
 
1368
  if (right<0) {
 
1369
    return 0;
 
1370
  }
 
1371
 
 
1372
  while (left < right-1) {
 
1373
    long int middle=(left+right)/2;
 
1374
    if (VECTOR(*v)[middle] > what) {
 
1375
      right=middle;
 
1376
    } else if (VECTOR(*v)[middle] < what) {
 
1377
      left=middle;
 
1378
    } else {
 
1379
      left=middle;
 
1380
      break;
 
1381
    }
 
1382
  }
 
1383
 
 
1384
  return VECTOR(*v)[left]==what || VECTOR(*v)[right]==what;
 
1385
}
 
1386
 
 
1387
/**
 
1388
 * \function igraph_vector_scale
 
1389
 * \brief Multiply all elements of a vector by a constant
 
1390
 * 
 
1391
 * \param v The vector.
 
1392
 * \param by The constant.
 
1393
 * \return Error code. The current implementation always returns with success.
 
1394
 * 
 
1395
 * Added in version 0.2.</para><para>
 
1396
 * 
 
1397
 * Time complexity: O(n), the number of elements in a vector.
 
1398
 */
 
1399
 
 
1400
void FUNCTION(igraph_vector,scale)(TYPE(igraph_vector) *v, BASE by) {
 
1401
  long int i;
 
1402
  for (i=0; i<FUNCTION(igraph_vector,size)(v); i++) {
 
1403
    VECTOR(*v)[i] *= by;
 
1404
  }
 
1405
}
 
1406
 
 
1407
/**
 
1408
 * \function igraph_vector_add_constant
 
1409
 * \brief Add a constant to the vector.
 
1410
 * 
 
1411
 * \p plus is added to every element of \p v. Note that overflow
 
1412
 * might happen.
 
1413
 * \param v The input vector.
 
1414
 * \param plus The constant to add.
 
1415
 * 
 
1416
 * Time complexity: O(n), the number of elements.
 
1417
 */
 
1418
 
 
1419
void FUNCTION(igraph_vector,add_constant)(TYPE(igraph_vector) *v, BASE plus) {
 
1420
  long int i, n=FUNCTION(igraph_vector,size)(v);
 
1421
  for (i=0; i<n; i++) {
 
1422
    VECTOR(*v)[i] += plus;
 
1423
  }
 
1424
}
 
1425
 
 
1426
/** 
 
1427
 * \function igraph_vector_contains
 
1428
 * \brief Linear search in a vector.
 
1429
 * 
 
1430
 * Check whether the supplied element is included in the vector, by
 
1431
 * linear search.
 
1432
 * \param v The input vector.
 
1433
 * \param e The element to look for.
 
1434
 * \return \c TRUE if the element is found and \c FALSE otherwise.
 
1435
 * 
 
1436
 * Time complexity: O(n), the length of the vector.
 
1437
 */
 
1438
 
 
1439
igraph_bool_t FUNCTION(igraph_vector,contains)(const TYPE(igraph_vector) *v, 
 
1440
                                               BASE e) {
 
1441
  BASE *p=v->stor_begin;
 
1442
  while (p<v->end) {
 
1443
    if (*p==e) { 
 
1444
      return 1;
 
1445
    }
 
1446
    p++;
 
1447
  }
 
1448
  return 0;
 
1449
}
 
1450
 
 
1451
/**
 
1452
 * \function igraph_vector_search
 
1453
 * \brief Search from a given position
 
1454
 *
 
1455
 * The supplied element \p what is searched in vector \p v, starting
 
1456
 * from element index \p from. If found then the index of the first
 
1457
 * instance (after \p from) is stored in \p pos.
 
1458
 * \param v The input vector.
 
1459
 * \param from The index to start searching from. No range checking is
 
1460
 *     performed. 
 
1461
 * \param what The element to find.
 
1462
 * \param pos If not \c NULL then the index of the found element is
 
1463
 *    stored here.
 
1464
 * \return Boolean, \c TRUE if the element was found, \c FALSE
 
1465
 *   otherwise.
 
1466
 * 
 
1467
 * Time complexity: O(m), the number of elements to search, the length
 
1468
 * of the vector minus the \p from argument.
 
1469
 */
 
1470
 
 
1471
igraph_bool_t FUNCTION(igraph_vector,search)(const TYPE(igraph_vector) *v, 
 
1472
                                             long int from, BASE what, 
 
1473
                                             long int *pos) {
 
1474
  long int i, n=FUNCTION(igraph_vector,size)(v);  
 
1475
  for (i=from; i<n; i++) {
 
1476
    if (VECTOR(*v)[i]==what) break;
 
1477
  }
 
1478
  
 
1479
  if (i<n) {
 
1480
    if (pos != 0) {
 
1481
      *pos=i;
 
1482
    }
 
1483
    return 1;
 
1484
  } else {
 
1485
    return 0;
 
1486
  }
 
1487
}
 
1488
 
 
1489
/**
 
1490
 * \function igraph_vector_filter_smaller
 
1491
 * \ingroup internal
 
1492
 */
 
1493
 
 
1494
int FUNCTION(igraph_vector,filter_smaller)(TYPE(igraph_vector) *v, 
 
1495
                                           BASE elem) {
 
1496
  long int i=0, n=FUNCTION(igraph_vector,size)(v);
 
1497
  long int s;
 
1498
  while (i<n && VECTOR(*v)[i]<elem) {
 
1499
    i++;
 
1500
  }
 
1501
  s=i;
 
1502
  
 
1503
  while (s<n && VECTOR(*v)[s]==elem) {
 
1504
    s++;
 
1505
  }
 
1506
  
 
1507
  FUNCTION(igraph_vector,remove_section)(v, 0, i+(s-i)/2);
 
1508
  return 0;
 
1509
}
 
1510
 
 
1511
/**
 
1512
 * \function igraph_vector_append
 
1513
 * \brief Append a vector to another one.
 
1514
 * 
 
1515
 * The target vector will be resized (except \p from is empty).
 
1516
 * \param to The vector to append to.
 
1517
 * \param from The vector to append, it is kept unchanged.
 
1518
 * \return Error code.
 
1519
 *
 
1520
 * Time complexity: O(n), the number of elements in the new vector.
 
1521
 */
 
1522
 
 
1523
int FUNCTION(igraph_vector,append)(TYPE(igraph_vector) *to, 
 
1524
                                   const TYPE(igraph_vector) *from) {
 
1525
  long int tosize, fromsize;
 
1526
  
 
1527
  tosize=FUNCTION(igraph_vector,size)(to);
 
1528
  fromsize=FUNCTION(igraph_vector,size)(from);
 
1529
  IGRAPH_CHECK(FUNCTION(igraph_vector,resize)(to, tosize+fromsize));
 
1530
  memcpy(to->stor_begin+tosize, from->stor_begin, 
 
1531
         sizeof(BASE)*fromsize);
 
1532
  to->end=to->stor_begin+tosize+fromsize;
 
1533
  
 
1534
  return 0;
 
1535
}
 
1536
 
 
1537
/**
 
1538
 * \function igraph_vector_get_interval
 
1539
 */
 
1540
 
 
1541
int FUNCTION(igraph_vector,get_interval)(const TYPE(igraph_vector) *v, 
 
1542
                                         TYPE(igraph_vector) *res,
 
1543
                                         long int from, long int to) {
 
1544
  IGRAPH_CHECK(FUNCTION(igraph_vector,resize)(res, to-from));
 
1545
  memcpy(res->stor_begin, v->stor_begin+from, (to-from)*sizeof(BASE));
 
1546
  return 0;
 
1547
}
 
1548
 
 
1549
/**
 
1550
 * \function igraph_vector_maxdifference
 
1551
 * \brief The largest element of \p m1 - \p m2
 
1552
 *
 
1553
 * The maximum the elementwise performed difference is returned.
 
1554
 * Both vectors must be non-empty, but they not need to have the same
 
1555
 * length, the extra elements in the longer vector are ignored.
 
1556
 * \param m1 The first vector.
 
1557
 * \param m2 The second vector.
 
1558
 * \return The maximum of the difference of \p m1 and \p m2.
 
1559
 *
 
1560
 * Time complexity: O(n), the number of elements in the shorter
 
1561
 * vector.
 
1562
 */
 
1563
 
 
1564
BASE FUNCTION(igraph_vector,maxdifference)(const TYPE(igraph_vector) *m1,
 
1565
                                           const TYPE(igraph_vector) *m2) {
 
1566
  long int n1=FUNCTION(igraph_vector,size)(m1);
 
1567
  long int n2=FUNCTION(igraph_vector,size)(m2);
 
1568
  long int n= n1 < n2 ? n1 : n2;
 
1569
  long int i;
 
1570
  BASE diff=ZERO;
 
1571
  
 
1572
  for (i=0; i<n; i++) {
 
1573
    BASE d=fabs(VECTOR(*m1)[i]-VECTOR(*m2)[i]);
 
1574
    if (d > diff) {
 
1575
      diff=d;
 
1576
    }
 
1577
  }
 
1578
  
 
1579
  return diff;
 
1580
}
 
1581
 
 
1582
/**
 
1583
 * \function igraph_vector_update
 
1584
 * \brief Update a vector from another one.
 
1585
 * 
 
1586
 * After this operation the contents of \p to will be exactly the same
 
1587
 * \p from. \p to will be resized if it was originally shorter or
 
1588
 * longer than \p from.
 
1589
 * \param to The vector to update.
 
1590
 * \param from The vector to update from.
 
1591
 * \return Error code.
 
1592
 * 
 
1593
 * Time complexity: O(n), the number of elements in \p from.
 
1594
 */
 
1595
 
 
1596
int FUNCTION(igraph_vector,update)(TYPE(igraph_vector) *to, 
 
1597
                                   const TYPE(igraph_vector) *from) {
 
1598
  long int n=FUNCTION(igraph_vector,size)(from);
 
1599
  FUNCTION(igraph_vector,resize)(to, n);
 
1600
  memcpy(to->stor_begin, from->stor_begin, sizeof(BASE)*n);
 
1601
  return 0;
 
1602
}
 
1603
 
 
1604
/**
 
1605
 * \function igraph_vector_swap
 
1606
 * \brief Swap elements of two vectors.
 
1607
 * 
 
1608
 * The two vectors must have the same length, otherwise an error
 
1609
 * happens.
 
1610
 * \param v1 The first vector.
 
1611
 * \param v2 The second vector.
 
1612
 * \return Error code.
 
1613
 * 
 
1614
 * Time complexity: O(n), the length of the vectors.
 
1615
 */
 
1616
 
 
1617
int FUNCTION(igraph_vector,swap)(TYPE(igraph_vector) *v1, TYPE(igraph_vector) *v2) {
 
1618
  
 
1619
  long int i, n1=FUNCTION(igraph_vector,size)(v1);
 
1620
  long int n2=FUNCTION(igraph_vector,size)(v2);
 
1621
  if (n1 != n2) {
 
1622
    IGRAPH_ERROR("Vectors must have the same number of elements for swapping",
 
1623
                 IGRAPH_EINVAL);
 
1624
  }
 
1625
  
 
1626
  for (i=0; i<n1; i++) {
 
1627
    BASE tmp;
 
1628
    tmp=VECTOR(*v1)[i];
 
1629
    VECTOR(*v1)[i]=VECTOR(*v2)[i];
 
1630
    VECTOR(*v2)[i]=tmp;
 
1631
  }
 
1632
  return 0;
 
1633
}
 
1634
 
 
1635
/**
 
1636
 * \function igraph_vector_swap_elements
 
1637
 * \brief Swap two elements in a vector.
 
1638
 * 
 
1639
 * Note that currently no range checking if performed.
 
1640
 * \param v The input vector.
 
1641
 * \param i Index of the first element.
 
1642
 * \param j index of the second element. (Might be the same the
 
1643
 * first.)
 
1644
 * \return Error code, currently always \c IGRAPH_SUCCESS.
 
1645
 * 
 
1646
 * Time complexity: O(1).
 
1647
 */
 
1648
 
 
1649
int FUNCTION(igraph_vector,swap_elements)(TYPE(igraph_vector) *v,
 
1650
                                          long int i, long int j) {
 
1651
  BASE tmp=VECTOR(*v)[i];
 
1652
  VECTOR(*v)[i]=VECTOR(*v)[j];
 
1653
  VECTOR(*v)[j]=tmp;
 
1654
  
 
1655
  return 0;
 
1656
}
 
1657
 
 
1658
/**
 
1659
 * \function igraph_vector_reverse
 
1660
 * \brief Reverse the elements of a vector.
 
1661
 * 
 
1662
 * The first element will be last, the last element will be
 
1663
 * first, etc.
 
1664
 * \param v The input vector.
 
1665
 * \return Error code, currently always \c IGRAPH_SUCCESS.
 
1666
 * 
 
1667
 * Time complexity: O(n), the number of elements.
 
1668
 */
 
1669
 
 
1670
int FUNCTION(igraph_vector,reverse)(TYPE(igraph_vector) *v) {
 
1671
  
 
1672
  long int n=FUNCTION(igraph_vector,size)(v), n2=n/2;
 
1673
  long int i,j;
 
1674
  for (i=0, j=n-1; i<n2; i++, j--) {
 
1675
    BASE tmp;
 
1676
    tmp=VECTOR(*v)[i];
 
1677
    VECTOR(*v)[i]=VECTOR(*v)[j];
 
1678
    VECTOR(*v)[j]=tmp;
 
1679
  }
 
1680
  return 0;
 
1681
}
 
1682
 
 
1683
/**
 
1684
 * \ingroup vector
 
1685
 * \function igraph_vector_shuffle
 
1686
 * \brief Shuffles a vector in-place using the Fisher-Yates method
 
1687
 * 
 
1688
 * </para><para>
 
1689
 * The Fisher-Yates shuffle ensures that every implementation is
 
1690
 * equally probable when using a proper randomness source. Of course
 
1691
 * this does not apply to pseudo-random generators as the cycle of
 
1692
 * these generators is less than the number of possible permutations
 
1693
 * of the vector if the vector is long enough.
 
1694
 * \param v The vector object.
 
1695
 * \return Error code, currently always \c IGRAPH_SUCCESS.
 
1696
 *
 
1697
 * Time complexity: O(n),
 
1698
 * n is the number of elements in the
 
1699
 * vector. 
 
1700
 */
 
1701
 
 
1702
int FUNCTION(igraph_vector,shuffle)(TYPE(igraph_vector) *v) {
 
1703
  long int n = FUNCTION(igraph_vector,size)(v);
 
1704
  long int k;
 
1705
  BASE dummy;
 
1706
 
 
1707
  RNG_BEGIN();
 
1708
  while (n > 1) {
 
1709
    k = RNG_INTEGER(0, n-1);
 
1710
    n--;
 
1711
    dummy = VECTOR(*v)[n];
 
1712
    VECTOR(*v)[n] = VECTOR(*v)[k];
 
1713
    VECTOR(*v)[k] = dummy;
 
1714
  }
 
1715
  RNG_END();
 
1716
 
 
1717
  return IGRAPH_SUCCESS;
 
1718
}
 
1719
 
 
1720
/**
 
1721
 * \function igraph_vector_add
 
1722
 * \brief Add two vectors.
 
1723
 * 
 
1724
 * Add the elements of \p v2 to \p v1, the result is stored in \p
 
1725
 * v1. The two vectors must have the same length.
 
1726
 * \param v1 The first vector, the result will be stored here.
 
1727
 * \param v2 The second vector, its contents will be unchanged. 
 
1728
 * \return Error code.
 
1729
 * 
 
1730
 * Time complexity: O(n), the number of elements.
 
1731
 */
 
1732
 
 
1733
int FUNCTION(igraph_vector,add)(TYPE(igraph_vector) *v1, 
 
1734
                                const TYPE(igraph_vector) *v2) {
 
1735
  
 
1736
  long int n1=FUNCTION(igraph_vector,size)(v1);
 
1737
  long int n2=FUNCTION(igraph_vector,size)(v2);
 
1738
  long int i;
 
1739
  if (n1 != n2) {
 
1740
    IGRAPH_ERROR("Vectors must have the same number of elements for swapping",
 
1741
                 IGRAPH_EINVAL);
 
1742
  }
 
1743
  
 
1744
  for (i=0; i<n1; i++) {
 
1745
    VECTOR(*v1)[i] += VECTOR(*v2)[i];
 
1746
  }
 
1747
  
 
1748
  return 0;
 
1749
}
 
1750
 
 
1751
/**
 
1752
 * \function igraph_vector_sub
 
1753
 * \brief Subtract a vector from another one.
 
1754
 * 
 
1755
 * Substract the elements of \p v2 from \p v1, the result is stored in
 
1756
 * \p v1. The two vectors must have the same length.
 
1757
 * \param v1 The first vector, to subtract from. The result is stored
 
1758
 *    here.
 
1759
 * \param v2 The vector to subtract, it will be unchanged.
 
1760
 * \return Error code.
 
1761
 * 
 
1762
 * Time complexity: O(n), the length of the vectors.
 
1763
 */
 
1764
 
 
1765
int FUNCTION(igraph_vector,sub)(TYPE(igraph_vector) *v1, 
 
1766
                                const TYPE(igraph_vector) *v2) {
 
1767
  
 
1768
  long int n1=FUNCTION(igraph_vector,size)(v1);
 
1769
  long int n2=FUNCTION(igraph_vector,size)(v2);
 
1770
  long int i;
 
1771
  if (n1 != n2) {
 
1772
    IGRAPH_ERROR("Vectors must have the same number of elements for swapping",
 
1773
                 IGRAPH_EINVAL);
 
1774
  }
 
1775
  
 
1776
  for (i=0; i<n1; i++) {
 
1777
    VECTOR(*v1)[i] -= VECTOR(*v2)[i];
 
1778
  }
 
1779
  
 
1780
  return 0;
 
1781
}
 
1782
 
 
1783
/**
 
1784
 * \function igraph_vector_mul
 
1785
 * \brief Multiply two vectors.
 
1786
 * 
 
1787
 * \p v1 will be multiplied by \p v2, elementwise. The two vectors
 
1788
 * must have the same length.
 
1789
 * \param v1 The first vector, the result will be stored here.
 
1790
 * \param v2 The second vector, it is left unchanged.
 
1791
 * \return Error code.
 
1792
 * 
 
1793
 * Time complexity: O(n), the number of elements.
 
1794
 */
 
1795
 
 
1796
int FUNCTION(igraph_vector,mul)(TYPE(igraph_vector) *v1, 
 
1797
                                const TYPE(igraph_vector) *v2) {
 
1798
  
 
1799
  long int n1=FUNCTION(igraph_vector,size)(v1);
 
1800
  long int n2=FUNCTION(igraph_vector,size)(v2);
 
1801
  long int i;
 
1802
  if (n1 != n2) {
 
1803
    IGRAPH_ERROR("Vectors must have the same number of elements for swapping",
 
1804
                 IGRAPH_EINVAL);
 
1805
  }
 
1806
  
 
1807
  for (i=0; i<n1; i++) {
 
1808
    VECTOR(*v1)[i] *= VECTOR(*v2)[i];
 
1809
  }
 
1810
  
 
1811
  return 0;
 
1812
}
 
1813
 
 
1814
/**
 
1815
 * \function igraph_vector_div
 
1816
 * \brief Divide a vector by another one.
 
1817
 * 
 
1818
 * \p v1 is divided by \p v2, elementwise. They must have the same length. If the
 
1819
 * base type of the vector can generate divide by zero errors then
 
1820
 * please make sure that \p v2 contains no zero if you want to avoid
 
1821
 * trouble.
 
1822
 * \param v1 The dividend. The result is also stored here.
 
1823
 * \param v2 The divisor, it is left unchanged.
 
1824
 * \return Error code.
 
1825
 * 
 
1826
 * Time complexity: O(n), the length of the vectors.
 
1827
 */
 
1828
 
 
1829
int FUNCTION(igraph_vector,div)(TYPE(igraph_vector) *v1, 
 
1830
                                const TYPE(igraph_vector) *v2) {
 
1831
  
 
1832
  long int n1=FUNCTION(igraph_vector,size)(v1);
 
1833
  long int n2=FUNCTION(igraph_vector,size)(v2);
 
1834
  long int i;
 
1835
  if (n1 != n2) {
 
1836
    IGRAPH_ERROR("Vectors must have the same number of elements for swapping",
 
1837
                 IGRAPH_EINVAL);
 
1838
  }
 
1839
  
 
1840
  for (i=0; i<n1; i++) {
 
1841
    VECTOR(*v1)[i] /= VECTOR(*v2)[i];
 
1842
  }
 
1843
  
 
1844
  return 0;
 
1845
}
 
1846
 
 
1847
/**
 
1848
 * \function igraph_vector_minmax
 
1849
 * \brief Minimum and maximum elements of a vector.
 
1850
 * 
 
1851
 * Handy if you want to have both the smallest and largest element of
 
1852
 * a vector. The vector is only traversed once. The vector must by non-empty.
 
1853
 * \param v The input vector. It must contain at least one element.
 
1854
 * \param min Pointer to a base type variable, the minimum is stored
 
1855
 *     here.
 
1856
 * \param max Pointer to a base type variable, the maximum is stored
 
1857
 *     here.
 
1858
 * \return Error code.
 
1859
 * 
 
1860
 * Time complexity: O(n), the number of elements.
 
1861
 */
 
1862
 
 
1863
int FUNCTION(igraph_vector,minmax)(const TYPE(igraph_vector) *v,
 
1864
                                   BASE *min, BASE *max) {
 
1865
  long int n=FUNCTION(igraph_vector,size)(v);
 
1866
  long int i;
 
1867
  *min=*max=VECTOR(*v)[0];
 
1868
  for (i=1; i<n; i++) {
 
1869
    BASE tmp=VECTOR(*v)[i];
 
1870
    if (tmp > *max) { 
 
1871
      *max=tmp;
 
1872
    } else if (tmp < *min) {
 
1873
      *min=tmp;
 
1874
    }
 
1875
  }
 
1876
  return 0;
 
1877
}
 
1878
 
 
1879
/**
 
1880
 * \function igraph_vector_which_minmax
 
1881
 * \brief Index of the minimum and maximum elements
 
1882
 * 
 
1883
 * Handy if you need the indices of the smallest and largest
 
1884
 * elements. The vector is traversed only once. The vector must to
 
1885
 * non-empty.
 
1886
 * \param v The input vector. It must contain at least one element.
 
1887
 * \param which_min The index of the minimum element will be stored
 
1888
 *   here.
 
1889
 * \param which_max The index of the maximum element will be stored
 
1890
 *   here.
 
1891
 * \return Error code.
 
1892
 * 
 
1893
 * Time complexity: O(n), the number of elements.
 
1894
 */
 
1895
 
 
1896
int FUNCTION(igraph_vector,which_minmax)(const TYPE(igraph_vector) *v,
 
1897
                                         long int *which_min, long int *which_max) {
 
1898
 
 
1899
  long int n=FUNCTION(igraph_vector,size)(v);
 
1900
  long int i;
 
1901
  BASE min, max;
 
1902
  *which_min=*which_max=0;
 
1903
  min=max=VECTOR(*v)[0];
 
1904
  for (i=1; i<n; i++) {
 
1905
    BASE tmp=VECTOR(*v)[i];
 
1906
    if (tmp > max) {
 
1907
      max=tmp;
 
1908
      *which_max=i;
 
1909
    } else if (tmp < min) {
 
1910
      min=tmp;
 
1911
      *which_min=i;
 
1912
    }
 
1913
  }
 
1914
  return 0;
 
1915
}
 
1916
 
 
1917
/**
 
1918
 * \function igraph_vector_isnull
 
1919
 * \brief Are all elements zero?
 
1920
 * 
 
1921
 * Checks whether all elements of a vector are zero.
 
1922
 * \param v The input vector
 
1923
 * \return Boolean, \c TRUE if the vector contains only zeros, \c
 
1924
 *    FALSE otherwise.
 
1925
 * 
 
1926
 * Time complexity: O(n), the number of elements.
 
1927
 */
 
1928
 
 
1929
igraph_bool_t FUNCTION(igraph_vector,isnull)(const TYPE(igraph_vector) *v) {
 
1930
 
 
1931
  long int n=FUNCTION(igraph_vector,size)(v);
 
1932
  long int i=0;
 
1933
  
 
1934
  while ( i<n && VECTOR(*v)[i]==0) {
 
1935
    i++;
 
1936
  }
 
1937
  
 
1938
  return i==n;
 
1939
}
 
1940
 
 
1941
/**
 
1942
 * \function igraph_vector_intersect_sorted
 
1943
 * \brief Calculates the intersection of two sorted vectors
 
1944
 *
 
1945
 * The elements that are contained in both vectors are stored in the result
 
1946
 * vector. All three vectors must be initialized.
 
1947
 *
 
1948
 * \param v1 the first vector
 
1949
 * \param v2 the second vector
 
1950
 * \param result the result vector
 
1951
 * \param keep_multiplicity whether to preserve the multiplicity of the
 
1952
 *   elements. If \c FALSE, all elements in the resulting vector will be
 
1953
 *   unique
 
1954
 */
 
1955
int FUNCTION(igraph_vector,intersect_sorted)(const TYPE(igraph_vector) *v1,
 
1956
    const TYPE(igraph_vector) *v2, TYPE(igraph_vector) *result,
 
1957
    igraph_bool_t keep_multiplicity) {
 
1958
  long int i, j, k, i0, j0;
 
1959
  i0 = FUNCTION(igraph_vector,size)(v1);
 
1960
  j0 = FUNCTION(igraph_vector,size)(v2);
 
1961
  i = j = 0;
 
1962
  FUNCTION(igraph_vector,clear)(result);
 
1963
  while (i < i0 && j < j0) {
 
1964
    BASE element;
 
1965
    if (VECTOR(*v1)[i] == VECTOR(*v2)[j]) {
 
1966
      element = VECTOR(*v1)[i]; k=0;
 
1967
      while (i < i0 && VECTOR(*v1)[i] == element) { i++; k++; }
 
1968
      while (j < j0 && VECTOR(*v2)[j] == element) { j++; k++; }
 
1969
      if (!keep_multiplicity) k=1;
 
1970
      while (k>0) {
 
1971
        FUNCTION(igraph_vector,push_back)(result, element);
 
1972
        k--;
 
1973
      }
 
1974
    } else if (VECTOR(*v1)[i] < VECTOR(*v2)[j]) i++;
 
1975
    else j++;
 
1976
  }
 
1977
 
 
1978
  return 0;
 
1979
}
 
1980