File: sathread.c

package info (click to toggle)
trn4 4.0-test77-18
  • links: PTS, VCS
  • area: non-free
  • in suites: sid, trixie
  • size: 4,016 kB
  • sloc: ansic: 48,332; sh: 6,795; tcl: 1,696; yacc: 662; perl: 108; makefile: 26
file content (133 lines) | stat: -rw-r--r-- 2,567 bytes parent folder | download | duplicates (11)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/* This file Copyright 1992 by Clifford A. Adams */
/* sathread.c
 *
 */

#include "EXTERN.h"
#include "common.h"
#ifdef SCAN_ART
#include "list.h"
#include "hash.h"
#include "cache.h"
#include "bits.h"	/* for absfirst */
#include "head.h"	/* hc_setspin() */
#include "ngdata.h"
#include "mempool.h"
#include "scanart.h"
#include "sadesc.h"	/* sa_desc_subject() */
#include "samain.h"
#include "samisc.h"
#include "sorder.h"
#include "util.h"
#include "INTERN.h"
#include "sathread.h"

static long sa_num_threads = 0;
static HASHTABLE* sa_thread_hash = 0;

void
sa_init_threads()
{
    mp_free(MP_SATHREAD);
    sa_num_threads = 0;
    if (sa_thread_hash) {
	hashdestroy(sa_thread_hash);
	sa_thread_hash = 0;
    }
}

/* called only if the macro didn't find a value */
/* XXX: dependent on hash feature that data.dat_len is not used in
 * the default comparison function, so it can be used for a number.
 * later: write a custom comparison function.
 */
long
sa_get_subj_thread(e)
long e;			/* entry number */
{
    HASHDATUM data;
    char* s;
    bool old_untrim;
    char* p;

    old_untrim = untrim_cache;
    untrim_cache = TRUE;
    s = sa_desc_subject(e);
    untrim_cache = old_untrim;

    if (!s || !*s)
      return -2;
    if ((*s == '>') && (s[1] == ' '))
	s += 2;

    if (!sa_thread_hash) {
	sa_thread_hash = hashcreate(401, HASH_DEFCMPFUNC);
    }
    data = hashfetch(sa_thread_hash,s,strlen(s));
    if (data.dat_ptr) {
	return (long)(data.dat_len);
    }
    p = mp_savestr(s,MP_SATHREAD);
    data = hashfetch(sa_thread_hash,p,strlen(s));
    data.dat_ptr = p;
    data.dat_len = (unsigned)(sa_num_threads+1);
    hashstorelast(data);
    sa_num_threads++;
    sa_ents[e].subj_thread_num = sa_num_threads;
    return sa_num_threads;
}

int
sa_subj_thread_count(a)
long a;
{
    int count;
    long b;

    count = 1;
    b = a;

    while ((b = sa_subj_thread_next(b)) != 0)
	if (sa_basic_elig(b))
	    count++;
    return count;
}

/* returns basic_elig previous subject thread */
long
sa_subj_thread_prev(a)
long a;
{
    int i,j;

    i = sa_subj_thread(a);
    while ((a = s_prev(a)) != 0) {
	if (!sa_basic_elig(a))
	    continue;
	if (!(j = sa_ents[a].subj_thread_num))
	    j = sa_subj_thread(a);
	if (i == j)
	    return a;
    }
    return 0L;
}

long
sa_subj_thread_next(a)
long a;
{
    int i,j;

    i = sa_subj_thread(a);
    while ((a = s_next(a)) != 0) {
	if (!sa_basic_elig(a))
	    continue;
	if (!(j = sa_ents[a].subj_thread_num))
	    j = sa_subj_thread(a);
	if (i == j)
	    return a;
    }
    return 0L;
}

#endif /* SCAN */