~alivema4ever/ubuntu/trusty/weechat/lp-1299347-fix

« back to all changes in this revision

Viewing changes to src/core/wee-hashtable.c

  • Committer: Package Import Robot
  • Author(s): Emmanuel Bouthenot
  • Date: 2013-01-23 18:44:36 UTC
  • mfrom: (1.1.23)
  • Revision ID: package-import@ubuntu.com-20130123184436-pillcj7jmtyyj00j
Tags: 0.4.0-1
* New upstream release.
* Bump Standards-Version to 3.9.4
* Remove UPGRADE_0.3 from doc (no more included in upstream sources).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
 * Copyright (C) 2010-2012 Sebastien Helleu <flashcode@flashtux.org>
 
2
 * wee-hashtable.c - implementation of hashtable
 
3
 *
 
4
 * Copyright (C) 2010-2013 Sebastien Helleu <flashcode@flashtux.org>
3
5
 *
4
6
 * This file is part of WeeChat, the extensible chat client.
5
7
 *
17
19
 * along with WeeChat.  If not, see <http://www.gnu.org/licenses/>.
18
20
 */
19
21
 
20
 
/*
21
 
 * wee-hashtable.c: implementation of hashtable
22
 
 */
23
 
 
24
22
#ifdef HAVE_CONFIG_H
25
23
#include "config.h"
26
24
#endif
45
43
 
46
44
 
47
45
/*
48
 
 * hashtable_get_type: get integer for type (string)
 
46
 * Searches for a hashtable type.
 
47
 *
 
48
 * Returns index of type in enum t_hashtable_type, -1 if type is not found.
49
49
 */
50
50
 
51
51
int
67
67
}
68
68
 
69
69
/*
70
 
 * hashtable_hash_key_string_cb: default callback to hash a string key
 
70
 * Hashes a string key (default callback).
 
71
 *
 
72
 * Returns an unsigned integer between 0 and size-1.
71
73
 */
72
74
 
73
75
unsigned int
86
88
}
87
89
 
88
90
/*
89
 
 * hashtable_keycmp_string_cb: default callback for string comparison on keys
 
91
 * Compares two string keys (default callback).
 
92
 *
 
93
 * Returns:
 
94
 *   -1: key1 < key2
 
95
 *    0: key1 == key2
 
96
 *    1: key1 > key2
90
97
 */
91
98
 
92
99
int
100
107
}
101
108
 
102
109
/*
103
 
 * hashtable_new: create a new hash table
 
110
 * Creates a new hashtable.
 
111
 *
 
112
 * The size is NOT a limit for number of items in hashtable. It is the size of
 
113
 * internal array to store hashed keys: a high value uses more memory, but has
 
114
 * better performance because this reduces the collisions of hashed keys and
 
115
 * then reduces length of linked lists.
 
116
 *
 
117
 * Returns pointer to new hashtable, NULL if error.
104
118
 */
105
119
 
106
120
struct t_hashtable *
157
171
}
158
172
 
159
173
/*
160
 
 * hashtable_alloc_type: alloc space for a key or value
 
174
 * Allocates space for a key or value.
161
175
 */
162
176
 
163
177
void
214
228
}
215
229
 
216
230
/*
217
 
 * hashtable_free_key: free space used by a key
 
231
 * Frees space used by a key.
218
232
 */
219
233
 
220
234
void
237
251
}
238
252
 
239
253
/*
240
 
 * hashtable_free_value: free space used by a value
 
254
 * Frees space used by a value.
241
255
 */
242
256
 
243
257
void
269
283
}
270
284
 
271
285
/*
272
 
 * hashtable_set_with_size: set value for item in hash table
273
 
 *                          argument size is used only for type "buffer"
274
 
 *                          return 1 if ok, 0 if error
 
286
 * Sets value for a key in hashtable.
 
287
 *
 
288
 * The size arguments are used only for type "buffer".
 
289
 *
 
290
 * Returns:
 
291
 *   1: OK
 
292
 *   0: error
275
293
 */
276
294
 
277
295
int
289
307
        return 0;
290
308
    }
291
309
 
292
 
    /* search position for item in hash table */
 
310
    /* search position for item in hashtable */
293
311
    hash = hashtable->callback_hash_key (hashtable, key);
294
312
    pos_item = NULL;
295
313
    for (ptr_item = hashtable->htable[hash];
300
318
        pos_item = ptr_item;
301
319
    }
302
320
 
303
 
    /* replace value if item is already in hash table */
 
321
    /* replace value if item is already in hashtable */
