1*1da177e4SLinus Torvalds /* 2*1da177e4SLinus Torvalds Red Black Trees 3*1da177e4SLinus Torvalds (C) 1999 Andrea Arcangeli <andrea@suse.de> 4*1da177e4SLinus Torvalds (C) 2002 David Woodhouse <dwmw2@infradead.org> 5*1da177e4SLinus Torvalds 6*1da177e4SLinus Torvalds This program is free software; you can redistribute it and/or modify 7*1da177e4SLinus Torvalds it under the terms of the GNU General Public License as published by 8*1da177e4SLinus Torvalds the Free Software Foundation; either version 2 of the License, or 9*1da177e4SLinus Torvalds (at your option) any later version. 10*1da177e4SLinus Torvalds 11*1da177e4SLinus Torvalds This program is distributed in the hope that it will be useful, 12*1da177e4SLinus Torvalds but WITHOUT ANY WARRANTY; without even the implied warranty of 13*1da177e4SLinus Torvalds MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14*1da177e4SLinus Torvalds GNU General Public License for more details. 15*1da177e4SLinus Torvalds 16*1da177e4SLinus Torvalds You should have received a copy of the GNU General Public License 17*1da177e4SLinus Torvalds along with this program; if not, write to the Free Software 18*1da177e4SLinus Torvalds Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 19*1da177e4SLinus Torvalds 20*1da177e4SLinus Torvalds linux/lib/rbtree.c 21*1da177e4SLinus Torvalds */ 22*1da177e4SLinus Torvalds 23*1da177e4SLinus Torvalds #include <linux/rbtree.h> 24*1da177e4SLinus Torvalds #include <linux/module.h> 25*1da177e4SLinus Torvalds 26*1da177e4SLinus Torvalds static void __rb_rotate_left(struct rb_node *node, struct rb_root *root) 27*1da177e4SLinus Torvalds { 28*1da177e4SLinus Torvalds struct rb_node *right = node->rb_right; 29*1da177e4SLinus Torvalds 30*1da177e4SLinus Torvalds if ((node->rb_right = right->rb_left)) 31*1da177e4SLinus Torvalds right->rb_left->rb_parent = node; 32*1da177e4SLinus Torvalds right->rb_left = node; 33*1da177e4SLinus Torvalds 34*1da177e4SLinus Torvalds if ((right->rb_parent = node->rb_parent)) 35*1da177e4SLinus Torvalds { 36*1da177e4SLinus Torvalds if (node == node->rb_parent->rb_left) 37*1da177e4SLinus Torvalds node->rb_parent->rb_left = right; 38*1da177e4SLinus Torvalds else 39*1da177e4SLinus Torvalds node->rb_parent->rb_right = right; 40*1da177e4SLinus Torvalds } 41*1da177e4SLinus Torvalds else 42*1da177e4SLinus Torvalds root->rb_node = right; 43*1da177e4SLinus Torvalds node->rb_parent = right; 44*1da177e4SLinus Torvalds } 45*1da177e4SLinus Torvalds 46*1da177e4SLinus Torvalds static void __rb_rotate_right(struct rb_node *node, struct rb_root *root) 47*1da177e4SLinus Torvalds { 48*1da177e4SLinus Torvalds struct rb_node *left = node->rb_left; 49*1da177e4SLinus Torvalds 50*1da177e4SLinus Torvalds if ((node->rb_left = left->rb_right)) 51*1da177e4SLinus Torvalds left->rb_right->rb_parent = node; 52*1da177e4SLinus Torvalds left->rb_right = node; 53*1da177e4SLinus Torvalds 54*1da177e4SLinus Torvalds if ((left->rb_parent = node->rb_parent)) 55*1da177e4SLinus Torvalds { 56*1da177e4SLinus Torvalds if (node == node->rb_parent->rb_right) 57*1da177e4SLinus Torvalds node->rb_parent->rb_right = left; 58*1da177e4SLinus Torvalds else 59*1da177e4SLinus Torvalds node->rb_parent->rb_left = left; 60*1da177e4SLinus Torvalds } 61*1da177e4SLinus Torvalds else 62*1da177e4SLinus Torvalds root->rb_node = left; 63*1da177e4SLinus Torvalds node->rb_parent = left; 64*1da177e4SLinus Torvalds } 65*1da177e4SLinus Torvalds 66*1da177e4SLinus Torvalds void rb_insert_color(struct rb_node *node, struct rb_root *root) 67*1da177e4SLinus Torvalds { 68*1da177e4SLinus Torvalds struct rb_node *parent, *gparent; 69*1da177e4SLinus Torvalds 70*1da177e4SLinus Torvalds while ((parent = node->rb_parent) && parent->rb_color == RB_RED) 71*1da177e4SLinus Torvalds { 72*1da177e4SLinus Torvalds gparent = parent->rb_parent; 73*1da177e4SLinus Torvalds 74*1da177e4SLinus Torvalds if (parent == gparent->rb_left) 75*1da177e4SLinus Torvalds { 76*1da177e4SLinus Torvalds { 77*1da177e4SLinus Torvalds register struct rb_node *uncle = gparent->rb_right; 78*1da177e4SLinus Torvalds if (uncle && uncle->rb_color == RB_RED) 79*1da177e4SLinus Torvalds { 80*1da177e4SLinus Torvalds uncle->rb_color = RB_BLACK; 81*1da177e4SLinus Torvalds parent->rb_color = RB_BLACK; 82*1da177e4SLinus Torvalds gparent->rb_color = RB_RED; 83*1da177e4SLinus Torvalds node = gparent; 84*1da177e4SLinus Torvalds continue; 85*1da177e4SLinus Torvalds } 86*1da177e4SLinus Torvalds } 87*1da177e4SLinus Torvalds 88*1da177e4SLinus Torvalds if (parent->rb_right == node) 89*1da177e4SLinus Torvalds { 90*1da177e4SLinus Torvalds register struct rb_node *tmp; 91*1da177e4SLinus Torvalds __rb_rotate_left(parent, root); 92*1da177e4SLinus Torvalds tmp = parent; 93*1da177e4SLinus Torvalds parent = node; 94*1da177e4SLinus Torvalds node = tmp; 95*1da177e4SLinus Torvalds } 96*1da177e4SLinus Torvalds 97*1da177e4SLinus Torvalds parent->rb_color = RB_BLACK; 98*1da177e4SLinus Torvalds gparent->rb_color = RB_RED; 99*1da177e4SLinus Torvalds __rb_rotate_right(gparent, root); 100*1da177e4SLinus Torvalds } else { 101*1da177e4SLinus Torvalds { 102*1da177e4SLinus Torvalds register struct rb_node *uncle = gparent->rb_left; 103*1da177e4SLinus Torvalds if (uncle && uncle->rb_color == RB_RED) 104*1da177e4SLinus Torvalds { 105*1da177e4SLinus Torvalds uncle->rb_color = RB_BLACK; 106*1da177e4SLinus Torvalds parent->rb_color = RB_BLACK; 107*1da177e4SLinus Torvalds gparent->rb_color = RB_RED; 108*1da177e4SLinus Torvalds node = gparent; 109*1da177e4SLinus Torvalds continue; 110*1da177e4SLinus Torvalds } 111*1da177e4SLinus Torvalds } 112*1da177e4SLinus Torvalds 113*1da177e4SLinus Torvalds if (parent->rb_left == node) 114*1da177e4SLinus Torvalds { 115*1da177e4SLinus Torvalds register struct rb_node *tmp; 116*1da177e4SLinus Torvalds __rb_rotate_right(parent, root); 117*1da177e4SLinus Torvalds tmp = parent; 118*1da177e4SLinus Torvalds parent = node; 119*1da177e4SLinus Torvalds node = tmp; 120*1da177e4SLinus Torvalds } 121*1da177e4SLinus Torvalds 122*1da177e4SLinus Torvalds parent->rb_color = RB_BLACK; 123*1da177e4SLinus Torvalds gparent->rb_color = RB_RED; 124*1da177e4SLinus Torvalds __rb_rotate_left(gparent, root); 125*1da177e4SLinus Torvalds } 126*1da177e4SLinus Torvalds } 127*1da177e4SLinus Torvalds 128*1da177e4SLinus Torvalds root->rb_node->rb_color = RB_BLACK; 129*1da177e4SLinus Torvalds } 130*1da177e4SLinus Torvalds EXPORT_SYMBOL(rb_insert_color); 131*1da177e4SLinus Torvalds 132*1da177e4SLinus Torvalds static void __rb_erase_color(struct rb_node *node, struct rb_node *parent, 133*1da177e4SLinus Torvalds struct rb_root *root) 134*1da177e4SLinus Torvalds { 135*1da177e4SLinus Torvalds struct rb_node *other; 136*1da177e4SLinus Torvalds 137*1da177e4SLinus Torvalds while ((!node || node->rb_color == RB_BLACK) && node != root->rb_node) 138*1da177e4SLinus Torvalds { 139*1da177e4SLinus Torvalds if (parent->rb_left == node) 140*1da177e4SLinus Torvalds { 141*1da177e4SLinus Torvalds other = parent->rb_right; 142*1da177e4SLinus Torvalds if (other->rb_color == RB_RED) 143*1da177e4SLinus Torvalds { 144*1da177e4SLinus Torvalds other->rb_color = RB_BLACK; 145*1da177e4SLinus Torvalds parent->rb_color = RB_RED; 146*1da177e4SLinus Torvalds __rb_rotate_left(parent, root); 147*1da177e4SLinus Torvalds other = parent->rb_right; 148*1da177e4SLinus Torvalds } 149*1da177e4SLinus Torvalds if ((!other->rb_left || 150*1da177e4SLinus Torvalds other->rb_left->rb_color == RB_BLACK) 151*1da177e4SLinus Torvalds && (!other->rb_right || 152*1da177e4SLinus Torvalds other->rb_right->rb_color == RB_BLACK)) 153*1da177e4SLinus Torvalds { 154*1da177e4SLinus Torvalds other->rb_color = RB_RED; 155*1da177e4SLinus Torvalds node = parent; 156*1da177e4SLinus Torvalds parent = node->rb_parent; 157*1da177e4SLinus Torvalds } 158*1da177e4SLinus Torvalds else 159*1da177e4SLinus Torvalds { 160*1da177e4SLinus Torvalds if (!other->rb_right || 161*1da177e4SLinus Torvalds other->rb_right->rb_color == RB_BLACK) 162*1da177e4SLinus Torvalds { 163*1da177e4SLinus Torvalds register struct rb_node *o_left; 164*1da177e4SLinus Torvalds if ((o_left = other->rb_left)) 165*1da177e4SLinus Torvalds o_left->rb_color = RB_BLACK; 166*1da177e4SLinus Torvalds other->rb_color = RB_RED; 167*1da177e4SLinus Torvalds __rb_rotate_right(other, root); 168*1da177e4SLinus Torvalds other = parent->rb_right; 169*1da177e4SLinus Torvalds } 170*1da177e4SLinus Torvalds other->rb_color = parent->rb_color; 171*1da177e4SLinus Torvalds parent->rb_color = RB_BLACK; 172*1da177e4SLinus Torvalds if (other->rb_right) 173*1da177e4SLinus Torvalds other->rb_right->rb_color = RB_BLACK; 174*1da177e4SLinus Torvalds __rb_rotate_left(parent, root); 175*1da177e4SLinus Torvalds node = root->rb_node; 176*1da177e4SLinus Torvalds break; 177*1da177e4SLinus Torvalds } 178*1da177e4SLinus Torvalds } 179*1da177e4SLinus Torvalds else 180*1da177e4SLinus Torvalds { 181*1da177e4SLinus Torvalds other = parent->rb_left; 182*1da177e4SLinus Torvalds if (other->rb_color == RB_RED) 183*1da177e4SLinus Torvalds { 184*1da177e4SLinus Torvalds other->rb_color = RB_BLACK; 185*1da177e4SLinus Torvalds parent->rb_color = RB_RED; 186*1da177e4SLinus Torvalds __rb_rotate_right(parent, root); 187*1da177e4SLinus Torvalds other = parent->rb_left; 188*1da177e4SLinus Torvalds } 189*1da177e4SLinus Torvalds if ((!other->rb_left || 190*1da177e4SLinus Torvalds other->rb_left->rb_color == RB_BLACK) 191*1da177e4SLinus Torvalds && (!other->rb_right || 192*1da177e4SLinus Torvalds other->rb_right->rb_color == RB_BLACK)) 193*1da177e4SLinus Torvalds { 194*1da177e4SLinus Torvalds other->rb_color = RB_RED; 195*1da177e4SLinus Torvalds node = parent; 196*1da177e4SLinus Torvalds parent = node->rb_parent; 197*1da177e4SLinus Torvalds } 198*1da177e4SLinus Torvalds else 199*1da177e4SLinus Torvalds { 200*1da177e4SLinus Torvalds if (!other->rb_left || 201*1da177e4SLinus Torvalds other->rb_left->rb_color == RB_BLACK) 202*1da177e4SLinus Torvalds { 203*1da177e4SLinus Torvalds register struct rb_node *o_right; 204*1da177e4SLinus Torvalds if ((o_right = other->rb_right)) 205*1da177e4SLinus Torvalds o_right->rb_color = RB_BLACK; 206*1da177e4SLinus Torvalds other->rb_color = RB_RED; 207*1da177e4SLinus Torvalds __rb_rotate_left(other, root); 208*1da177e4SLinus Torvalds other = parent->rb_left; 209*1da177e4SLinus Torvalds } 210*1da177e4SLinus Torvalds other->rb_color = parent->rb_color; 211*1da177e4SLinus Torvalds parent->rb_color = RB_BLACK; 212*1da177e4SLinus Torvalds if (other->rb_left) 213*1da177e4SLinus Torvalds other->rb_left->rb_color = RB_BLACK; 214*1da177e4SLinus Torvalds __rb_rotate_right(parent, root); 215*1da177e4SLinus Torvalds node = root->rb_node; 216*1da177e4SLinus Torvalds break; 217*1da177e4SLinus Torvalds } 218*1da177e4SLinus Torvalds } 219*1da177e4SLinus Torvalds } 220*1da177e4SLinus Torvalds if (node) 221*1da177e4SLinus Torvalds node->rb_color = RB_BLACK; 222*1da177e4SLinus Torvalds } 223*1da177e4SLinus Torvalds 224*1da177e4SLinus Torvalds void rb_erase(struct rb_node *node, struct rb_root *root) 225*1da177e4SLinus Torvalds { 226*1da177e4SLinus Torvalds struct rb_node *child, *parent; 227*1da177e4SLinus Torvalds int color; 228*1da177e4SLinus Torvalds 229*1da177e4SLinus Torvalds if (!node->rb_left) 230*1da177e4SLinus Torvalds child = node->rb_right; 231*1da177e4SLinus Torvalds else if (!node->rb_right) 232*1da177e4SLinus Torvalds child = node->rb_left; 233*1da177e4SLinus Torvalds else 234*1da177e4SLinus Torvalds { 235*1da177e4SLinus Torvalds struct rb_node *old = node, *left; 236*1da177e4SLinus Torvalds 237*1da177e4SLinus Torvalds node = node->rb_right; 238*1da177e4SLinus Torvalds while ((left = node->rb_left) != NULL) 239*1da177e4SLinus Torvalds node = left; 240*1da177e4SLinus Torvalds child = node->rb_right; 241*1da177e4SLinus Torvalds parent = node->rb_parent; 242*1da177e4SLinus Torvalds color = node->rb_color; 243*1da177e4SLinus Torvalds 244*1da177e4SLinus Torvalds if (child) 245*1da177e4SLinus Torvalds child->rb_parent = parent; 246*1da177e4SLinus Torvalds if (parent) 247*1da177e4SLinus Torvalds { 248*1da177e4SLinus Torvalds if (parent->rb_left == node) 249*1da177e4SLinus Torvalds parent->rb_left = child; 250*1da177e4SLinus Torvalds else 251*1da177e4SLinus Torvalds parent->rb_right = child; 252*1da177e4SLinus Torvalds } 253*1da177e4SLinus Torvalds else 254*1da177e4SLinus Torvalds root->rb_node = child; 255*1da177e4SLinus Torvalds 256*1da177e4SLinus Torvalds if (node->rb_parent == old) 257*1da177e4SLinus Torvalds parent = node; 258*1da177e4SLinus Torvalds node->rb_parent = old->rb_parent; 259*1da177e4SLinus Torvalds node->rb_color = old->rb_color; 260*1da177e4SLinus Torvalds node->rb_right = old->rb_right; 261*1da177e4SLinus Torvalds node->rb_left = old->rb_left; 262*1da177e4SLinus Torvalds 263*1da177e4SLinus Torvalds if (old->rb_parent) 264*1da177e4SLinus Torvalds { 265*1da177e4SLinus Torvalds if (old->rb_parent->rb_left == old) 266*1da177e4SLinus Torvalds old->rb_parent->rb_left = node; 267*1da177e4SLinus Torvalds else 268*1da177e4SLinus Torvalds old->rb_parent->rb_right = node; 269*1da177e4SLinus Torvalds } else 270*1da177e4SLinus Torvalds root->rb_node = node; 271*1da177e4SLinus Torvalds 272*1da177e4SLinus Torvalds old->rb_left->rb_parent = node; 273*1da177e4SLinus Torvalds if (old->rb_right) 274*1da177e4SLinus Torvalds old->rb_right->rb_parent = node; 275*1da177e4SLinus Torvalds goto color; 276*1da177e4SLinus Torvalds } 277*1da177e4SLinus Torvalds 278*1da177e4SLinus Torvalds parent = node->rb_parent; 279*1da177e4SLinus Torvalds color = node->rb_color; 280*1da177e4SLinus Torvalds 281*1da177e4SLinus Torvalds if (child) 282*1da177e4SLinus Torvalds child->rb_parent = parent; 283*1da177e4SLinus Torvalds if (parent) 284*1da177e4SLinus Torvalds { 285*1da177e4SLinus Torvalds if (parent->rb_left == node) 286*1da177e4SLinus Torvalds parent->rb_left = child; 287*1da177e4SLinus Torvalds else 288*1da177e4SLinus Torvalds parent->rb_right = child; 289*1da177e4SLinus Torvalds } 290*1da177e4SLinus Torvalds else 291*1da177e4SLinus Torvalds root->rb_node = child; 292*1da177e4SLinus Torvalds 293*1da177e4SLinus Torvalds color: 294*1da177e4SLinus Torvalds if (color == RB_BLACK) 295*1da177e4SLinus Torvalds __rb_erase_color(child, parent, root); 296*1da177e4SLinus Torvalds } 297*1da177e4SLinus Torvalds EXPORT_SYMBOL(rb_erase); 298*1da177e4SLinus Torvalds 299*1da177e4SLinus Torvalds /* 300*1da177e4SLinus Torvalds * This function returns the first node (in sort order) of the tree. 301*1da177e4SLinus Torvalds */ 302*1da177e4SLinus Torvalds struct rb_node *rb_first(struct rb_root *root) 303*1da177e4SLinus Torvalds { 304*1da177e4SLinus Torvalds struct rb_node *n; 305*1da177e4SLinus Torvalds 306*1da177e4SLinus Torvalds n = root->rb_node; 307*1da177e4SLinus Torvalds if (!n) 308*1da177e4SLinus Torvalds return NULL; 309*1da177e4SLinus Torvalds while (n->rb_left) 310*1da177e4SLinus Torvalds n = n->rb_left; 311*1da177e4SLinus Torvalds return n; 312*1da177e4SLinus Torvalds } 313*1da177e4SLinus Torvalds EXPORT_SYMBOL(rb_first); 314*1da177e4SLinus Torvalds 315*1da177e4SLinus Torvalds struct rb_node *rb_last(struct rb_root *root) 316*1da177e4SLinus Torvalds { 317*1da177e4SLinus Torvalds struct rb_node *n; 318*1da177e4SLinus Torvalds 319*1da177e4SLinus Torvalds n = root->rb_node; 320*1da177e4SLinus Torvalds if (!n) 321*1da177e4SLinus Torvalds return NULL; 322*1da177e4SLinus Torvalds while (n->rb_right) 323*1da177e4SLinus Torvalds n = n->rb_right; 324*1da177e4SLinus Torvalds return n; 325*1da177e4SLinus Torvalds } 326*1da177e4SLinus Torvalds EXPORT_SYMBOL(rb_last); 327*1da177e4SLinus Torvalds 328*1da177e4SLinus Torvalds struct rb_node *rb_next(struct rb_node *node) 329*1da177e4SLinus Torvalds { 330*1da177e4SLinus Torvalds /* If we have a right-hand child, go down and then left as far 331*1da177e4SLinus Torvalds as we can. */ 332*1da177e4SLinus Torvalds if (node->rb_right) { 333*1da177e4SLinus Torvalds node = node->rb_right; 334*1da177e4SLinus Torvalds while (node->rb_left) 335*1da177e4SLinus Torvalds node=node->rb_left; 336*1da177e4SLinus Torvalds return node; 337*1da177e4SLinus Torvalds } 338*1da177e4SLinus Torvalds 339*1da177e4SLinus Torvalds /* No right-hand children. Everything down and left is 340*1da177e4SLinus Torvalds smaller than us, so any 'next' node must be in the general 341*1da177e4SLinus Torvalds direction of our parent. Go up the tree; any time the 342*1da177e4SLinus Torvalds ancestor is a right-hand child of its parent, keep going 343*1da177e4SLinus Torvalds up. First time it's a left-hand child of its parent, said 344*1da177e4SLinus Torvalds parent is our 'next' node. */ 345*1da177e4SLinus Torvalds while (node->rb_parent && node == node->rb_parent->rb_right) 346*1da177e4SLinus Torvalds node = node->rb_parent; 347*1da177e4SLinus Torvalds 348*1da177e4SLinus Torvalds return node->rb_parent; 349*1da177e4SLinus Torvalds } 350*1da177e4SLinus Torvalds EXPORT_SYMBOL(rb_next); 351*1da177e4SLinus Torvalds 352*1da177e4SLinus Torvalds struct rb_node *rb_prev(struct rb_node *node) 353*1da177e4SLinus Torvalds { 354*1da177e4SLinus Torvalds /* If we have a left-hand child, go down and then right as far 355*1da177e4SLinus Torvalds as we can. */ 356*1da177e4SLinus Torvalds if (node->rb_left) { 357*1da177e4SLinus Torvalds node = node->rb_left; 358*1da177e4SLinus Torvalds while (node->rb_right) 359*1da177e4SLinus Torvalds node=node->rb_right; 360*1da177e4SLinus Torvalds return node; 361*1da177e4SLinus Torvalds } 362*1da177e4SLinus Torvalds 363*1da177e4SLinus Torvalds /* No left-hand children. Go up till we find an ancestor which 364*1da177e4SLinus Torvalds is a right-hand child of its parent */ 365*1da177e4SLinus Torvalds while (node->rb_parent && node == node->rb_parent->rb_left) 366*1da177e4SLinus Torvalds node = node->rb_parent; 367*1da177e4SLinus Torvalds 368*1da177e4SLinus Torvalds return node->rb_parent; 369*1da177e4SLinus Torvalds } 370*1da177e4SLinus Torvalds EXPORT_SYMBOL(rb_prev); 371*1da177e4SLinus Torvalds 372*1da177e4SLinus Torvalds void rb_replace_node(struct rb_node *victim, struct rb_node *new, 373*1da177e4SLinus Torvalds struct rb_root *root) 374*1da177e4SLinus Torvalds { 375*1da177e4SLinus Torvalds struct rb_node *parent = victim->rb_parent; 376*1da177e4SLinus Torvalds 377*1da177e4SLinus Torvalds /* Set the surrounding nodes to point to the replacement */ 378*1da177e4SLinus Torvalds if (parent) { 379*1da177e4SLinus Torvalds if (victim == parent->rb_left) 380*1da177e4SLinus Torvalds parent->rb_left = new; 381*1da177e4SLinus Torvalds else 382*1da177e4SLinus Torvalds parent->rb_right = new; 383*1da177e4SLinus Torvalds } else { 384*1da177e4SLinus Torvalds root->rb_node = new; 385*1da177e4SLinus Torvalds } 386*1da177e4SLinus Torvalds if (victim->rb_left) 387*1da177e4SLinus Torvalds victim->rb_left->rb_parent = new; 388*1da177e4SLinus Torvalds if (victim->rb_right) 389*1da177e4SLinus Torvalds victim->rb_right->rb_parent = new; 390*1da177e4SLinus Torvalds 391*1da177e4SLinus Torvalds /* Copy the pointers/colour from the victim to the replacement */ 392*1da177e4SLinus Torvalds *new = *victim; 393*1da177e4SLinus Torvalds } 394*1da177e4SLinus Torvalds EXPORT_SYMBOL(rb_replace_node); 395