Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
yangxiujun
/
paidan_demo
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit 0991c619
authored
Jul 11, 2023
by
王力
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'dev_rescheduling20230711' into 'develop'
算法优化 See merge request !200
2 parents
5bcdc36a
1f25fd63
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
41 additions
and
26 deletions
project-order/src/main/java/com/dituhui/pea/order/common/SegmentInsertion.java
project-order/src/main/java/com/dituhui/pea/order/common/SegmentInsertion.java
View file @
0991c61
package
com
.
dituhui
.
pea
.
order
.
common
;
import
lombok.Data
;
import
lombok.extern.slf4j.Slf4j
;
import
java.util.List
;
@Slf4j
public
class
SegmentInsertion
{
@Data
public
static
class
Segment
{
private
String
sid
;
private
int
start
;
private
int
end
;
public
static
int
insertSegment
(
int
segmentLength
,
int
startRange
,
int
endRange
,
List
<
Segment
>
segments
)
{
public
Segment
(
String
sid
,
int
start
,
int
end
)
{
this
.
sid
=
sid
;
this
.
start
=
start
;
this
.
end
=
end
;
if
(
segments
.
isEmpty
())
{
return
0
;
}
int
pos
=
startRange
;
while
(
pos
<=
endRange
)
{
for
(
int
index
=
0
;
index
<
segments
.
size
();
index
++)
{
Segment
segment
=
segments
.
get
(
index
);
if
(
index
==
segments
.
size
()
-
1
)
{
// 最后一个节点
if
(
pos
>
segment
.
getEnd
())
{
return
index
+
1
;
}
}
public
static
int
insertSegment
(
int
segmentLength
,
int
startRange
,
int
endRange
,
List
<
Segment
>
occupiedSegments
)
{
if
(
pos
>=
segment
.
getStart
()
&&
pos
<=
segment
.
getEnd
())
{
// 起点在 segment 内,不符合要求
pos
=
segment
.
getEnd
()
+
1
;
continue
;
}
if
(
startRange
<
0
||
endRange
>
1440
)
{
return
-
1
;
// 新线段范围超出了有效范围,无法插入
if
(
pos
<
segment
.
getStart
()
&&
pos
+
segmentLength
>
segment
.
getStart
())
{
pos
=
segment
.
getEnd
()
+
1
;
continue
;
}
for
(
Segment
segment
:
occupiedSegments
)
{
if
(
startRange
<
segment
.
getEnd
()
&&
endRange
>
segment
.
getStart
())
{
return
-
1
;
// 新线段与已占用的线段重叠,无法插入
if
(
pos
<
segment
.
getStart
()
&&
pos
+
segmentLength
<
segment
.
getStart
()
)
{
return
index
;
}
}
}
for
(
int
i
=
0
;
i
<
occupiedSegments
.
size
();
i
++)
{
Segment
segment
=
occupiedSegments
.
get
(
i
);
if
(
i
==
0
&&
startRange
<
segment
.
getStart
()
&&
endRange
<=
segment
.
getStart
())
{
// 新线段与第一个线段相交,无法插入
return
i
;
}
else
if
(
i
==
occupiedSegments
.
size
()
-
1
&&
endRange
>
segment
.
getEnd
()
&&
startRange
>=
segment
.
getEnd
())
{
// 新线段与最后一个线段相交,无法插入
return
i
;
}
else
if
(
startRange
>
segment
.
getEnd
()
&&
endRange
<
occupiedSegments
.
get
(
i
+
1
).
getStart
())
{
return
i
;
return
-
1
;
}
@Data
public
static
class
Segment
{
private
String
sid
;
private
int
start
;
private
int
end
;
public
Segment
(
String
sid
,
int
start
,
int
end
)
{
this
.
sid
=
sid
;
this
.
start
=
start
;
this
.
end
=
end
;
}
return
-
1
;
// 无法插入,所有已占用线段都与新线段相交
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment