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 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552
|
-- This file is free software, which comes along with SmartEiffel. This
-- software 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. You can modify it as you want, provided
-- this header is kept unaltered, and a notification of the changes is added.
-- You are allowed to redistribute it and sell it, alone or as a part of
-- another product.
--
-- Copyright(C) 1994-2002: INRIA - LORIA (INRIA Lorraine) - ESIAL U.H.P.
-- - University of Nancy 1 - FRANCE
-- Copyright(C) 2003: INRIA - LORIA (INRIA Lorraine) - I.U.T. Charlemagne
-- - University of Nancy 2 - FRANCE
--
-- Dominique COLNET, Suzanne COLLIN, Olivier ZENDRA,
-- Philippe RIBET, Cyril ADRIAN
--
-- http://SmartEiffel.loria.fr - SmartEiffel@loria.fr
--
class TWO_WAY_LINKED_LIST[E]
--
-- Two way linked list with internal automatic memorization of
-- the last access.
--
inherit LINKED_COLLECTION[E]
redefine reverse end
creation make, from_collection
feature {TWO_WAY_LINKED_LIST,ITERATOR_ON_TWO_WAY_LINKED_LIST}
first_link: LINK2[E]
-- Void when empty or gives access to the first element.
feature {TWO_WAY_LINKED_LIST}
last_link: like first_link
-- Void when empty or gives access to the last element.
mem_idx: INTEGER; mem_lnk: like first_link
-- To speed up accessing, `mem_idx' and `mem_lnk' is the
-- memory of the last access done. For example, after
-- item(1), `mem_idx' is 1 and `mem_lnk' is `first_link'.
-- When list is empty, `first_link' is Void as well as
-- `mem_lnk' and `mem_idx' is 0
feature
make is
-- Make an empty list
do
first_link := Void
last_link := Void
upper := 0
mem_idx := 0
mem_lnk := Void
ensure
count = 0
end
is_empty: BOOLEAN is
do
Result := first_link = Void
end
add_first(element: like item) is
do
if first_link = Void then
!!first_link.make(element,Void,Void)
last_link := first_link
upper := 1
mem_idx := 1
mem_lnk := first_link
else
!!first_link.make(element,Void,first_link)
first_link.next.set_previous(first_link)
upper := upper + 1
mem_idx := mem_idx + 1
end
ensure then
upper = 1 + old upper
end
add_last(element: like item) is
do
if first_link = Void then
!!first_link.make(element,Void,Void)
last_link := first_link
upper := 1
mem_idx := 1
mem_lnk := first_link
else
!!last_link.make(element,last_link,Void)
last_link.previous.set_next(last_link)
upper := upper + 1
end
end
add(element: like item; index: INTEGER) is
local
link: like first_link
do
if index = 1 then
add_first(element)
elseif index = upper + 1 then
add_last(element)
else
if (index - 1) /= mem_idx then
go_item(index - 1)
end
!!link.make(element,mem_lnk,mem_lnk.next)
link.next.set_previous(link)
mem_lnk.set_next(link)
upper := upper + 1
end
end
remove_first is
do
if upper = 1 then
make
else
mem_lnk := first_link
first_link := first_link.next
first_link.set_previous(Void)
mem_lnk := first_link
mem_idx := 1
upper := upper - 1
end
end
remove(index: INTEGER) is
local
link: like first_link
do
if index = 1 then
remove_first
elseif index = upper then
remove_last
else
if (index - 1) /= mem_idx then
go_item(index - 1)
end
link := mem_lnk.next
mem_lnk.set_next(link.next)
link.next.set_previous(mem_lnk)
upper := upper - 1
end
end
first: like item is
do
Result := first_link.item
end
last: like item is
do
Result := last_link.item
end
item, infix "@" (index: INTEGER): E is
do
if index /= mem_idx then
go_item(index)
end
Result := mem_lnk.item
end
put(element: like item; index: INTEGER) is
do
if index /= mem_idx then
go_item(index)
end
mem_lnk.set_item(element)
end
count: INTEGER is
do
Result := upper
end
set_all_with(v: like item) is
do
if first_link /= Void then
first_link.set_all_with(v)
end
end
copy(other: like Current) is
do
from_collection(other)
end
is_equal(other: like Current): BOOLEAN is
local
lnk1, lnk2: like first_link
do
if Current = other then
Result := true
elseif upper = other.upper then
from
Result := true
lnk1 := first_link
lnk2 := other.first_link
until
lnk1 = Void or not Result
loop
Result := lnk1.item = lnk2.item
lnk1 := lnk1.next
lnk2 := lnk2.next
end
end
end
is_equal_map(other: like Current): BOOLEAN is
local
lnk1, lnk2: like first_link
do
if Current = other then
Result := true
elseif upper = other.upper then
from
Result := true
lnk1 := first_link
lnk2 := other.first_link
until
lnk1 = Void or not Result
loop
Result := safe_equal(lnk1.item,lnk2.item)
lnk1 := lnk1.next
lnk2 := lnk2.next
end
end
end
index_of(x: like item): INTEGER is
do
from
Result := lower
until
(Result > upper) or else safe_equal(x,item(Result))
loop
Result := Result + 1
end
end
fast_index_of(x: like item): INTEGER is
local
u: like upper
do
from
Result := lower
u := upper
until
(Result > u) or else x = item(Result)
loop
Result := Result + 1
end
end
clear is
do
if first_link /= Void then
first_link := Void
mem_idx := 0
mem_lnk := Void
upper := 0
last_link := Void
end
ensure then
upper = 0
end
from_collection(model: COLLECTION[like item]) is
local
i, up: INTEGER
lnk: like first_link
do
if first_link = Void then
from
i := model.lower
up := model.upper
until
i > up
loop
add_last(model.item(i))
i := i + 1
end
else
from
lnk := first_link
i := model.lower
up := model.upper
until
i > up
loop
if lnk = Void then
add_last(model.item(i))
else
lnk.set_item(model.item(i))
lnk := lnk.next
end
i := i + 1
end
if lnk = first_link then
check
model.count = 0
end
clear
elseif lnk /= Void then
i := model.count
if mem_idx /= i then
go_item(i)
end
check
lnk = mem_lnk.next
end
mem_lnk.set_next(Void)
upper := i
last_link := mem_lnk
end
end
end
slice(low, up: INTEGER): like Current is
local
lnk: like first_link
i: INTEGER
do
from
!!Result.make
if mem_idx /= low then
go_item(low)
end
lnk := mem_lnk
i := up - low + 1
until
i = 0
loop
Result.add_last(lnk.item)
lnk := lnk.next
i := i - 1
end
end
occurrences(element: like item): INTEGER is
local
lnk: like first_link
do
from
lnk := first_link
until
lnk = Void
loop
if safe_equal(element,lnk.item) then
Result := Result + 1
end
lnk := lnk.next
end
end
fast_occurrences(element: like item): INTEGER is
local
lnk: like first_link
do
from
lnk := first_link
until
lnk = Void
loop
if element = lnk.item then
Result := Result + 1
end
lnk := lnk.next
end
end
force(element: E; index: INTEGER) is
local
v: like element
do
from
until
index <= upper
loop
add_last(v)
end
put(element,index)
end
all_default: BOOLEAN is
local
l: like first_link
d: like item
do
from
Result := true
l := first_link
until
not Result or else l = Void
loop
d := l.item
if d /= Void then
Result := d.is_default
end
l := l.next
end
end
remove_last is
do
if upper = 1 then
make
else
if upper - 1 /= mem_idx then
go_item(upper - 1)
end
upper := upper - 1
last_link := mem_lnk
last_link.set_next(Void)
end
end
replace_all(old_value, new_value: like item) is
local
i: INTEGER
do
from
i := lower
until
i > upper
loop
if safe_equal(item(i),old_value) then
put(new_value,i)
end
i := i + 1
end
end
fast_replace_all(old_value, new_value: like item) is
local
i: INTEGER
do
from
i := lower
until
i > upper
loop
if item(i) = old_value then
put(new_value, i)
end
i := i + 1
end
end
reverse is
local
temp: E
low: like first_link
high: like first_link
i: INTEGER
do
from
low := first_link
high := last_link
i := count // 2
invariant
i > 0 implies (low /= Void and high /= Void)
i > 0 implies (low /= high and low.previous /= high)
until
i = 0
loop
temp := low.item
low.set_item (high.item)
high.set_item (temp)
low := low.next
high := high.previous
i := i - 1
end
end
get_new_iterator: ITERATOR[E] is
do
!ITERATOR_ON_TWO_WAY_LINKED_LIST[E]!Result.make(Current)
end
feature {NONE}
go_item(index: INTEGER) is
require
valid_index(index)
mem_idx /= index
mem_idx > 0
mem_lnk /= Void
do
if index > mem_idx then
if (upper - index) < (index - mem_idx) then
from
mem_idx := upper
mem_lnk := last_link
until
index = mem_idx
loop
mem_lnk := mem_lnk.previous
mem_idx := mem_idx - 1
end
else
from
until
index = mem_idx
loop
mem_lnk := mem_lnk.next
mem_idx := mem_idx + 1
end
end
elseif (mem_idx - index) < (index - 1) then
from
until
index = mem_idx
loop
mem_lnk := mem_lnk.previous
mem_idx := mem_idx - 1
end
else
from
mem_idx := 1
mem_lnk := first_link
until
index = mem_idx
loop
mem_lnk := mem_lnk.next
mem_idx := mem_idx + 1
end
end
ensure
mem_idx = index
mem_lnk /= Void
end
invariant
empty_status:
first_link = Void implies
(last_link = Void and upper = 0 and mem_idx = 0
and mem_lnk = Void)
not_empty_status:
first_link /= Void implies
(last_link /= Void and upper > 0 and mem_idx > 0
and mem_lnk /= Void)
end -- TWO_WAY_LINKED_LIST[E]
|