304
322
    if (ptr_item && (hashtable->callback_keycmp (hashtable, key, ptr_item->key) == 0))
305
323
    {
306
324
        hashtable_free_value (hashtable, ptr_item);
349
367
}
350
368
 
351
369
/*
352
 
 * hashtable_set: set value for item in hash table
353
 
 *                return 1 if ok, 0 if error
354
 
 *                Note: this function can be called *only* if key AND value are
355
 
 *                      *not* of type "buffer"
 
370
 * Sets value for a key in hashtable.
 
371
 *
 
372
 * Note: this function can be called *only* if key AND value are *not* of type
 
373
 * "buffer".
 
374
 *
 
375
 * Returns:
 
376
 *   1: OK
 
377
 *   0: error
356
378
 */
357
379
 
358
380
int
363
385
}
364
386
 
365
387
/*
366
 
 * hashtable_get_item: search an item in hashtable
367
 
 *                     if hash is non NULL pointer, then it is set with
368
 
 *                     hash value of key (even if key is not found)
 
388
 * Searches for an item in hashtable.
 
389
 *
 
390
 * If hash is non NULL, then it is set with hash value of key (even if key is
 
391
 * not found).
369
392
 */
370
393
 
371
394
struct t_hashtable_item *
397
420
}
398
421
 
399
422
/*
400
 
 * hashtable_get: get value for a key in hash table
401
 
 *                return pointer to "value" for key,
402
 
 *                or NULL if key is not found in hash table
 
423
 * Gets value for a key in hashtable.
 
424
 *
 
425
 * Returns pointer to value for key, NULL if key is not found.
403
426
 */
404
427
 
405
428
void *
413
436
}
414
437
 
415
438
/*
416
 
 * hashtable_has_key: return 1 if key is in hashtable, otherwise 0
 
439
 * Checks if a key exists in the hashtable.
 
440
 *
 
441
 * Returns:
 
442
 *   1: key exists
 
443
 *   0: key does not exist
417
444
 */
418
445
 
419
446
int
423
450
}
424
451
 
425
452
/*
426
 
 * hashtable_to_string: convert a value (from any type) to a string
427
 
 *                      Value returned is a pointer to a static buffer (except
428
 
 *                      if type is string, then pointer to string itself is
429
 
 *                      returned) and must be used immediately, it is
430
 
 *                      overwritten by subsequent calls to this function.
 
453
 * Converts a value (from any type) to a string.
 
454
 *
 
455
 * Returns pointer to a static buffer (for type string, returns pointer to
 
456
 * string itself), which must be used immediately, it is overwritten by
 
457
 * subsequent calls to this function.
431
458
 */
432
459
 
433
460
const char *
463
490
}
464
491
 
465
492
/*
466
 
 * hashtable_map: call a function on all hashtable entries
 
493
 * Calls a function on all hashtable entries.
467
494
 */
468
495
 
469
496
void
495
522
}
496
523
 
497
524
/*
498
 
 * hashtable_map_string: call a function on all hashtable entries,
499
 
 *                       sending keys and values as strings
 
525
 * Calls a function on all hashtable entries (sends keys and values as strings).
500
526
 */
501
527
 
502
528
void
543
569
}
544
570
 
545
571
/*
546
 
 * hashtable_duplicate_map_cb: function called for each variable in hashtable
547
 
 *                             to duplicate all keys
 
572
 * Duplicates key/value in another hashtable (callback called for each variable
 
573
 * in hashtable).
548
574
 */
549
575
 
550
576
void
563
589
}
564
590
 
565
591
/*
566
 
 * hashtable_dup: duplicate hashtable
 
592
 * Duplicates a hashtable.
 
593
 *
 
594
 * Returns pointer to new hashtable, NULL if error.
567
595
 */
568
596
 
569
597
struct t_hashtable *
588
616
}
589
617
 
590
618
/*
591
 
 * hashtable_get_list_keys_map_cb: function called for each variable in
592
 
 *                                 hashtable to build sorted list of keys
 
619
 * Builds sorted list of keys (callback called for each variable in hashtable).
593
620
 */
594
621
 
595
622
void
610
637
}
611
638
 
612
639
/*
613
 
 * hashtable_get_list_keys: get list with sorted keys of hashtable
614
 
 *                          Note: list must be freed after use
 
640
 * Gets list with sorted keys of hashtable.
 
641
 *
 
642
 * Note: list must be freed after use.
615
643
 */
616
644
 
617
645
struct t_weelist *
626
654
}
627
655
 
628
656
/*
629
 
 * hashtable_get_integer: get a hashtable property as integer
 
657
 * Gets a hashtable property as integer.
630
658
 */
