summaryrefslogtreecommitdiff
path: root/storage/oqgraph/oqgraph_judy.cc
blob: 0ad9f9fe1f53dba91b0d094fa84879cd90e038de (plain)
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
/* Copyright (C) 2007-2013 Arjen G Lentz & Antony T Curtis for Open Query

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335 USA */

/* ======================================================================
   Open Query Graph Computation Engine, based on a concept by Arjen Lentz
   v3 implementation by Antony Curtis, Arjen Lentz, Andrew McDonnell
   For more information, documentation, support, enhancement engineering,
   see http://openquery.com/graph or contact graph@openquery.com
   ======================================================================
*/

#include "oqgraph_judy.h"

/*
  Currently the only active code that can return error is:
    judy_bitset::reset()/J1U()
    judy_bitset::setbit()/J1S()

  In most cases errors are either about wrong parameters passed to Judy
  functions or internal structures corruption. These definitely deserve
  abnormal process termination instead of exit() as it is done by original
  JUDYERROR.

  TODO: there's one exception that should be handled properly though: OOM.
*/
#include <stdio.h>
#define JUDYERROR(CallerFile, CallerLine, JudyFunc, JudyErrno, JudyErrID) \
    {                                                                     \
        (void) fprintf(stderr, "File '%s', line %d: %s(), "               \
           "JU_ERRNO_* == %d, ID == %d\n",                                \
           CallerFile, CallerLine,                                        \
           JudyFunc, JudyErrno, JudyErrID);                               \
        abort();                                                          \
    }
#include <Judy.h>

void open_query::judy_bitset::clear()
{
  int rc;
  J1FA(rc, array);
}

bool open_query::judy_bitset::test(size_type n) const
{
  int rc;
  J1T(rc, array, n);
  return rc == 1;
}

open_query::judy_bitset& open_query::judy_bitset::setbit(size_type n)
{
  int rc;
  J1S(rc, array, n);
  return *this;
}

open_query::judy_bitset& open_query::judy_bitset::reset(size_type n)
{
  int rc;
  J1U(rc, array, n);
  return *this;
}

open_query::judy_bitset& open_query::judy_bitset::flip(size_type n)
{
  int rc;
  J1U(rc, array, n);
  if (!rc)
  {
    J1S(rc, array, n);
  }
  return *this;
}

open_query::judy_bitset::size_type open_query::judy_bitset::num_blocks() const
{
  Word_t rc;
  J1MU(rc, array);
  return rc;
}

open_query::judy_bitset::size_type open_query::judy_bitset::size() const
{
  int rc;
  Word_t index = (Word_t) -1;
  J1L(rc, array, index);
  if (!rc)
    return index;
  else
    return npos;
}

open_query::judy_bitset::size_type open_query::judy_bitset::count() const
{
  Word_t rc;
  J1C(rc, array, 0, -1);
  return rc;
}

open_query::judy_bitset& open_query::judy_bitset::set(const judy_bitset& src)
{
  if (!src.empty())
  {
    for (size_type pos= src.find_first(); pos != npos; pos= src.find_next(pos))
    {
      set(pos);
    }
  }
  return *this;
}


open_query::judy_bitset::size_type open_query::judy_bitset::find_first() const
{
  int rc;
  Word_t index = 0;
  J1F(rc, array, index);
  if (!rc)
    return index;
  else
    return npos;
}

open_query::judy_bitset::size_type open_query::judy_bitset::find_next(size_type n) const
{
  int rc;
  Word_t index = (Word_t) n;
  J1N(rc, array, index);
  if (!rc)
    return index;
  else
    return npos;
}