Merge from emacs-24; up to 2014-06-06T02:22:40Z!monnier@iro.umontreal.ca
[bpt/emacs.git] / oldXMenu / XMakeAssoc.c
CommitLineData
e745ede7
DL
1/* Copyright Massachusetts Institute of Technology 1985 */
2
0ff3f435
GM
3#include "copyright.h"
4
e745ede7 5
55660072 6#include "XMenuInt.h"
e745ede7 7#include <X11/Xresource.h>
e745ede7
DL
8#include <errno.h>
9
10#ifndef NULL
11#define NULL 0
12#endif
13
e745ede7
DL
14/*
15 * XMakeAssoc - Insert data into an XAssocTable keyed on an XId.
16 * Data is inserted into the table only once. Redundant inserts are
17 * meaningless (but cause no problems). The queue in each association
18 * bucket is sorted (lowest XId to highest XId).
19 */
76320384 20void
55660072 21XMakeAssoc(register Display *dpy, register XAssocTable *table, register XID x_id, register void *data)
e745ede7
DL
22{
23 int hash;
24 register XAssoc *bucket;
25 register XAssoc *Entry;
26 register XAssoc *new_entry;
177c0ea7 27
e745ede7
DL
28 /* Hash the XId to get the bucket number. */
29 hash = x_id & (table->size - 1);
30 /* Look up the bucket to get the entries in that bucket. */
31 bucket = &table->buckets[hash];
32 /* Get the first entry in the bucket. */
33 Entry = bucket->next;
34
35 /* If (Entry != bucket), the bucket is empty so make */
36 /* the new entry the first entry in the bucket. */
37 /* if (Entry == bucket), the we have to search the */
38 /* bucket. */
39 if (Entry != bucket) {
40 /* The bucket isn't empty, begin searching. */
41 /* If we leave the for loop then we have either passed */
42 /* where the entry should be or hit the end of the bucket. */
43 /* In either case we should then insert the new entry */
44 /* before the current value of "Entry". */
45 for (; Entry != bucket; Entry = Entry->next) {
46 if (Entry->x_id == x_id) {
47 /* Entry has the same XId... */
48 if (Entry->display == dpy) {
49 /* Entry has the same Display... */
50 /* Therefore there is already an */
51 /* entry with this XId and Display, */
52 /* reset its data value and return. */
53 Entry->data = data;
54 return;
55 }
56 /* We found an association with the right */
57 /* id but the wrong display! */
58 continue;
59 }
60 /* If the current entry's XId is greater than the */
61 /* XId of the entry to be inserted then we have */
62 /* passed the location where the new XId should */
63 /* be inserted. */
64 if (Entry->x_id > x_id) break;
65 }
66 }
67
68 /* If we are here then the new entry should be inserted just */
69 /* before the current value of "Entry". */
70 /* Create a new XAssoc and load it with new provided data. */
bdf41b96 71 new_entry = (XAssoc *) malloc(sizeof(XAssoc));
e745ede7
DL
72 new_entry->display = dpy;
73 new_entry->x_id = x_id;
74 new_entry->data = data;
75
76 /* Insert the new entry. */
77 emacs_insque((struct qelem *)new_entry, (struct qelem *)Entry->prev);
78}