File: compact_ulong_store.c

package info (click to toggle)
genometools 1.5.3-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 57,988 kB
  • ctags: 45,574
  • sloc: ansic: 475,937; ruby: 24,092; python: 4,519; sh: 3,014; perl: 2,523; makefile: 1,839; java: 158; haskell: 37; xml: 6; sed: 5
file content (166 lines) | stat: -rw-r--r-- 5,676 bytes parent folder | download | duplicates (4)
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
  Copyright (c) 2011 Stefan Kurtz <kurtz@zbh.uni-hamburg.de>
  Copyright (c) 2012 Dirk Willrodt <willrodt@zbh.uni-hamburg.de>
  Copyright (c) 2011-2012 Center for Bioinformatics, University of Hamburg

  Permission to use, copy, modify, and distribute this software for any
  purpose with or without fee is hereby granted, provided that the above
  copyright notice and this permission notice appear in all copies.

  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/

#include <limits.h>
#include "intbits.h"
#include "error_api.h"
#include "mathsupport.h"
#include "ensure.h"
#include "assert_api.h"
#include "compact_ulong_store.h"

struct GtCompactUlongStore
{
  GtUword *tab,
                numofentries,
                maskright;
  unsigned int bitsperentry,
               bitsleft;
};

GtCompactUlongStore *gt_compact_ulong_store_new(GtUword numofentries,
                                                unsigned int bitsperentry)
{
  GtCompactUlongStore *cus;
  GtUword arraysize, totalbits;

  /* if this assertion appears, then probably the 32-bit version of the
     code is used. Better use the 64-bit version by compiling with
     64bit=yes */
  gt_assert(numofentries <= ULONG_MAX/bitsperentry);
  gt_assert(bitsperentry <= (unsigned int) GT_INTWORDSIZE);
  totalbits = numofentries * bitsperentry;
  cus = gt_malloc(sizeof (*cus));
  cus->numofentries = numofentries;
  arraysize = GT_DIVWORDSIZE(totalbits);
  if (GT_MODWORDSIZE(totalbits) > 0) {
    arraysize++;
  }
  cus->tab = gt_calloc((size_t) arraysize,sizeof (*cus->tab));
  cus->bitsperentry = bitsperentry;
  cus->bitsleft = (unsigned int) GT_INTWORDSIZE - cus->bitsperentry;
  cus->maskright = ~0UL >> cus->bitsleft;
  return cus;
}

size_t gt_compact_ulong_store_size(GtUword numofentries,
                                   unsigned int bitsperentry)
{
  GtUword arraysize, totalbits;

  /* if this assertion appears, then probably the 32-bit version of the
     code is used. Better use the 64-bit version by compiling with
     64bit=yes */
  gt_assert(numofentries <= ULONG_MAX/bitsperentry);
  totalbits = numofentries * bitsperentry;
  arraysize = GT_DIVWORDSIZE(totalbits);
  if (GT_MODWORDSIZE(totalbits) > 0) {
    arraysize++;
  }
  return sizeof (GtCompactUlongStore) + sizeof (GtUword) * arraysize;
}

void gt_compact_ulong_store_delete(GtCompactUlongStore *cus)
{
  if (cus != NULL) {
    gt_free(cus->tab);
    gt_free(cus);
  }
}

GtUword gt_compact_ulong_store_get(const GtCompactUlongStore *cus,
                                         GtUword idx)
{
  unsigned int unitoffset;
  GtUword unitindex;

  gt_assert(idx < cus->numofentries);
  idx *= cus->bitsperentry;
  unitoffset = (unsigned int) GT_MODWORDSIZE(idx);
  unitindex = GT_DIVWORDSIZE(idx);
  if (unitoffset <= (unsigned int) cus->bitsleft) {
    return (GtUword) (cus->tab[unitindex] >>
                             (cus->bitsleft - unitoffset))
           & cus->maskright;
  } else {
    return (GtUword)
           ((cus->tab[unitindex] <<
                (unitoffset + cus->bitsperentry - GT_INTWORDSIZE)) |
              (cus->tab[unitindex+1] >>
                (GT_INTWORDSIZE + cus->bitsleft - unitoffset))) &
           cus->maskright;
  }
}

void gt_compact_ulong_store_update(GtCompactUlongStore *cus,
                                   GtUword idx, GtUword value)
{
  unsigned int unitoffset;
  GtUword unitindex;

  gt_assert(idx < cus->numofentries && value <= cus->maskright);
  idx *= cus->bitsperentry;
  unitoffset = (unsigned int) GT_MODWORDSIZE(idx);
  unitindex = GT_DIVWORDSIZE(idx);
  if (unitoffset <= (unsigned int) cus->bitsleft) {
    unsigned int shiftleft = cus->bitsleft - unitoffset;

    cus->tab[unitindex]
      = (GtUword) (cus->tab[unitindex] & ~(cus->maskright << shiftleft)) |
        (GtUword) (value << shiftleft);
  } else {
    unsigned int shift = unitoffset - cus->bitsleft;

    cus->tab[unitindex]
      = (GtUword) (cus->tab[unitindex] & ~(cus->maskright >> shift)) |
        (GtUword)(value >> shift);
    shift = (unsigned int) GT_INTWORDSIZE - shift;
    cus->tab[unitindex+1]
      = (GtUword) (cus->tab[unitindex+1] & ~(cus->maskright << shift)) |
        (GtUword) (value << shift);
  }
}

int gt_compact_ulong_store_unit_test(GtError *err)
{
  GtCompactUlongStore *cus;
  const GtUword constnums = 100000UL;
  unsigned int bits;
  GtUword value, nums, idx, numforbits, *checknumbers;
  int had_err = 0;

  checknumbers = gt_malloc(sizeof (*checknumbers) * constnums);
  for (bits = 1U; bits <= (unsigned int) GT_INTWORDSIZE-1; bits++) {
    numforbits = 1UL << bits;
    nums = numforbits < constnums ? numforbits : constnums;
    cus = gt_compact_ulong_store_new(nums,bits);
    for (idx = 0; idx < nums; idx++) {
      checknumbers[idx] = nums == constnums ? gt_rand_max(numforbits-1) : idx;
      gt_compact_ulong_store_update(cus,idx,checknumbers[idx]);
      value = gt_compact_ulong_store_get(cus,idx);
      gt_ensure(checknumbers[idx] == value);
    }
    for (idx = 0; had_err == 0 && idx < nums; idx++) {
      value = gt_compact_ulong_store_get(cus,idx);
      gt_ensure(checknumbers[idx] == value);
    }
    gt_compact_ulong_store_delete(cus);
  }
  gt_free(checknumbers);
  return had_err;
}