631
659
 
632
660
int
644
672
}
645
673
 
646
674
/*
647
 
 * hashtable_compute_length_keys_cb: compute length of all keys
 
675
 * Computes length of all keys (callback called for each variable in hashtable).
648
676
 */
649
677
 
650
678
void
666
694
}
667
695
 
668
696
/*
669
 
 * hashtable_compute_length_values_cb: compute length of all values
 
697
 * Computes length of all values (callback called for each variable in
 
698
 * hashtable).
670
699
 */
671
700
 
672
701
void
695
724
}
696
725
 
697
726
/*
698
 
 * hashtable_compute_length_keys_values_cb: compute length of all keys + values
 
727
 * Computes length of all keys + values (callback called for each variable in
 
728
 * hashtable).
699
729
 */
700
730
 
701
731
void
708
738
}
709
739
 
710
740
/*
711
 
 * hashtable_build_string_keys_cb: build string with all keys
 
741
 * Builds a string with all keys (callback called for each variable in
 
742
 * hashtable).
712
743
 */
713
744
 
714
745
void
733
764
}
734
765
 
735
766
/*
736
 
 * hashtable_build_string_values_cb: build string with all values
 
767
 * Builds a string with all values (callback called for each variable in
 
768
 * hashtable).
737
769
 */
738
770
 
739
771
void
765
797
}
766
798
 
767
799
/*
768
 
 * hashtable_build_string_keys_values_cb: build string with all keys + values
 
800
 * Builds a string with all keys + values (callback called for each variable in
 
801
 * hashtable).
769
802
 */
770
803
 
771
804
void
800
833
}
801
834
 
802
835
/*
803
 
 * hashtable_get_keys_values: get keys and/or values of hashtable as string
804
 
 *                            string has format, one of:
805
 
 *                              keys only:     "key1,key2,key3"
806
 
 *                              values only:   "value1,value2,value3"
807
 
 *                              keys + values: "key1:value1,key2:value2,key3:value3"
 
836
 * Gets keys and/or values of hashtable as string.
 
837
 *
 
838
 * Returns a string with one of these formats:
 
839
 *   if keys == 1 and values == 0: "key1,key2,key3"
 
840
 *   if keys == 0 and values == 1: "value1,value2,value3"
 
841
 *   if keys == 1 and values == 1: "key1:value1,key2:value2,key3:value3"
808
842
 */
809
843
 
810
844
const char *
876
910
}
877
911
 
878
912
/*
879
 
 * hashtable_get_string: get a hashtable property as string
 
913
 * Gets a hashtable property as string.
880
914
 */
881
915
 
882
916
const char *
904
938
}
905
939
 
906
940
/*
907
 
 * hashtable_set_pointer: set a hashtable property (pointer)
 
941
 * Sets a hashtable property (pointer).
908
942
 */
909
943
 
910
944
void
919
953
}
920
954
 
921
955
/*
922
 
 * hashtable_add_to_infolist: add hashtable keys and values to infolist
923
 
 *                            return 1 if ok, 0 if error
 
956
 * Adds hashtable keys and values in an infolist.
 
957
 *
 
958
 * Returns:
 
959
 *   1: OK
 
960
 *   0: error
924
961
 */
925
962
 
926
963
int
987
1024
}
988
1025
 
989
1026
/*
990
 
 * hashtable_remove_item: remove an item from hashmap
 
1027
 * Removes an item from hashtable.
991
1028
 */
992
1029
 
993
1030
void
1016
1053
}
1017
1054
 
1018
1055
/*
1019
 
 * hashtable_remove: remove an item from hashmap (search it with key)
 
1056
 * Removes an item from hashtable (searches it with key).
1020
1057
 */
1021
1058
 
1022
1059
void
1034
1071
}
1035
1072
 
1036
1073
/*
1037
 
 * hashtable_remove_all: remove all items from hashmap
 
1074
 * Removes all items from hashtable.
1038
1075
 */
1039
1076
 
1040
1077
void
1055
1092
}
1056
1093
 
1057
1094
/*
1058
 
 * hashtable_free: free hashtable (remove all items and free hashtable)
 
1095
 * Frees a hashtable: removes all items and frees hashtable.
1059
1096
 */
1060
1097
 
1061
1098
void
1072
1109
}
1073
1110
 
1074
1111
/*
1075
 
 * hashtable_print_log: print hashtable in log (usually for crash dump)
 
1112
 * Prints hashtable in WeeChat log file (usually for crash dump).
1076
1113
 */
1077
1114
 
1078
1115
void