2 * Copyright © 2011 Red Hat Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 * Authors: Benjamin Otte <otte@gnome.org>
22 static inline GtkBitmask *
23 _gtk_bitmask_new (void)
25 return _gtk_bitmask_from_bits (0);
28 static inline GtkBitmask *
29 _gtk_bitmask_copy (const GtkBitmask *mask)
31 if (_gtk_bitmask_is_allocated (mask))
32 return _gtk_allocated_bitmask_copy (mask);
34 return (GtkBitmask *) mask;
38 _gtk_bitmask_free (GtkBitmask *mask)
40 if (_gtk_bitmask_is_allocated (mask))
41 return _gtk_allocated_bitmask_free (mask);
45 _gtk_bitmask_to_string (const GtkBitmask *mask)
49 string = g_string_new (NULL);
50 _gtk_allocated_bitmask_print (mask, string);
51 return g_string_free (string, FALSE);
55 _gtk_bitmask_print (const GtkBitmask *mask,
58 return _gtk_allocated_bitmask_print (mask, string);
61 static inline GtkBitmask *
62 _gtk_bitmask_intersect (GtkBitmask *mask,
63 const GtkBitmask *other)
65 return _gtk_allocated_bitmask_intersect (mask, other);
68 static inline GtkBitmask *
69 _gtk_bitmask_union (GtkBitmask *mask,
70 const GtkBitmask *other)
72 if (_gtk_bitmask_is_allocated (mask) ||
73 _gtk_bitmask_is_allocated (other))
74 return _gtk_allocated_bitmask_union (mask, other);
76 return _gtk_bitmask_from_bits (_gtk_bitmask_to_bits (mask)
77 | _gtk_bitmask_to_bits (other));
80 static inline GtkBitmask *
81 _gtk_bitmask_subtract (GtkBitmask *mask,
82 const GtkBitmask *other)
84 return _gtk_allocated_bitmask_subtract (mask, other);
87 static inline gboolean
88 _gtk_bitmask_get (const GtkBitmask *mask,
91 if (_gtk_bitmask_is_allocated (mask))
92 return _gtk_allocated_bitmask_get (mask, index_);
94 return index_ < GTK_BITMASK_N_DIRECT_BITS
95 ? !!(_gtk_bitmask_to_bits (mask) & (((size_t) 1) << index_))
99 static inline GtkBitmask *
100 _gtk_bitmask_set (GtkBitmask *mask,
104 if (_gtk_bitmask_is_allocated (mask) ||
105 (index_ >= GTK_BITMASK_N_DIRECT_BITS && value))
106 return _gtk_allocated_bitmask_set (mask, index_, value);
107 else if (index_ < GTK_BITMASK_N_DIRECT_BITS)
109 gsize bits = _gtk_bitmask_to_bits (mask);
112 bits |= ((size_t) 1) << index_;
114 bits &= ~(((size_t) 1) << index_);
116 return _gtk_bitmask_from_bits (bits);
122 static inline GtkBitmask *
123 _gtk_bitmask_invert_range (GtkBitmask *mask,
127 if (_gtk_bitmask_is_allocated (mask) ||
128 (end > GTK_BITMASK_N_DIRECT_BITS))
129 return _gtk_allocated_bitmask_invert_range (mask, start, end);
132 size_t invert = (((size_t) 1) << end) - (((size_t) 1) << start);
134 return _gtk_bitmask_from_bits (_gtk_bitmask_to_bits (mask) ^ invert);
138 static inline gboolean
139 _gtk_bitmask_is_empty (const GtkBitmask *mask)
141 return mask == _gtk_bitmask_from_bits (0);
144 static inline gboolean
145 _gtk_bitmask_equals (const GtkBitmask *mask,
146 const GtkBitmask *other)
151 if (!_gtk_bitmask_is_allocated (mask) ||
152 !_gtk_bitmask_is_allocated (other))
155 return _gtk_allocated_bitmask_equals (mask, other);
158 static inline gboolean
159 _gtk_bitmask_intersects (const GtkBitmask *mask,
160 const GtkBitmask *other)
162 if (_gtk_bitmask_is_allocated (mask) ||
163 _gtk_bitmask_is_allocated (other))
164 return _gtk_allocated_bitmask_intersects (mask, other);
166 return _gtk_bitmask_to_bits (mask) & _gtk_bitmask_to_bits (other) ? TRUE : FALSE;